android - 自定义 View 中的渐变文本颜色

标签 android textview gradient android-custom-view

我有这个扩展 AppCompatTextView 的 GradientTextView 类

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 这样的 Activity 中使用它时会显示错误。

代码

        textView.setGradientColors(ContextCompat.getColor(this,R.color.ncolorEnd, R.color.ncolorStart));

错误

Here you can find the error which

最佳答案

您应该将 colorStart 和 colorEnd 定义为 GradientTextView 类的实例变量。 然后,在类中定义一个 public 方法,以编程方式设置 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();
    }

}

关于android - 自定义 View 中的渐变文本颜色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55219528/

相关文章:

android - 有没有一种简单的方法可以删除应用小部件中的文本?

java - 单击按钮时设置 Android TextView 的值

java - 全局变量让我为空

Android 库项目到混淆的 JAR

java - 如何从 android spinner 获取文本?

android - 设置 Canvas (TextView)的高度,以便渲染多行

Android - 视频重新启动或恢复

opengl - OpenGL 中的 Gradient "miter"在连接处显示接缝

optimization - 参数空间受限时如何运行梯度下降算法?

CSS3渐变渲染从透明到白色的问题