android 轮播图(自定义组合控件)

SlideShowView

github:turemi/SlideShow
自动轮播图控件,自动添加小圆点指示器,标题栏展示,修改滚动速度以及添加动画插值器.

使用:

  • 添加依赖
    1.项目gradle添加一下配置:

      allprojects {
      	repositories {
      	...
      	maven { url 'https://jitpack.io' }
      	}
      }
    

    2.module中的gradle添加依赖:

      dependencies {
          implementation 'com.github.truemi:SlideShow:1.0'
      } 
    
  • xml中添加view:

      <com.truemi.slideshow.SlideShowView
     		android:id="@+id/slide_show"
      	android:layout_width="match_parent"
      	android:layout_height="200dp"
      	app:mDotRaduis="8px"//小圆点半径
      	app:mDotNormalColor="#999999"//小圆点默认颜色
      	app:mDotSelectColor="#FF0000"//小圆点选中的颜色
      	app:mDotlocation="bottom_right"//小圆点显示的位置
      	app:mAutoStandTime="5000"//每个界面停留的时间间隔
      	app:mBottomTextView="true"//是否显示底部标题栏
      	app:mDotNavigation="true"//是否显示小圆点
      	app:mTextColor="#FFF"//标题栏文字颜色
      	app:mTextSize="12sp"//标题栏文字大小
      	app:mTextBgColor ="#44000000"//标题栏背景颜色
      	app:mBottomTextViewHeight="40dp">//标题栏高度
      </com.truemi.slideshow.SlideShowView>
    
  • activity中设置数据:

      //图片集合
      ArrayList<String> urlLists = new ArrayList<>();
      urlLists.add("https://img03.sogoucdn.com/app/a/100520024/c25c07885f822d67c91256b3033749e7");
      urlLists.add("https://img04.sogoucdn.com/app/a/100520024/ee6b8a48e6322e18a85a62ddcb01f432");
      urlLists.add("https://img01.sogoucdn.com/app/a/100520024/ebb532d5da0e26e285ac2dc025bc99ec");
      urlLists.add("https://img01.sogoucdn.com/app/a/100520024/83922cd9e4aaf9b4c012f08629a5e160");
      //标题栏文字集合
      String titles[] ={" 足球 ","设计 时尚"," 风华绝代 一代巨星张国荣","发现时光的痕迹"};
      final SlideShowView slideShow = findViewById(R.id.slide_show);
      //设置adapter,构造方法还可以传入图片资源id数组
      slideShow.setAdapter(new SlideAdapter(this,urlLists,titles));
      //图片点击事件
      slideShow.setOnItemClickListener(new SlideShowView.OnViewPagerItemClickListener() {
          @Override
          public void onViewPagerItemClick(int position) {
              Toast.makeText(MainActivity.this,"点击了第"+position+"张图片", Toast.LENGTH_SHORT).show();
          }
      }); 
    

自定义属性:

属性 描述
mDotNavigation true/false 是否显示小圆点(默认显示)
mBottomTextView true/false 是否显示底部标题栏(默认显示)
mDotRaduis 2dp 小圆点半径
mDotNormalColor #999999 小圆点默认颜色
mDotSelectColor #FF0000 小圆点选中的颜色(当前显示的)
mDotlocation bottom_center 小圆点显示的位置(默认bottom_right)
mDuration 500 页面切换时间
mAutoStandTime 5000 页面停留时间
mBottomTextViewHeight 40dp 标题栏高度
mTextSize 12sp 标题栏文字大小
mTextColor #FFFFFF 标题栏文字颜色
mTextBgColor #44000000 标题栏背景颜色

公开方法:

  • setAdapter(SlideAdapter adapter);//设置adapter
  • setDuration(int mDuration, Interpolator interpolator);//设置界面切换时间和动画插值器
  • setPageTransformer(boolean b, ViewPager.PageTransformer transformer);//设置切换动画
  • setOnItemClickListener;//设置点击事件
  • setBottomTextBg(int color);//设置标题栏背景颜色
  • setBottomTextColor(int color);//设置标题栏文字颜色
  • setBottomTextSize(int size);//设置标题栏文字大小

注意:

  • 库中使用glide作为图片加载框架,如果你的项目中已经使用glide,直接删除你项目中的依赖即可
  • 使用前请在androidManifest.xml中添加网络权限
