【自定义view】 图片轮播+小圆点

本文介绍了如何在Android中实现自定义View,创建一个带有可配置选项的图片轮播器,包括小圆点的位置(居中或居右)和颜色。用户可以自定义小圆点的背景颜色、选中颜色以及未选中颜色,同时支持设置是否轮播和在无图片时的占位选项。
摘要由CSDN通过智能技术生成

图片轮播,小圆点可支持居中或居右设置,颜色可自定义,效果如下: 


 关键代码如下:

public class BannerView<T> extends RelativeLayout {
    private boolean isRight;
    private int point_size;
    private int point_bg;
    private int nomalColor;
    private int selectColor;

    private ViewPager vp;
    private RelativeLayout pointLayout;
    private List<T> pictureList;
    private T t;
    //是否在没有图片的情况下占位
    private boolean isNeedStaticLocaltion;
    //是否轮播
    private boolean isPlay = false;
    private static int MARGIN = ScreenUtil.dip2px(BaseApplication.getBaseApplication(), 5);
    private List<MyCilcleView> myCilcleViews;
    private List<ImageView> imageViews;
    private UserControal<T> userControal;
    private int FLAG = 1;
    private int currentPosition = 0;

    private float currentX = 0;
    private float currentY = 0;
    private float MINE_XY = 20;
    public static final int MARGIN_LEFtT = ScreenUtil.dip2px(BaseApplication.getBaseApplication(), 19);
    private int mDefaultImg = R.mipmap.ic_launcher;
    //宽高比例
    private float proportion = 0.6F;
    private Handler hander = new Handler() {
        @Override
        public void handleMessage(Message msg) {
            super.handleMessage(msg);
            if (null != msg) {
                if (msg.what == FLAG) {
                    hander.removeMessages(FLAG);
                    vp.setCurrentItem(currentPosition + 1);
                    hander.sendEmptyMessageDelayed(FLAG, 3000);
                }
            }
        }
    };

    public BannerView(Context context) {
        this(context, null);
    }

    public BannerView(Context context, AttributeSet attrs) {
        this(context, attrs, 0);
    }

    public BannerView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.BannerView);
        point_size = (int) array.getDimension(R.styleable.BannerView_point_size, 15);
        //point_bg = array.getColor(R.styleable.BannerView_point_bg, 0x00000000);
        point_bg = Color.parseColor("#00000000");
        isRight = array.getBoolean(R.styleable.BannerView_is_right, false);
        isPlay = array.getBoolean(R.styleable.BannerView_is_play, true);
        isNeedStaticLocaltion = array.getBoolean(R.styleable.BannerView_is_static_location, true);
        nomalColor = array.getColor(R.styleable.BannerView_point_color_unselect, Color.WHITE);
        selectColor = array.getColor(R.styleable.BannerView_point_color_select, Color.BLACK);
        proportion = array.getFloat(R.styleable.BannerView_banner_proportion, 0.6f);
        array.recycle();
    }

    /**
     * 添加ViewPager和指向器布局
     */
    private void init() {
        vp = new ViewPager(getContext());
        pointLayout = new RelativeLayout(getContext());
        addView(vp);
        addView(pointLayout);
        LayoutParams lpVp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT);
        LayoutParams lp = new LayoutParams(ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        lp.rightMargin = 38;
        pointLayout.setBackgroundColor(point_bg);
    
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值