参考:http://chiuki.github.io/android-shaders-filters/#/5
实现的效果如下:
主要使用自定义控件(TextView),颜色渐变处理(Shader)
代码如下:
public class RainbowTextView extends TextView {
public RainbowTextView(Context context) {
super(context);
}
public RainbowTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public RainbowTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
int[] rainbow = getRainbowColors();
Shader shader = new LinearGradient(0, 0, 0, w, rainbow, null, Shader.TileMode.MIRROR);
Matrix matrix = new Matrix();
matrix.setRotate(90);
shader.setLocalMatrix(matrix);
getPaint().setShader(shader);
}
private int[] getRainbowColors() {
return new int[] {
getResources().getColor(R.color.rainbow_red),
getResources().getColor(R.color.rainbow_yellow),
getResources().getColor(R.color.rainbow_green),
getResources().getColor(R.color.rainbow_blue),
getResources().getColor(R.color.rainbow_purple)
};
}
}