android自定义组合控件(使用原生控件,组合成为一个整体,具有新的功能的控件)是android自定义控件的重要组成部分.适当的组合控件,可以提高开发的效率.

#####需求:

  1. 轮播图自动滚动
    2.手势滑动与自动轮播不冲突,手指触摸时,不轮播(触摸事件的处理)
    3.提供对外的点击事件监听(点击动作的识别)
    4.根据轮播图得数量绘制"小红点",可以设置"小红点"的位置(代码布局),“小红点"的颜色随页面切换变化
    5.极简的使用方法
    #####思路:
    1.轮播图使用RelativeLayout中添加ViewPager的布局形式,(使用RelativeLayout作为容器,是为了添加指示器"小红点”)
    2.指示器"小红点"使用自定义View,根据传入的图片/链接的数量绘制指示器.
    3.把指示器放入指定的位置
    #####步骤:看代码读注释(注释比较详细)
    ######1.指示器"小红点"的绘制:
    创建一个类,继承View,提供一个公开的方法获取小红点的数量
public DotView setDotCount(int count) {
      dotCount = count;
      return this;
  }

提供一个公开的方法刷新小红点的状态

 public DotView changePager(int pager) {
        this.selectedPager = pager;//当前界面展示的图片的角标(第几张图片)
        postInvalidate();//重新绘制
        return this;
    }

onMeasure方法中计算宽高

 @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        for (int i = 0; i < dotCount; i++) {
            viewWidth = i * MARGING + RADUIS;
        }
       int viewHeight = RADUIS*2;//一个小红点的高度
        setMeasuredDimension(viewWidth+RADUIS, viewHeight);//从0开始遍历,加上缺少的半径为View的宽度
    }

绘制小红点

private void drawCricle(Canvas canvas) {

        for (int i = 0; i < dotCount; i++) {
            if (selectedPager == i) {//当前界面展示图片
                paint.setColor(dotSelectColor);//设置界面指示的小红点的颜色
            } else {
                paint.setColor(dotNormalColor);//设置其他小红点的颜色
            }
            //MARGING两个圆心之间的距离,RADUIS半径
            canvas.drawCircle((i) * MARGING + RADUIS, RADUIS, RADUIS, paint);//绘制
        }
    }

"小红点"完整代码

public class DotView extends View {
    private int RADUIS;
    private int MARGING;
    private int dotCount;
    private Paint paint;
    private int dotNormalColor;
    private int dotSelectColor;
    private int selectedPager;
    Context context;
    private int viewWidth;

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

    public DotView(Context context, @Nullable AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public DotView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        paint = new Paint();
        paint.setColor(dotNormalColor);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        drawCricle(canvas);
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        for (int i = 0; i < dotCount; i++) {
            viewWidth = i * MARGING + RADUIS;
        }
       int viewHeight = RADUIS*2;
        setMeasuredDimension(viewWidth+RADUIS, viewHeight);
    }

    private void drawCricle(Canvas canvas) {

        for (int i = 0; i < dotCount; i++) {
            if (selectedPager == i) {
                paint.setColor(dotSelectColor);
            } else {
                paint.setColor(dotNormalColor);
            }
            canvas.drawCircle((i) * MARGING + RADUIS, RADUIS, RADUIS, paint);
        }
    }

    public DotView setRaduis(int raduis) {
        RADUIS = raduis;
        return this;
    }

    public DotView setMaring(int maring) {
        MARGING = maring;
        return this;
    }

    public DotView setDotCount(int count) {
        dotCount = count;
        return this;
    }

    public DotView setdotNormalColor(int color) {
        this.dotNormalColor = color;
        return this;
    }

    public DotView setdotSelectColor(int color) {
        this.dotSelectColor = color;
        return this;
    }

    public DotView  changePager(int pager) {
        this.selectedPager = pager;
        postInvalidate();
        return this;
    }
}

######2.组合控件:
创建个类SlideShowView 继承 RelativeLayout ,(ViewPager的一些公开的方法如果有需求,自己提供公开的方法即可).实现构造方法,自定义属性:

