美文网首页自定义控件
简易Banner 通用指示器封装

简易Banner 通用指示器封装

作者: 小白彡 | 来源:发表于2020-01-11 20:05 被阅读0次

    项目中需要使用到轮播图,为了方便,使用了Banner第三方库,但banner的轮播指示器太死,不符合项目要求,于是自己封装了一个方便的指示器,下面是使用效果:


    QQ图片20200111195135.jpg QQ图片20200111195146.jpg

    下面看一下怎么调用的

            IndicatorView indicator = new IndicatorView.Builder()
                    .setContext(_mActivity)
                    //指示器数量
                    .setIndicatorCount(indicatorCount)
                    //初始选择第几个
                    .setSelectPosition(0)
                    //被选中后的样式
                    .setSelectColor(selectDrawble)
                    //未选中的样式
                    .setNormalColor(normalDrawble)
                    //未选中的透明度 0 ~ 1
                    .setNormalAlpha(0.6f)
                    .builder();
    

    只需要将需要的样式传进去就可以了,会自行根据banner的数量生成指示器。
    生成后将指示器添加到自己的ViewGroup中就可以了,可以放在任何位置

    FrameLayout.LayoutParams layoutParams = new FrameLayout.LayoutParams(FrameLayout.LayoutParams.WRAP_CONTENT, FrameLayout.LayoutParams.WRAP_CONTENT);
            layoutParams.gravity = Gravity.BOTTOM | Gravity.CENTER_HORIZONTAL;
            indicator.setLayoutParams(layoutParams);
           //将指示器添加到想要放置的位置
            viewGroup.addView(indicator);
    

    之后在Viewpage的监听中改变选择的指示器:

    //改变指示器选中项
    indicatorView.changeIndicator(i);
    

    只是简单的使用,没做复杂的动画,需要做动画的可以将指示器的Imageview替换成自定义View实现自己想要的动画,下面贴一下代码:

    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 : SizeUtils.dp2px(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);
            }
        }
    }
    

    相关文章

      网友评论

        本文标题:简易Banner 通用指示器封装

        本文链接:https://www.haomeiwen.com/subject/ssebactx.html