Android-DIY-ShimmerTextView

Android

Android自定义控件:闪烁文字效果


源码传送门


效果展示

ShimmerTextView

讲解

使用线性渲染LinearGradient设置画笔的着色器Shader,Matrix用于对图像的图形处理,然后不停绘制文本。

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh)
{
super.onSizeChanged(w, h, oldw, oldh);
if (mViewWidth == 0)
{
mViewWidth = getMeasuredWidth();
if (mViewWidth > 0)
{
mPaint = getPaint();
// 设置线性渲染
mLinearGradient = new LinearGradient(-mViewWidth, 0, 0, 0, new int[] { 0x59ffffff, 0xffffffff, 0x59ffffff }, new float[] { 0, 0.5f, 1 }, Shader.TileMode.CLAMP);
// 设置Paint着色器
mPaint.setShader(mLinearGradient);
mGradientMatrix = new Matrix();
}
}
}
@Override
protected void onDraw(Canvas canvas)
{
super.onDraw(canvas);
if (mGradientMatrix != null)
{
mTranslate += mViewWidth / 10;
if (mTranslate > 2 * mViewWidth)
{
mTranslate = -mViewWidth;
}
mGradientMatrix.setTranslate(mTranslate, 0);
mLinearGradient.setLocalMatrix(mGradientMatrix);
postInvalidateDelayed(50);
}
}
谢谢老板,请尽情用红包来蹂躏我吧!!!
0%