<declare-styleable name="SlideShowView">
        <attr name="mDotRaduis" format="dimension"/> <!--小红点的半径-->
        <attr name="mDotNormalColor" format="color"/><!--小红点指示的颜色-->
        <attr name="mDotSelectColor" format="color"/><!--小红点不指示的颜色-->
        <attr name="mDotlocation" format="enum"><!--小红点的位置,底部居中,底部靠左,底部靠右-->
            <enum name="bottom_center" value="0" />
            <enum name="bottom_left" value="-1" />
            <enum name="bottom_right" value="1" />
        </attr>
 <!--页面切换时间-->
        <attr name="mDuration" format="integer"/>
        <!--自动切换界面停留时间-->
        <attr name="mAutoStandTime" format="integer"/>
        <attr name="mBottomTextViewHeight" format="dimension"/>
        <!--是否显示底部标题栏-->
        <attr name="mBottomTextView" format="boolean"/>
        <attr name="mTextSize" format="dimension"/>
        <attr name="mTextColor" format="color"/>
        <attr name="mTextBgColor" format="color"/>
    </declare-styleable>

代码中获取属性值

 if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.MyViewPager);
            RADUIS = (int) typedArray.getDimension(R.styleable.MyViewPager_mDotRaduis, 0);
            MARGING = 4 * RADUIS;//获取半径设置间距
            dotNormalColor = typedArray.getColor(R.styleable.MyViewPager_mDotNormalColor, 0);
            dotSelectColor = typedArray.getColor(R.styleable.MyViewPager_mDotSelectColor, 0);
            dotLocation = typedArray.getInt(R.styleable.MyViewPager_mDotlocation, 0);//位置
            typedArray.recycle();
        }

添加viewPager到RelativeLayout中

 viewPager = new ViewPager(context);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        viewPager.setLayoutParams(layoutParams);
        viewPager.setOnPageChangeListener(this);
        viewPager.setOnTouchListener(this);
        addView(viewPager);

通过Handler控制界面切换

 private int     itemPosition = 0;
    private Handler mHandler     = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            mHandler.removeMessages(0);//消息清零
            if (!isTouch) {//判断手指是否触摸,没有触摸自动滚动,否则不发送消息
                if (itemPosition == count) {//已经是最后一个,则跳到第0个,否则跳到下一个
                    itemPosition = 0;
                    viewPager.setCurrentItem(itemPosition, false);
                } else {
                    itemPosition++;
                    viewPager.setCurrentItem(itemPosition, true);
                }
                mHandler.sendEmptyMessageDelayed(0, 2000);//延迟两秒发送消息
            }
            return false;
        }
    });

通过viePager的监听,判断位置,绘制小红点的状态

 /**
     * 绘制小圆点
     * @param position
     */
    private void drawCricle(int position) {
        if (dot != null)
            removeView(dot);
        dot = new PagerPointDotView2(context);//创建小红点实例
        dot.setDotCount(count)//设置小红点数量
                .setdotNormalColor(dotNormalColor)//默认颜色
                .setdotSelectColor(dotSelectColor)//选中颜色
                .setMaring(MARGING).setRaduis(RADUIS);//设置间距和半径
//设置小红点的位置
        RelativeLayout.LayoutParams layoutParamsDot2 = new RelativeLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, RADUIS * 2);//设置宽高
        layoutParamsDot2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);//位于父布局底部
        if (dotLocation == 0) {
            layoutParamsDot2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);//水平居中
            layoutParamsDot2.setMargins(0, 0, 0, 20);//下边距20
        } else if (dotLocation == -1) {
            layoutParamsDot2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);//父布局左对齐
            layoutParamsDot2.setMargins(30, 0, 0, 20);//左边距30,下边距20
        } else if (dotLocation == 1) {
            layoutParamsDot2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);//父布局右对齐
            layoutParamsDot2.setMargins(0, 0, 30, 20);//左边距右边距,下边距20
        }
        dot.changePager(position);//绘制小红点
        addView(dot, layoutParamsDot2);//添加小红点的RelativeLayout中
    }

点击事件处理,ViewConfiguration.get(context).getScaledEdgeSlop();获取滑动标识(使用识别是否右滑动动作的数值)

 case MotionEvent.ACTION_UP:
                long touchUpTime = System.currentTimeMillis();
                long l = touchUpTime - touchDownTime;
                //触摸移动距离小于滑动识别距离,触摸时间小于200ms确认为点击事件
                if (scaledEdgeSlop > (mCurPosY - mPosY) && scaledEdgeSlop > (mCurPosX - mPosX) && l < 200) {
                    clickListener.onViewPagerItemClick(viewPager.getCurrentItem());
                }
                isTouch = false;
                mHandler.sendEmptyMessageDelayed(0, 2000);
                break;

