TextView 添加渐变和阴影

发布于 2024-12-03 11:05:53 字数 755 浏览 2 评论 0原文

我有一个问题。我想要一个带有渐变颜色的文本视图。背后还有一个黑色的影子。问题是阴影使用渐变的颜色而不是使用调用的颜色(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!?
              );

有人知道该怎么做吗

I have a problem. I would like to have a textview with a gradient as color. And a black shadow behind it. The problem is that the shadow is using the color of the gradient in stead of using the called color (Color.BLACK)

My code is:
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!?
              );

Does anybody knows what to do

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

这样的小城市 2024-12-10 11:05:53

我有完全相同的问题。
我设法通过扩展 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);
}

但是,如果您想在渐变中使用具有透明度的颜色,则此方法可能不起作用。

I had exactly the same problem.
I managed to fix it by extending TextView and overriding onDraw method.
Here is how it looks like

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

However this method probably won't work if you want to use colors with transparency in your gradient.

如何视而不见 2024-12-10 11:05:53

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

我添加这个答案是因为我找到了一种在渐变中使用具有透明度的颜色的方法。
所以,请先参考 sidon 的回答并为他的回答投票。

我在此处的“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;

再次感谢sidon的回答。

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

输出:
输入图片此处描述

Thank you for sidon's answer. It helped me.

And I add this answer because I found a way to use colors with transparency in gradient.
So, please refer to sidon's answer first and upvote his one.

I found below at "setShadowLayer" Method Description from here.

The alpha of the shadow will be the paint's alpha if the shadow color is opaque, or the alpha from the shadow color if not.

So, point is shadowColor must be not opaque to use colors with transparency in gradient.

Here is code.

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

If shadowColor is opaque, you may change it as not opaque by reduce the alpha by one.

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

Thank you once again for sidon's answer.

2018-12-16 Edit:

If you have the same alpha value about colors, below code would better.

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

Because the alpha of the shadow will be the paint's alpha if the shadow color is opaque. And the alpha of the gradient colors is the paint's alpha if the alpha of the gradient colors are ff(opaque).

(Or the final alpha of text can be scaled again by the gradient color's alpha value provided that the paint's alpha is ff.)

Output:
enter image description here

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文