效果图如下:
一、自定义属性:
1.1、字体要变的颜色
1.2、字体不变的颜色
二、继承TextView
2.1、初始化画笔 两个字体画笔 :变色与不变色
2.2、onDraw() : 通过两个画笔(paint)画出同一位置的两次文字,
* 通过canvas.clipRect() (画布的裁剪方法),裁剪出要显示文字的指定区域,
* 通过裁剪方法、与Paint(画笔)定义的颜色,可以实现同一文字不同颜色效果显示
三 、实现文字变色的朝向 :左到右、右到左
四、使用
与viewPager 的监听方法结合,通过加减偏移量 算出字体要显示的部分区域,达到要实现的效果
实现
1、自定义属性:
没有attrs.xml 文件就新建一个
attrs.xml
2、新建 ColorTextView_02 类,继承TextView
@SuppressLint("AppCompatCustomView")
public class ColorTextView_02 extends TextView {
//不变色字体的画笔
private Paint mOriginPaint;
//改变颜色字体的画笔
private Paint mChangePaint;
private float mCurrentProgress = 0f;//中心值
private Direction mDirection=Direction.LEFT_TO_RIGHT; //方向
/**
* 实现思路
* 1、创建自定义属性:1.1、字体要变的颜色 1.2、字体不变的颜色
* 2、自定义TextView ,继承相关的属性 :获取文字的大小 、颜色
* 3、初始化画笔 两个字体画笔 :变色与不变色
* 4、绘制 :通过两个画笔(paint)画出同一位置的两次文字,
* 通过canvas.clipRect() (画布的裁剪方法),裁剪出要显示文字的指定区域,
* 通过裁剪方法、与Paint(画笔)定义的颜色,可以实现同一文字不同颜色效果显示,
* 5、实现文字变色的朝向 :左到右、右到左
* 6、与viewPager 的监听方法结合,通过加减偏移量 算出字体要显示的部分区域,达到要实现的效果
*/
/**
* 左到右变色
* 右到左变色
*/
public enum Direction{
LEFT_TO_RIGHT,RIGHT_TO_LEFT
}
public ColorTextView_02(Context context) {
this(context, null);
}
public ColorTextView_02(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public ColorTextView_02(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initPaint(context, attrs);
}
/**
* 初始化画笔
*
* @param context
* @param attrs
*/
private void initPaint(Context context, AttributeSet attrs) {
//自定义属性
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.colorTextView);
int originColor = array.getColor(R.styleable.colorTextView_originColor, getTextColors().getDefaultColor());
int changeColor = array.getColor(R.styleable.colorTextView_changeColor, getTextColors().getDefaultColor());
mOriginPaint = getPaintByColor(originColor);
mChangePaint = getPaintByColor(changeColor);
}
/**
* 根据颜色获取画笔
*/
private Paint getPaintByColor(int color) {
Paint paint = new Paint();
paint.setColor(color);
paint.setAntiAlias(true);
paint.setDither(true);
paint.setTextSize(getTextSize());
return paint;
}
@Override
protected void onDraw(Canvas canvas) {
//中心值
int middle = (int) (mCurrentProgress * getWidth());
if (mDirection==Direction.LEFT_TO_RIGHT){
//不变色的字体
drawText(canvas,mChangePaint , 0, middle);
//变色的字体
drawText(canvas,mOriginPaint , middle, getWidth());
}else {
//不变色的字体
drawText(canvas, mChangePaint, getWidth()-middle, getWidth());
//变色的字体
drawText(canvas, mOriginPaint, 0, getWidth()-middle);
}
}
/**
* 绘制Text
*
* @param canvas
* @param paint
* @param start
* @param end
*/
public void drawText(Canvas canvas, Paint paint, int start, int end) {
canvas.save(); //保存
String text = getText().toString();//文字
//裁剪
Rect rect = new Rect(start, 0, end, getHeight());
canvas.clipRect(rect);
//获取字体的宽度
Rect bounds = new Rect();
paint.getTextBounds(text, 0, text.length(), bounds);
//获取字体横线中心的位置
int x = getWidth() / 2 - bounds.width() / 2;
//基线baseLine
Paint.FontMetricsInt fontMetricsInt = paint.getFontMetricsInt();
int dy = (fontMetricsInt.bottom - fontMetricsInt.top) / 2 - fontMetricsInt.bottom;//基线到底部的距离
int baseLine = getHeight() / 2 + dy; //获取基线
//绘制文字
canvas.drawText(text, x, baseLine, paint);
canvas.restore();
}
//进度
public void setCurrentProgress(float progress) {
mCurrentProgress = progress;
invalidate();
}
//朝向
public void setDirection(Direction direction){
this.mDirection=direction;
}
//设置不变色的字体
public void setOriginColor(int originColor){
this.mOriginPaint.setColor(originColor);
}
//设置要变色的字体
public void setChangeColor(int changeColor){
this.mChangePaint.setColor(changeColor);
}
}
3、使用
view_text_02.xml
fragment:
public class TextFragment_02 extends Fragment {
private Button toRight, toLeft;
private ColorTextView_02 colorView;
private static final String TAG = "TextFragment_02";
private ViewPager viewPager;
private List fragmentList;
private ViewPagerAdapter pagerAdapter;
private List colorTextViewList;
private LinearLayout colorView_linear;
private String[] item = {"段子", "视频", "漫画", "小说", "军事"};
@Nullable
@Override
public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
View view = inflater.inflate(R.layout.view_text_02, container, false);
init(view);
initIndicator();
setViewPager();
return view;
}
private void init(View view) {
colorView = view.findViewById(R.id.text_view);
toRight = view.findViewById(R.id.left_ToRight);
toLeft = view.findViewById(R.id.right_ToLeft);
viewPager = view.findViewById(R.id.viewPager);
colorView_linear = view.findViewById(R.id.colorView_linear);
colorTextViewList = new ArrayList<>();
fragmentList = new ArrayList<>();
click();
}
/**
* 初始化TabLayout
*/
private void initIndicator() {
for (int i = 0; i < item.length; i++) {
LinearLayout.LayoutParams params = new LinearLayout.LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
params.weight = 1;
ColorTextView_02 view = new ColorTextView_02(getContext());
view.setTextSize(20);
view.setChangeColor(Color.RED);
view.setText(item[i]);
view.setLayoutParams(params);
colorView_linear.addView(view);
colorTextViewList.add(view);
fragmentList.add(new PageFragment(item[i]));
}
}
/**
* 监听ViewPager的滑动
*/
private void setViewPager() {
FragmentManager fragmentPagerAdapter = getActivity().getSupportFragmentManager();
pagerAdapter = new ViewPagerAdapter(fragmentPagerAdapter);
viewPager.setAdapter(pagerAdapter);
viewPager.addOnPageChangeListener(new ViewPager.OnPageChangeListener() {
@Override
public void onPageScrolled(int position, float positionOffset, int positionOffsetPixels) {
//操作前面的字体
ColorTextView_02 view_02 = colorTextViewList.get(position);
view_02.setDirection(ColorTextView_02.Direction.RIGHT_TO_LEFT);
view_02.setCurrentProgress(1 - positionOffset);
//操作后面的字体
try {
ColorTextView_02 view = colorTextViewList.get(position+1);
view.setDirection(ColorTextView_02.Direction.LEFT_TO_RIGHT);
view.setCurrentProgress(positionOffset);
}catch (Exception ignored){
}
}
@Override
public void onPageSelected(int position) {
Log.d(TAG, "onPageSelected: " + position);
}
@Override
public void onPageScrollStateChanged(int state) {
Log.d(TAG, "onPageScrollStateChanged: " + state);
}
});
}
/**
* 点击事件
*/
private void click() {
toRight.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
leftToRight();
}
});
toLeft.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
rightToLeft();
}
});
}
//从左到右变色
private void leftToRight() {
colorView.setDirection(ColorTextView_02.Direction.LEFT_TO_RIGHT);
ValueAnimator animator = ObjectAnimator.ofFloat(0, 1);
animator.setDuration(2000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
Log.d(TAG, "onAnimationUpdate: " + (float) animation.getAnimatedValue());
colorView.setCurrentProgress((float) animation.getAnimatedValue());
}
});
animator.start();
}
//从右到左变色
private void rightToLeft() {
colorView.setDirection(ColorTextView_02.Direction.RIGHT_TO_LEFT);
ValueAnimator animator = ObjectAnimator.ofFloat(0, 1);
animator.setDuration(2000);
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
colorView.setCurrentProgress((float) animation.getAnimatedValue());
}
});
animator.start();
}
/**
* ViewPager 适配器
*/
class ViewPagerAdapter extends FragmentPagerAdapter {
public ViewPagerAdapter(@NonNull FragmentManager fm) {
super(fm);
}
@NonNull
@Override
public Fragment getItem(int position) {
return fragmentList.get(position);
}
@Override
public int getCount() {
return fragmentList.size();
}
}
}