美文网首页自定义控件
简易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 通用指示器封装

    项目中需要使用到轮播图,为了方便,使用了Banner第三方库,但banner的轮播指示器太死,不符合项目要求,于是...

  • Android BannerView通用封装

    之前封装过一个,但总觉得不够优雅,就有了这个通用封装,很简洁,不知道够不够优雅,不过原来那个有跟随指示器和丝滑滑动...

  • jdM站首页

    导航变色方法封装 banner轮播图方法封装 倒计时秒杀方法

  • 自定义Banner控件

    自定义轮播的Banner网上有很多资源,经过自己思考后决定想尝试下自己写一个更为通用的Banner。 Banner...

  • Android常用的开源项目库三

    轮播图Android-ConvenientBanner★2548 - 通用的广告栏控件banner★2221 - ...

  • 自定义ViewPager指示器

    app的引导页和banner功能,通常采用ViewPager实现,并且往往都会有指示器,显示当前所选页。本文用自定...

  • uni-app方法封装

    请求数据方法封装 文件上传封装 通用方法

  • iOS 04、iOS实现从下面弹出视图popView工具类

    实现自下而上的弹出视图popView,包含动画效果,封装了一个简易的类,可以直接使用,本文章简易介绍一个封装类使用...

  • 自定义Banner

    使用自定义Banner 思路1.添加自定义ViewPager2.添加title背景3.添加指示器4.添加title...

  • Map一个点的范围扩展

    一、封装通用函数 二、使用

网友评论

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

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