完整代码如下:

public class SlideShowView extends RelativeLayout implements ViewPager.OnPageChangeListener, View.OnTouchListener {

    private Context   context;
    private Paint     paint;
    private ViewPager viewPager;
    private int       RADUIS;//小圆点半径
    private int MARGING = 50;
    private int height;
    private int dotNormalColor = 0xFFFFFF;
    private int dotSelectColor = 0x000000;
    private PageChangeListener listener;
    private DotView            dot;
    private boolean            isTouch;//是否手势触摸
    private int                count;//轮播图数量
    private long               touchDownTime;
    private int                scaledEdgeSlop;
    private int                mPosX;
    private int                mPosY;
    private int                mCurPosX;
    private int                mCurPosY;
    private int                dotLocation;//小圆点的位置(-1,0,1),分别表示左边,居中,右边
    private float   bottomTextSize    = 12;//标题文字大小,sp
    private int     mTextBgColor = 0x44000000;//标题背景颜色
    private int     bottomTextColor   = 0x00e9e9e9;//标题文字颜色
    private int     mDuration         = 500;//滚动时间
    private int     mAutoStandTime    = 5000;//自动滚动停留时间
    private boolean haveBottomText    = true;//是否显示标题栏,默认显示
    private boolean mDotNavigation    = true;//是否显示小圆点,默认显示
    private TextView textView;//标题
    private int bottomTextHeight = 20;//dp,标题栏高度
    private String[]           titleText;//标题String数组
    private FixedSpeedScroller mScroller;

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

    public SlideShowView(Context context, AttributeSet attrs) {
        this(context, attrs, -1);
    }

    public SlideShowView(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        this.context = context;
        init(attrs);
        scaledEdgeSlop = ViewConfiguration.get(context).getScaledEdgeSlop();
    }

    private void init(AttributeSet attrs) {
        if (attrs != null) {
            TypedArray typedArray = getContext().obtainStyledAttributes(attrs, R.styleable.SlideShowView);
            RADUIS = (int) typedArray.getDimension(R.styleable.SlideShowView_mDotRaduis, 0);
            MARGING = 4 * RADUIS;
            dotNormalColor = typedArray.getColor(R.styleable.SlideShowView_mDotNormalColor, 0);
            dotSelectColor = typedArray.getColor(R.styleable.SlideShowView_mDotSelectColor, 0);
            dotLocation = typedArray.getInt(R.styleable.SlideShowView_mDotlocation, 1);
            mDuration = typedArray.getInt(R.styleable.SlideShowView_mDuration, 500);
            mAutoStandTime = typedArray.getInt(R.styleable.SlideShowView_mAutoStandTime, 5000);
            bottomTextHeight = typedArray.getDimensionPixelOffset(R.styleable.SlideShowView_mBottomTextViewHeight, 20);
            mDotNavigation = typedArray.getBoolean(R.styleable.SlideShowView_mDotNavigation, true);
            haveBottomText = typedArray.getBoolean(R.styleable.SlideShowView_mDuration, true);
            bottomTextSize = typedArray.getDimension(R.styleable.SlideShowView_mTextSize, 12);//sp
            bottomTextColor = typedArray.getColor(R.styleable.SlideShowView_mTextColor, 0x00e9e9e9);
            mTextBgColor = typedArray.getColor(R.styleable.SlideShowView_mTextBgColor, 0x1d000000);
            typedArray.recycle();

        }

        paint = new Paint();
        paint.setColor(dotNormalColor);
        paint.setStyle(Paint.Style.FILL);
        paint.setAntiAlias(true);

        viewPager = new ViewPager(context);
        LayoutParams layoutParams = new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT);
        viewPager.setLayoutParams(layoutParams);
        viewPager.setOnPageChangeListener(this);
        viewPager.setOnTouchListener(this);
        addView(viewPager);

