android-自定义视图中的渐变文本颜色
作者:互联网
我有这个GradientTextView类,它扩展了AppCompatTextView
public class GradientTextView extends android.support.v7.widget.AppCompatTextView {
public GradientTextView(Context context) {
super(context);
}
public GradientTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//Setting the gradient if layout is changed
if (changed) {
getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(),
ContextCompat.getColor(getContext(), R.color.colorStart),//Red Color
ContextCompat.getColor(getContext(), R.color.colorEnd),// Blue Color
Shader.TileMode.CLAMP));
}
}
}
这是渐变textview的xml
<package.GradientTextView
android:id="@+id/textLogin"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:text="Login."
android:textStyle="bold"
android:padding="4dp"
android:layout_marginStart="8dp"/>
现在我有一个带有红色和蓝色渐变的textview.
我想动态更改渐变颜色.
以编程方式更改颜色的最佳方法是什么?
在回答@Nicola Gallazzi之后,我正在更新我的问题现在GradientTextView的新文件是
public class GradientTextView extends android.support.v7.widget.AppCompatTextView {
int colorStart = ContextCompat.getColor(getContext(), R.color.colorStart);
int colorEnd = ContextCompat.getColor(getContext(), R.color.colorEnd);
public GradientTextView(Context context) {
super(context);
}
public GradientTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GradientTextView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//Setting the gradient if layout is changed
if (changed) {
getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(), colorStart, colorEnd,
Shader.TileMode.CLAMP));
}
}
public void setGradientColors(int colorStart, int colorEnd) {
this.colorStart = colorStart;
this.colorEnd = colorEnd;
// this forces view redrawing
invalidate();
}
}
但是这是一个新问题,就是我在@Nicola Gallazzi这样的活动中使用它时显示错误.
码
textView.setGradientColors(ContextCompat.getColor(this,R.color.ncolorEnd, R.color.ncolorStart));
错误
解决方法:
您应该将colorStart和colorEnd定义为GradientTextView类的实例变量.
然后,在类中定义一个公共方法,以编程方式设置colorStart和colorEnd:
public class GradientTextView extends android.support.v7.widget.AppCompatTextView {
private int colorStart = ContextCompat.getColor(getContext(), R.color.colorPrimary);
private int colorEnd = ContextCompat.getColor(getContext(), R.color.colorAccent);
public GradientTextView(Context context) {
super(context);
}
public GradientTextView(Context context, AttributeSet attrs) {
super(context, attrs);
}
public GradientTextView(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);
}
@Override
protected void onLayout(boolean changed, int left, int top, int right, int bottom) {
super.onLayout(changed, left, top, right, bottom);
//Setting the gradient if layout is changed
if (changed) {
getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(), colorStart, colorEnd,
Shader.TileMode.CLAMP));
}
}
public void setGradientColors(int colorStart, int colorEnd) {
this.colorStart = colorStart;
this.colorEnd = colorEnd;
// this forces view redrawing
invalidate();
}
}
标签:gradient,android-custom-view,android,textview 来源: https://codeday.me/bug/20191211/2105645.html