代码之家  ›  专栏  ›  技术社区  ›  BlackBlind

在textview中实用地更改渐变颜色?

  •  0
  • BlackBlind  · 技术社区  · 7 年前

    我有一个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));
        }
    }
    

    }

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

    我想改变梯度颜色DINAMICAL。

    用程序改变颜色的最好方法是什么?

    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();
    }
    

    }

    代码

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

    错误

    Here you can find the error which

    0 回复  |  直到 7 年前
        1
  •  3
  •   Nicola Gallazzi    7 年前

    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();
        }
    
    }
    

        2
  •  0
  •   Shivam Yadav    7 年前

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