Android 太阳出来了
程序员文章站
2022-07-01 18:26:45
public class ShimmerTextView extends View { private Paint mPaint; /** * 位移的变化 */ private int mDx; private LinearGradient mLinearGradient; private Path path; public ShimmerTextView(Context context) { this(c......
public class ShimmerTextView extends View {
private Paint mPaint;
/**
* 位移的变化
*/
private int mDx;
private LinearGradient mLinearGradient;
private Path path;
public ShimmerTextView(Context context) {
this(context, null);
}
public ShimmerTextView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public ShimmerTextView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mPaint = new Paint();
// mPaint.setAntiAlias(true);
// mPaint.setDither(true);
path = new Path();
path.addCircle(400, 400,200 , Path.Direction.CW);
PathMeasure mPathMeasure = new PathMeasure(path, false);
int length = (int) mPathMeasure.getLength();
createAnim(length);
creatLinearGradient(length);
}
/**
* 着色
*
* @param mLength
*/
private void creatLinearGradient(int mLength) {
// 需要注意的 : 开始的颜色 和结束的颜色 最好是一个颜色
int[] colors = {0xffff0000, 0xff00ff00, 0xffffff00, 0xffffffff, 0xffff0000};
float[] pos = {0.0f, 0.25f, 0.5f, 0.75f, 1.0f};
mLinearGradient = new LinearGradient(-mLength, 0, 0, 0, colors, pos, Shader.TileMode.CLAMP);
}
/**
* 绘制路径 文字长度 动画
*
* @param mLength
*/
private void createAnim(int mLength) {
ValueAnimator mValueAnimator = ValueAnimator.ofInt(0, mLength * 2);
mValueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
mDx = (int) animation.getAnimatedValue();
postInvalidate();
}
});
mValueAnimator.setRepeatMode(ValueAnimator.RESTART);
mValueAnimator.setRepeatCount(ValueAnimator.INFINITE);
mValueAnimator.setDuration(2000);
mValueAnimator.start();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Matrix mMatrix = new Matrix();
mMatrix.setTranslate(mDx, 0);
mLinearGradient.setLocalMatrix(mMatrix);
mPaint.setShader(mLinearGradient);
canvas.drawPath(path, mPaint);
}
}
本文地址:https://blog.csdn.net/FlyPig_Vip/article/details/107365354