自定义viewpager圆点

自定义viewpager圆点
public class IndicatorView extends LinearLayoutCompat {

    private Context context;

    private Drawable selectColor;
    private Drawable normalColor;
    private float alpha;

    public IndicatorView(Context context) {
        super(context);
        this.context = context;
    }

    public IndicatorView(Context context, AttributeSet attrs) {
        super(context, attrs);
        this.context = context;
    }

    public IndicatorView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
    }

    public IndicatorView initView(int indicatorCount,int selectPosition,Drawable selectDrawble,Drawable normalDrawble,float alpha){
        this.selectColor = selectDrawble;
        this.normalColor = normalDrawble;
        this.alpha = alpha;
        for (int i=0;i<indicatorCount;i++){
            AppCompatImageView ivIndicator = new AppCompatImageView(context);
            LinearLayoutCompat.LayoutParams lp = new LinearLayoutCompat.LayoutParams(LayoutParams.WRAP_CONTENT,LayoutParams.WRAP_CONTENT);
            lp.leftMargin = i == 0 ? 0 : dip2px(context,6);
            ivIndicator.setLayoutParams(lp);
            ivIndicator.setBackgroundDrawable(i == selectPosition ? selectDrawble : normalDrawble);
            ivIndicator.setAlpha(i == selectPosition ? 1 : alpha);
            addView(ivIndicator);
        }
        return this;
    }

    public void changeIndicator(int position){
        int count = getChildCount();
        for (int i=0;i<count;i++){
            AppCompatImageView ivIndecator  = (AppCompatImageView) getChildAt(i);
            ivIndecator.setBackgroundDrawable(normalColor);
            ivIndecator.setAlpha(alpha);
        }
        AppCompatImageView ivIndecator  = (AppCompatImageView) getChildAt(position);
        ivIndecator.setBackgroundDrawable(selectColor);
        ivIndecator.setAlpha(1.0f);
    }

    public static class Builder{
        private Context context;
        private int position;
        private int indicatorCount;
        private Drawable selectColor;
        private Drawable normalColor;
        private float alpha;

        public Builder setContext(Context context){
            this.context = context;
            return this;
        }

        public Builder setSelectPosition(int position){
            this.position = position;
            return this;
        }

        public Builder setIndicatorCount(int indicatorCount){
            this.indicatorCount = indicatorCount;
            return this;
        }

        public Builder setSelectColor(Drawable selectColor){
            this.selectColor = selectColor;
            return this;
        }

        public Builder setNormalColor(Drawable normalColor){
            this.normalColor = normalColor;
            return this;
        }

        public Builder setNormalAlpha(float alpha){
            this.alpha = alpha;
            return this;
        }

        public IndicatorView builder(){
            return  new IndicatorView(context).initView(indicatorCount,position,selectColor,normalColor,alpha);
        }
    }
    public static int dip2px(Context context, float dpValue) {
        return (int) ((dpValue * context.getResources().getDisplayMetrics().density) + 0.5f);
    }
}


indicator = new IndicatorView.Builder()
        .setContext(MainActivity.this)
//数量
        .setIndicatorCount(4)
        .setSelectPosition(0)
        .setSelectColor(getResources().getDrawable(R.drawable.sel_shap))
        .setNormalColor(getResources().getDrawable(R.drawable.nomal_shap))
        .setNormalAlpha(0.6f)
        .builder();
FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
indicator.setLayoutParams(layoutParams);
//添加到布局
main_ider.addView(indicator);

 

ViewPager滑动执行indicator.changeIndicator(arg0);

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值