说明
1、起初网上搜了个控件PagerSlidingTabStrip 发现已经很久不更新了,点击tab时候没有动画,不能自己单独使用,自己重写了他的功能即可以配合ViewPager亦可以单独使用
2、内部逻辑重新更改
实现效果
实现代码
PagerSlidingTabStrip.java
import android.animation.ValueAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.support.v4.view.ViewPager;
import android.util.AttributeSet;
import android.util.DisplayMetrics;
import android.util.TypedValue;
import android.view.Gravity;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
import android.widget.TextView;
/**
* Copyright (C), 2019, flyzhang
* Author: flyzhang
* Date: 2019/9/19 16:30
* Description:
* <p>
* </p>
* History:
* <author> <time> <version> <desc>
* 作者姓名 修改时间 版本号 描述
*/
public class PagerSlidingTabStrip extends LinearLayout {
private LinearLayout mTabsContainer;
private ViewPager mViewPager = null;
private Context mContext;
private float mLastPosition = 0;//上次点击
private onTabClickListener mOnTabClickListener;//tab 点击回调
private boolean mSmoothScroll = false;//点击tab viewpager滚动
private Paint mIndicatorPaint, mUnderLinePaint;//下划线 最下面线
private int mIndicatorHeight = 2;//dp 底部滚动线高度
private int mUnderLineWidth = 21;//dp 底部滚动线宽度
private float mCurrentPosition = 0;//当前下标
private float mImageOffset = 2.5f;//dp 减去小红点位置
private boolean mAutoHideBadge = true;//默认自动滚到位置隐藏
private int mSelectTabTextColor = 0xFF07AF39;//tab 选中字体颜色
private int mUnSelectTabTextColor = 0xFF080808;//tab 未选中字体颜色
private int mIndicatorColor = 0xFF1FB84D;//下划线color
private int mUnderIndicatorRound = 2;
private int mUnderLineHeight = 2;//下划线高度
private int mUnderLineColor = 0xFFEFEFEF;//下划线颜色
private int mUnderShadowColor = 0xFFEFEFEF;
private int mUnderRadius = 5;//dp
private int mUnderDx = 0;//dp
private int mUnderDy = 2;//dp
private int mTabTextSize = 14;//tab 字体大小
private int SCREEN_HEIGHT = 0, SCREEN_WIDTH = 0;//屏幕宽高
private int mTabWidth = 0;//每个tab宽
private int mLayoutWeight = 0; //tab 样式 当weight == 1是默认评分屏幕
public PagerSlidingTabStrip(Context context) {
super(context);
initView(context);
}
public PagerSlidingTabStrip(Context context, AttributeSet attrs) {
super(context, attrs);
initView(context);
}
private void initView(Context context) {
setWillNotDraw(false);
mContext = context;
DisplayMetrics dm = getResources().getDisplayMetrics();
mUnderLineWidth = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mUnderLineWidth, dm);
mImageOffset = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mImageOffset, dm);
mIndicatorHeight = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mIndicatorHeight, dm);
mUnderRadius = (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, mUnderRadius, dm);
mUnderDx = (int) TypedValue.applyDimension(TypedValue.C