TextView添加渐变和阴影

时间:2011-09-07 08:31:58

标签: android colors textview gradient shadow

我有问题。我想要一个带有渐变颜色的textview。它背后有一个黑色的影子。问题是阴影使用渐变的颜色而不是使用被调用的颜色(Color.BLACK

我的代码是: numberTextView = (TextView)findViewById(R.id.something);

    Shader textShaderTop = new LinearGradient(0, 30, 0, 60,
                new int[]{Color.parseColor("#A6A6A6"), Color.parseColor("#E8E8E8"), Color.parseColor("#A6A6A6")},
                new float[]{0, 0.5f, 1}, TileMode.CLAMP);
    numberTextView.getPaint().setShader(textShaderTop);

    numberTextView.setShadowLayer(
              0.1f,   //float radius
              20f,  //float dx
              20f,  //float dy 
              Color.BLACK //this is not black on the screen, but it uses the gradient color!?
              );

有人知道该怎么做

2 个答案:

答案 0 :(得分:18)

我有完全相同的问题。 我设法通过扩展TextView和重写onDraw方法来修复它。 这是它的样子

@Override
protected void onDraw(Canvas canvas) {
    // draw the shadow
    getPaint().setShadowLayer(1, 1, 1, 0xbf000000); // or whatever shadow you use
    getPaint().setShader(null);
    super.onDraw(canvas);

    // draw the gradient filled text
    getPaint().clearShadowLayer();
    getPaint().setShader(new LinearGradient(0, getHeight(), 0, 0, 0xffacacac, 0xffffffff, TileMode.CLAMP)); // or whatever gradient/shader you use
    super.onDraw(canvas);
}

但是,如果要在渐变中使用具有透明度的颜色,则此方法可能无效。

答案 1 :(得分:1)

谢谢西顿的回答。它帮助了我。

我添加了这个答案,因为我找到了一种在渐变中使用具有透明度的颜色的方法。 因此,请先参考sidon的答案,然后对他的答案进行投票。

我在下面的here的“ setShadowLayer”方法说明中找到了

  

如果阴影颜色是不透明的,则阴影的Alpha将为绘画的Alpha;否则,则为阴影颜色的Alpha。

因此,要点是shadowColor必须不透明才能使用渐变透​​明的颜色。

这是代码。

@Override
protected void onDraw(Canvas canvas) {
    // draw the shadow
    getPaint().setShader(null);
    setTextColor(0x00ffffff); // set the paint's alpha by 00
    getPaint().setShadowLayer(3.0f, 1.5f, 1.8f, shadowColor); // shadowColor must be not opaque
    super.onDraw(canvas);

    // draw the gradient filled text
    getPaint().clearShadowLayer();
    setTextColor(0xffffffff); // set the paint's alpha by ff
    getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(), 0x7fff8809, 0x7f09ffff, Shader.TileMode.CLAMP)); // or whatever gradient/shader you use
    super.onDraw(canvas);
}

如果shadowColor是不透明的,则可以通过将alpha值减一来将其更改为不透明。

if((shadowColor >>> 24) == 0xff)
    shadowColor &= 0xfeffffff;

再次感谢您对西顿的回答。

2018-12-16编辑:

如果您对颜色的alpha值相同,则下面的代码会更好。

public class TextView_Gradient extends TextView {

    public TextView_Gradient(Context context) {
        super(context);
        setTextColor(0x3fffffff); // set the paint's alpha by 3f
    }

    @Override
    protected void onDraw(Canvas canvas) {
        // draw the shadow
        getPaint().setShader(null);
        // shadowColor must be opaque.
        getPaint().setShadowLayer(3.0f, 1.5f, 1.8f, shadowColor);
        super.onDraw(canvas);

        // draw the gradient filled text
        getPaint().clearShadowLayer();
        // gradient colors must be opaque, too.
        getPaint().setShader(new LinearGradient(0, 0, getWidth(), getHeight(), 0xffff8809, 0xff09ffff, Shader.TileMode.CLAMP));
        super.onDraw(canvas);
    }
}

因为如果阴影颜色是不透明的,阴影的Alpha将是绘画的Alpha。如果渐变颜色的Alpha为ff(不透明),则渐变颜色的Alpha即为绘画的Alpha。

(或者,如果绘画的alpha为ff,则可以通过渐变颜色的alpha值再次缩放文本的最终alpha。)

输出: enter image description here

相关问题