首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >TextView添加渐变和阴影

TextView添加渐变和阴影
EN

Stack Overflow用户
提问于 2011-09-07 16:31:58
回答 1查看 6.6K关注 0票数 19

我有个问题。我想有一个文本视图与渐变的颜色。后面还有一个黑色的阴影。问题是阴影使用的是渐变的颜色,而不是调用的颜色(Color.BLACK)

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

代码语言:javascript
复制
    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!?
              );

有人知道该怎么做吗?

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-09-21 16:34:20

我也有同样的问题。我设法通过扩展TextView和覆盖onDraw方法来修复它。下面是它看起来的样子

代码语言:javascript
复制
@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);
}

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

票数 20
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/7330941

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档