Android carousel map (custom combination control)

SlideShowView

github:turemi/SlideShow
automatic carousel control, automatically add small dot indicator, title bar display, modify scrolling speed and add animation interpolator.

use:

  • Add dependencies
    1. Add configuration to project gradle:

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

    2. The gradle in the module adds dependencies:

      dependencies {
          implementation 'com.github.truemi:SlideShow:1.0'
      } 
    
  • Add view to xml:

      <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>
    
  • Set data in 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();
          }
      }); 
    

Custom properties:

Attributes value describe
mDotNavigation true/false Whether to display small dots (default display)
mBottomTextView true/false Whether to display the bottom title bar (default display)
mDotRaduis 2dp dot radius
mDotNormalColor #999999 Dot default color
mDotSelectColor #FF0000 The color selected by the small dot (currently displayed)
mDotlocation bottom_center The position displayed by the small dot (default bottom_right)
mDuration 500 page switching time
mAutoStandTime 5000 time on page
mBottomTextViewHeight 40dp title bar height
mTextSize 12sp Title bar text size
mTextColor #FFFFFF title bar text color
mTextBgColor #44000000 Title bar background color

Public method:

  • setAdapter(SlideAdapter adapter);//Set adapter
  • setDuration(int mDuration, Interpolator interpolator);//Set interface switching time and animation interpolator
  • setPageTransformer(boolean b, ViewPager.PageTransformer transformer);//Set switching animation
  • setOnItemClickListener;//Set click event
  • setBottomTextBg(int color);//Set the title bar background color
  • setBottomTextColor(int color);//Set the title bar text color
  • setBottomTextSize(int size);//Set the title bar text size

Notice:

  • The library uses glide as the image loading framework. If you have used glide in your project, just delete the dependencies in your project
  • Please add network permission in androidManifest.xml before use
Android custom combined controls (using native controls, combined into a whole, controls with new functions) are an important part of android custom controls. Appropriate combined controls can improve the efficiency of development.

#####need:

  1. The carousel map automatically scrolls
    2. Gesture sliding does not conflict with the automatic carousel, and when the finger touches, it does not rotate (handling of touch events)
    3. Provides external click event monitoring (recognition of click actions)
    4. According to the carousel map Draw "little red dots" in quantity, you can set the position of "little red dots" (code layout), the color of "little red dots" changes with the page switching
    5. Minimalist usage
    ##### Ideas:
    1. Wheel The layout form of adding ViewPager in RelativeLayout is used for broadcasting pictures, (using RelativeLayout as a container is to add the indicator "little red dot")
    2. The indicator "little red dot" uses a custom View, according to the incoming picture/link Quantity drawing indicator.
    3. Put the indicator into the specified position
    ##### Steps: read the comments in the code (the comments are more detailed)
    ######1. Drawing the indicator "little red dot":
    create A class that inherits View and provides a public method to obtain the number of small red dots
public DotView setDotCount(int count) {
      dotCount = count;
      return this;
  }

Provide a public method to refresh the status of the little red dot

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

Calculate the width and height in the onMeasure method

 @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的宽度
    }

draw little red dot

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);//绘制
        }
    }

"Little red dot" complete code

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. Combination control:
Create a class SlideShowView to inherit RelativeLayout, (if there is a need for some public methods of ViewPager, you can provide public methods yourself). Implement the construction method and customize the properties:

<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>

Get attribute value in code

 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();
        }

Add viewPager to 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);

Control interface switching through 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;
        }
    });

Through the monitoring of viePager, judge the position and draw the status of the small red dot

 /**
     * 绘制小圆点
     * @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中
    }

Click event processing, ViewConfiguration.get(context).getScaledEdgeSlop(); Get the sliding logo (use the value to identify whether the right sliding action)

 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;

The complete code is as follows:

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;
    }
}

Guess you like

Origin blog.csdn.net/m0_37780940/article/details/103210805