霓虹灯文字.gif
首先自定义一个控件继承TextView,使用LinearGradient进行渲染
代码语言:javascript复制 @Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
String text = getText().toString();
textMaxWidth = getPaint().measureText(text);
float onceWidth = textMaxWidth / text.length() * gradientCount;
linearGradient = new LinearGradient(onceWidth, 0, 0, 0, new int[]{getCurrentTextColor(), 0xff000000, getCurrentTextColor()},
null, Shader.TileMode.CLAMP);
getPaint().setShader(linearGradient);
}
效果:
LinearGradient.jpg
实现动起来的效果,在onDraw中处理
代码语言:javascript复制 @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
postix = FACTOR;
if (postix > textMaxWidth || postix < -1) {
FACTOR = -FACTOR;
}
matrix.setTranslate(postix, 0);
linearGradient.setLocalMatrix(matrix);
postInvalidateDelayed(50);
}