        if (mDotNavigation) {
            drawCricle(0);
        }
        initBottomTextView();
        setDuration(mDuration,new DecelerateInterpolator());
    }

    /**
     * 初始化底部标题栏
     */
    private void initBottomTextView() {
        if (dotLocation == -1 || dotLocation == 1 && haveBottomText) {
            textView = new TextView(context);
            textView.setBackgroundColor(mTextBgColor);
            textView.setTextColor(bottomTextColor);
            textView.setTextSize(Uiutils.px2sp(context,bottomTextSize));
            if (dotLocation == -1) {
                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.RIGHT);
                textView.setPadding(0, 0, 20, 0);
            } else {
                textView.setGravity(Gravity.CENTER_VERTICAL | Gravity.LEFT);
                textView.setPadding(20, 0, 0, 0);
            }
            Log.e("height","--------"+bottomTextHeight);
            LayoutParams layoutParamsText = new LayoutParams(LayoutParams.MATCH_PARENT, bottomTextHeight);
            layoutParamsText.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
            addView(textView, layoutParamsText);
        }
    }

    public void setAdapter(SlideAdapter adapter) {
//        ArrayList<String> urlList = adapter.getpagerItemUrl();
//        int[] imgList = adapter.getpagerItemImg();
//        int[] imgs = new int[imgList.length+2];
//        if (urlList!=null&&urlList.size()>0){
//            String s = urlList.get(urlList.size() - 1);
//            urlList.add(0,s);
//            urlList.add(urlList.get(1));
//            adapter.setUrlList(urlList);
//        }
//        if (imgList!=null&&imgList.length>0){
//            int length = imgList.length;
//            LogUtil.e(length+" 长度----------------------");
//            imgs[0] =imgList[length-1];
//            for (int i = 0; i < imgList.length; i++) {
//                imgs[i+1] =imgList[i];
//            }
//            imgs[imgs.length-1] = imgList[0];
//            adapter.setImgsList(imgs);
//        }
        count = adapter.getImgCount();
        titleText = adapter.getTitleText();
        viewPager.setAdapter(adapter);
        viewPager.setCurrentItem(Integer.MAX_VALUE / 2);
        mHandler.sendEmptyMessageDelayed(0, mAutoStandTime);
    }


    public void setBottomTextBg(int color) {
        if (textView == null) return;
        this.textView.setBackgroundColor(color);
    }

    public void setBottomTextColor(int color) {
        if (textView == null) return;
        this.textView.setTextColor(color);
    }

    public void setBottomTextSize(int size) {
        if (textView == null) return;
        this.textView.setTextSize(Uiutils.dp2px(context, size));
    }

    /**
     * 设置动画时间
     * @param mDuration
     */
    public void  setDuration(int mDuration, Interpolator interpolator){
        this.mDuration =mDuration;
        try {
            // 通过class文件获取mScroller属性
            Field mField = ViewPager.class.getDeclaredField("mScroller");
            mField.setAccessible(true);
            mScroller = new FixedSpeedScroller(viewPager.getContext(), interpolator);
            mField.set(viewPager, mScroller);
        } catch (Exception e) {
            e.printStackTrace();
        }
    }


    /**
     * 设置transformer
     *
     * @param b
     * @param transformer
     */
    public void setPageTransformer(boolean b, ViewPager.PageTransformer transformer) {
        if (viewPager != null) {
            viewPager.setPageTransformer(b, transformer);
        }
    }

    @Override
    public boolean onTouch(View view, MotionEvent motionEvent) {
        switch (motionEvent.getAction()) {
            case MotionEvent.ACTION_DOWN:
                touchDownTime = System.currentTimeMillis();
                mPosX = (int) motionEvent.getX();
                mPosY = (int) motionEvent.getY();
            case MotionEvent.ACTION_MOVE:
                isTouch = true;
                mCurPosX = (int) motionEvent.getX();
                mCurPosY = (int) motionEvent.getY();
                break;
            case MotionEvent.ACTION_UP:
                long touchUpTime = System.currentTimeMillis();
                long l = touchUpTime - touchDownTime;
                //触摸移动距离小于滑动识别距离,触摸时间小于200ms确认为点击事件
                if (scaledEdgeSlop > (mCurPosY - mPosY) && scaledEdgeSlop > (mCurPosX - mPosX) && l < 200) {
                    if (clickListener != null)
                        clickListener.onViewPagerItemClick(viewPager.getCurrentItem()%count);
                }
                isTouch = false;
                //延时1秒,如果没有触摸就自动开始滚动
                new Timer().schedule(new TimerTask() {
                    @Override
                    public void run() {
                        if (!isTouch) {
                            mHandler.sendEmptyMessageDelayed(0, mAutoStandTime - 1000);
                        }
                    }
                }, 1000);

                break;
        }

        return false;
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        height = MeasureSpec.getSize(heightMeasureSpec);
    }


    private int     itemPosition = 0;
    private Handler mHandler     = new Handler(new Handler.Callback() {
        @Override
        public boolean handleMessage(Message message) {
            mHandler.removeMessages(0);
            if (!isTouch) {
//                if (itemPosition == count-1) {
//                    itemPosition = 1;
//                    viewPager.setCurrentItem(itemPosition, false);
//                    mHandler.sendEmptyMessageDelayed(0, 0);
//                }else if (itemPosition==0){
//                    itemPosition = count-1;
//                    viewPager.setCurrentItem(itemPosition, false);
//                    mHandler.sendEmptyMessageDelayed(0, 0);
//                }else {
                itemPosition++;
                viewPager.setCurrentItem(itemPosition, true);
                mHandler.sendEmptyMessageDelayed(0, mAutoStandTime);
//                }

            }
            return false;
        }
    });


    @Override
    public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {

        if (listener != null) {
            listener.onPageScrolled(position% count, positionOffset, positionOffsetPixels);
        }

//        if (position == 0 && positionOffset == 0) viewPager .setCurrentItem(count - 1, false);
//        else if (position == count - 1 && positionOffset == 0) viewPager .setCurrentItem(1, false);
    }

    @Override
    public void onPageSelected(int position) {
        if (listener != null)
            listener.onPageSelected(position% count);
        itemPosition = position;
        viewPager.setCurrentItem(position);
        mScroller.setmDuration(mDuration);//设置页面切换时间
        if (textView != null && titleText != null && titleText.length > 0) {
            textView.setText(titleText[position % count]);
        }
        drawCricle(position % count);
    }

    @Override
    public void onPageScrollStateChanged(int state) {
        if (listener != null)
            listener.onPageScrollStateChanged(state);
    }

    /**
     * 绘制小圆点
     *
     * @param position
     */
    private void drawCricle(int position) {
        if (dot != null)
            removeView(dot);
        dot = new DotView(context);

        dot.setDotCount(count)
                .setdotNormalColor(dotNormalColor)
                .setdotSelectColor(dotSelectColor)
                .setMaring(MARGING).setRaduis(RADUIS);
        LayoutParams layoutParamsDot2 = new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, RADUIS * 2);

        layoutParamsDot2.addRule(RelativeLayout.ALIGN_PARENT_BOTTOM);
        if (dotLocation == 0) {
            layoutParamsDot2.addRule(RelativeLayout.CENTER_HORIZONTAL, RelativeLayout.TRUE);
            layoutParamsDot2.setMargins(0, 0, 0, bottomTextHeight / 3);
        } else if (dotLocation == -1) {
            layoutParamsDot2.addRule(RelativeLayout.ALIGN_PARENT_LEFT, RelativeLayout.TRUE);
            layoutParamsDot2.setMargins(30, 0, 0, bottomTextHeight / 3);
        } else if (dotLocation == 1) {
            layoutParamsDot2.addRule(RelativeLayout.ALIGN_PARENT_RIGHT, RelativeLayout.TRUE);
            layoutParamsDot2.setMargins(0, 0, 30, bottomTextHeight / 3);
        }
        dot.changePager(position);
        addView(dot, layoutParamsDot2);

    }


    /**
     * 页面切换监听
     */
    public interface PageChangeListener {
        void onPageScrolled(int position, float positionOffset, int positionOffsetPixels);

        void onPageSelected(int position);

        void onPageScrollStateChanged(int state);
    }

    public void setOnPageChangeListener(PageChangeListener listener) {
        this.listener = listener;
    }


    /**
     * 页面点击监听
     */
    public interface OnViewPagerItemClickListener {
        void onViewPagerItemClick(int position);
    }

    private OnViewPagerItemClickListener clickListener;

    public void setOnItemClickListener(OnViewPagerItemClickListener clickListener) {
        this.clickListener = clickListener;
    }
}

猜你喜欢

转载自blog.csdn.net/m0_37780940/article/details/103210805