纯纯的一边开发一边学习,是小白是菜鸟,单纯的记录和学习,大神勿喷,理解有错望指正~
定义颜色 输出为int的类型
// Color类的常量
int color = Color.BLUE;
// 通过Color转换RGB色值
int color = Color.parseColor("#6bbbec");
// 其中第一个参数表示透明,0表示完全透明,255(ff)表示完全不透明;后三位分别代表RGB的值了。
int color = Color.argb(255, 33, 106, 255);
// 必须使用0x开头,值也必须用8位表示。
// 分组一下0x|ff|ff00ff,0x是代表颜色整数的标记,ff是表示透明度,ff00ff表示RGB颜色值。
int color = 0xff00ff00;
实现颜色渐变的关键
// LinearGradient用于实现线性渐变的效果
public LinearGradient(float x0, float y0, float x1, float y1,
int color0, int color1, @NonNull Shader.TileMode tile) {
}
参数说明
(x0, y0):渐变起始点坐标
(x1, y1):渐变结束点坐标
color0:渐变起始颜色
color1:渐变终止颜色
tile:填充模式
CLAMP:边缘拉伸。使用边缘颜色对区域外的范围进行填充
REPEAT:重复模式。在水平和垂直两个方向上重复填充
MIRROR:镜像模式。在水平和垂直两个方向上以镜像的方式重复填充,相邻图像间有间隙
实践代码
private TextView mText;
// 在初始化的时候定给TextView赋颜色
public void initTextView(){
mText.findViewById(R.id.text_test);
// 在这里可以采用不同的方法来定义颜色
int startColor = Color.argb(255, 127, 241, 255);
int endColor = Color.argb(255, 33, 106, 255);
LinearGradient linearGradient = new LinearGradient(0, 0, 0, mSpeed.getLineHeight(),
startColor, endColor, Shader.TileMode.CLAMP);
mSpeed.getPaint().setShader(linearGradient);
mSpeed.invalidate();
}
参考链接: