之前写过一个seleteTab,由于之前写的匆忙很多代码有点乱,现在更新一下
public class SlidingTab extends LinearLayout implements View.OnClickListener, ValueAnimator.AnimatorUpdateListener {
private GradientDrawable mIndicatorDrawable = new GradientDrawable();
private GradientDrawable mRectDrawable = new GradientDrawable();
private Context context;
private Paint linePaint;
private int tabWidth;
private List tabList = new ArrayList<>();
private IndicatorPoint indicatorPoint = new IndicatorPoint();
private int position;
private boolean isFirst = true;
private ValueAnimator valueAnimator;
public SlidingTab(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
initView(context, attrs);
}
private void initView(Context context, AttributeSet attrs) {
this.context = context;
setOrientation(HORIZONTAL);
setWillNotDraw(false);//重写onDraw方法,需要调用这个方法来清除flag
setClipChildren(false);
setClipToPadding(false);
TypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.selectTabView);
typedArray.recycle();
//动画
valueAnimator = ObjectAnimator.ofObject(new PointEvaluator(), startPoint, endPoint);
valueAnimator.addUpdateListener(this);
}
//填充tab (View的长度在代码中写死了)
public void setTabList(List tabs) {
tabList.clear();
tabList.addAll(tabs);
for (String tab : tabList) {
TextView textView = new TextView(context);
LayoutParams layoutParams = new LayoutParams(0, LinearLayout.LayoutParams.MATCH_PARENT, 1.0f);
textView.setGravity(Gravity.CENTER);
textView.setText(tab);
textView.setId(tabList.indexOf(tab));
textView.setOnClickListener(this);
addView(textView, tabList.indexOf(tab), layoutParams);
}
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(widthMeasureSpec, heightMeasureSpec);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//话外部的轮廓线
int width = getWidth();
int height = getHeight();
mRectDrawable.setStroke(2, Color.RED);
mRectDrawable.setCornerRadius(getHeight() / 2);
mRectDrawable.setBounds(0, 0, width, height);
mRectDrawable.draw(canvas);
//初始化指示器
if (isFirst) {
indicatorPoint.left = getChildAt(0).getLeft();
indicatorPoint.right = getChildAt(0).getRight();
((TextView) getChildAt(0)).setTextColor(Color.WHITE);
}
mIndicatorDrawable.setColor(Color.BLUE);
mIndicatorDrawable.setCornerRadius(getHeight() / 2 - 2);
mIndicatorDrawable.setBounds(indicatorPoint.left + 2, 2, indicatorPoint.right - 2, height - 2);
mIndicatorDrawable.draw(canvas);
}
@Override
public void onClick(View v) {
if (v.getId() == position) {
return;
}
isFirst = false;
//没有动画直接运行
/* View tabView = getChildAt(v.getId());
indicatorPoint.left = tabView.getLeft();
indicatorPoint.right = tabView.getRight();
position = v.getId();
invalidate();*/
//设置动画 期初对象
View startView = getChildAt(position);
startPoint.left = startView.getLeft();
startPoint.right = startView.getRight();
((TextView) startView).setTextColor(Color.BLACK);
View tabView = getChildAt(v.getId());
endPoint.left = tabView.getLeft();
endPoint.right = tabView.getRight();
((TextView) tabView).setTextColor(Color.WHITE);
valueAnimator.setObjectValues(startPoint, endPoint);
valueAnimator.setDuration(260);
valueAnimator.start();
position = v.getId();
}
@Override
public void onAnimationUpdate(ValueAnimator animation) {
IndicatorPoint animatedValue = (IndicatorPoint) animation.getAnimatedValue();
indicatorPoint.left = animatedValue.left;
indicatorPoint.right = animatedValue.right;
invalidate();
}
//自定义估值器
class PointEvaluator implements TypeEvaluator {
@Override
public IndicatorPoint evaluate(float fraction, IndicatorPoint startValue, IndicatorPoint endValue) {
float left = startValue.left + fraction * (endValue.left - startValue.left);
float right = startValue.right + fraction * (endValue.right - startValue.right);
IndicatorPoint point = new IndicatorPoint();
point.left = ((int) left);
point.right = ((int) right);
return point;
}
}
private IndicatorPoint startPoint = new IndicatorPoint();
private IndicatorPoint endPoint = new IndicatorPoint();
//创建动画执行的对象
private class IndicatorPoint {
public int left;
public int right;
}
}