【问题标题】:LinearGradient and transparent colors in TextViewTextView 中的 LinearGradient 和透明颜色
【发布时间】:2013-05-18 19:19:56
【问题描述】:

我使用Linear Gradient在TextView中设置渐变色,发现非常有趣的情况。要设置 TextView 渐变,我使用了以下代码:

int c1 = getResources().getColor(R.color.test1);
int c2 = getResources().getColor(R.color.test2);
Shader shader = new LinearGradient(0, 0, 0, status.getTextSize(),
                                   new int[]{c1, c2},
                                   new float[]{0, 1}, Shader.TileMode.CLAMP);
status.getPaint().setShader(shader);

我玩了一点颜色

  1. 仅设置 c1=green 和 c2-transparent
    <color name="test1">#ff00ff00</color>
    <color name="test2">#000000ff</color>
    

  1. 仅设置 c2=blue 和 c1-transparent
    <color name="test1">#0000ff00</color>
    <color name="test2">#ff0000ff</color>
    

所以我在第二种情况下看不到透明。

有人能解释一下这个渐变生成器的本质吗?

更新: 感谢@Romain Guy 的回答。我更多地使用渐变,几乎得到了我想要的:

    Shader shader = new LinearGradient(0, 0, 0, status.getTextSize(),
            new int[]{c1, c2, colour, colour},
            new float[]{0.2f, 0.7f, 0.699f, 1}, Shader.TileMode.CLAMP);

和status.setIncludeFontPadding();

真:

错误:

您可以看到纯色的中心边框和渐变色根据填充值移动。

我们可以计算渐变边框的浮点值来准确获取 TextView 中字符的中心吗? 我认为这将取决于 FontType。

【问题讨论】:

    标签: android textview transparency gradient


    【解决方案1】:

    半透明效果就在那里。问题是你从文本上方的 0 开始渐变。您可能应该考虑到 TextView 的填充和/或您正在使用的字体的 FontMetrics。

    【讨论】:

    • 谢谢,这真的让我对这里发生的事情有了更多的了解。你知道如何获取字体填充并重新计算它以获取 TextView 中的 exectli 起始像素和字符中心吗?
    猜你喜欢
    • 2015-12-04
    • 2013-09-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2022-11-03
    • 1970-01-01
    • 2017-09-23
    • 1970-01-01
    相关资源
    最近更新 更多