ViewPager自定义indicator指示器,这里是做一下记录,以免忘记

时间:2022-08-29 23:57:40

代码复制过去就能使用,还是希望读者看懂再去用,对学习有帮助

第一种方式,根据hongyang大神的博客记录下来的

package com.pro.view;

 
import com.pro.activity.R;
 
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.widget.LinearLayout;
 
public class SimpleViewPagerIndicator extends LinearLayout {
    
    private static final String TAG = SimpleViewPagerIndicator.class.getSimpleName();
    
    private int mTabCount;
    private int mIndicatorColor = R.color.color_line;
    private float mTranslationX;
    private Paint mPaint = new Paint();
    private int mTabWidth;
 
    public SimpleViewPagerIndicator(Context context) {
        this(context, null);
    }
 
    public SimpleViewPagerIndicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        mPaint.setColor(mIndicatorColor);
        mPaint.setStrokeWidth(20.0F);
    }
    
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        //如果需要设置textView的点击事件,在此设置
        mTabCount = getChildCount();
    }
    
    @Override
    protected void onSizeChanged(int w, int h, int oldw, int oldh) {
        super.onSizeChanged(w, h, oldw, oldh);
        mTabWidth = w / mTabCount;
    }
 
    public void setIndicatorColor(int indicatorColor) {
        this.mIndicatorColor = indicatorColor;
    }
 
    @Override
    protected void dispatchDraw(Canvas canvas) {
        super.dispatchDraw(canvas);
        canvas.save();
        canvas.translate(mTranslationX, getHeight() - 2);
        canvas.drawLine(0, 0, mTabWidth, 0, mPaint);
        canvas.restore();
    }
 
    public void scroll(int position, float offset) {
        mTranslationX = getWidth() / mTabCount * (position + offset);
        invalidate();
    }
    
}


第二种方式

package com.pro.view;

import com.pro.activity.R;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.util.AttributeSet;
import android.widget.LinearLayout;

public class Indicator extends LinearLayout {
    
    private Paint mPaint; // 画指示符的paint
    
    private int mTop; // 指示符的top
    private int mLeft; // 指示符的left
    private int mWidth; // 指示符的width
    private int mHeight = 5; // 指示符的高度,固定了
    private int mColor; // 指示符的颜色
    private int mChildCount; // 子item的个数,用于计算指示符的宽度
    
    public Indicator(Context context, AttributeSet attrs) {
        super(context, attrs);
        setBackgroundColor(Color.TRANSPARENT);  // 必须设置背景,否则onDraw不执行
        
        // 获取自定义属性 指示符的颜色
        TypedArray ta = context.obtainStyledAttributes(attrs, R.styleable.Indicator, 0, 0);
        mColor = ta.getColor(R.styleable.Indicator_color, 0X0000FF);
        ta.recycle();
        
        // 初始化paint
        mPaint = new Paint();
        mPaint.setColor(mColor);
        mPaint.setAntiAlias(true);
    }
    
    @Override
    protected void onFinishInflate() {
        super.onFinishInflate();
        mChildCount = getChildCount();  // 获取子item的个数
    }
    
    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mTop = getMeasuredHeight(); // 测量的高度即指示符的顶部位置
        int width = getMeasuredWidth(); // 获取测量的总宽度
        int height = mTop + mHeight; // 重新定义一下测量的高度
        mWidth = width / mChildCount; // 指示符的宽度为总宽度/item的个数
        
        setMeasuredDimension(width, height);
    }
    
    /**
     * 指示符滚动
     * @param position 现在的位置
     * @param offset  偏移量 0 ~ 1
     */
    public void scroll(int position, float offset) {
        mLeft = (int) ((position + offset) * mWidth);
        invalidate();
    }
    
    @Override
    protected void onDraw(Canvas canvas) {
        // 圈出一个矩形
        Rect rect = new Rect(mLeft, mTop, mLeft + mWidth, mTop + mHeight);
        canvas.drawRect(rect, mPaint); // 绘制该矩形
        super.onDraw(canvas);
    }
    
}


第三种方式

<ImageView
        android:id="@+id/setting_theme_pager_progressbar"
        android:layout_width="60dp"
        android:layout_height="2dp"
        android:layout_alignBottom="@id/ll_headview"
        android:background="@drawable/setting_theme_pager_progressbar" />


/** 进度条 **/
    private ImageView mPagerProgressBar;
    private RelativeLayout.LayoutParams mLayoutParams;
    /** 进度条移动值 **/
    private Integer mPagerProgressBarMoveLength;


private void initView() {
        mPagerProgressBar = (ImageView) findViewById(Res.id.setting_theme_pager_progressbar);
        mLayoutParams = (RelativeLayout.LayoutParams) mPagerProgressBar.getLayoutParams();
    }


@Override
    public void onWindowFocusChanged(boolean hasFocus) {
        super.onWindowFocusChanged(hasFocus);

            mLayoutParams.width = (int) (mtabGroup.getChildAt(0).getHeight() * 0.9);
            mLayoutParams.leftMargin = (mOneTabWidth - mLayoutParams.width) / 2;
            mPagerProgressBar.setLayoutParams(mLayoutParams);

     }

}

private void tabMove(int position, int positionOffsetPixels) {
        mPagerProgressBarMoveLength = (int) ((int) (mOneTabWidth * position)
                + (mOneTabWidth - mLayoutParams.width) / 2 + (((double) positionOffsetPixels / viewPagerW) * mOneTabWidth));
        mLayoutParams.leftMargin = mPagerProgressBarMoveLength;
        mPagerProgressBar.setLayoutParams(mLayoutParams);
    }


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