【问题标题】:Gradient text color in a custom view自定义视图中的渐变文本颜色
【发布时间】:2019-03-18 10:45:51
【问题描述】:

我有这个扩展 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));
    }
}

}

这里是渐变文本视图的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"/>

现在我有一个带有红色和蓝色渐变的文本视图。 我想动态地改变渐变颜色。

以编程方式更改颜色的最佳方法是什么?

@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));

错误

【问题讨论】:

    标签: android textview gradient android-custom-view


    【解决方案1】:

    您应该将 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();
        }
    
    }
    

    【讨论】:

    • 兄弟我对 Paint Instance 有一个疑问。什么是 R.color.startColor?
    • 你的意思是 R.color.colorStart?我从问题中复制并粘贴它
    • 还有一个疑问。如何以编程方式使用它?
    • 好的,根据我的问题,R.color.colorStart 是红色的参考,R.color.colorEnd 是 color.xml 文件中定义的蓝色参考
    • 现在我已经更新了我的答案。你可以去看看。
    【解决方案2】:

    您可以简单地从下面的 java 代码中创建渐变可绘制对象-

    GradientDrawable drawable = new GradientDrawable();
    drawable.setStroke(width, Color.RED);
    drawable.setCornerRadius(8);
    drawable.setColor(ContextCompat.getColor(context,R.color.colorRed));
    

    并将drawable设置为如下所示的imageview

        imageView.setBackgroundDrawable(drawable);
    

    【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2019-09-19
    • 2013-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-08-19
    相关资源
    最近更新 更多