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!?
);
有人知道该怎么做吗
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我有完全相同的问题。
我设法通过扩展 TextView 并重写 onDraw 方法来修复它。
它看起来是这样的
但是,如果您想在渐变中使用具有透明度的颜色,则此方法可能不起作用。
I had exactly the same problem.
I managed to fix it by extending TextView and overriding onDraw method.
Here is how it looks like
However this method probably won't work if you want to use colors with transparency in your gradient.
谢谢西登的回答。它帮助了我。
我添加这个答案是因为我找到了一种在渐变中使用具有透明度的颜色的方法。
所以,请先参考 sidon 的回答并为他的回答投票。
我在此处的“setShadowLayer”方法描述中找到了下面的内容。
因此,要点是 ShadowColor 必须不是不透明的,才能在渐变中使用具有透明度的颜色。
这是代码。
如果 ShadowColor 是不透明的,您可以通过将 alpha 减一来将其更改为不透明。
再次感谢sidon的回答。
2018-12-16 编辑:
如果你对颜色有相同的 alpha 值,下面的代码会更好。
因为如果阴影颜色不透明,则阴影的 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.
So, point is shadowColor must be not opaque to use colors with transparency in gradient.
Here is code.
If shadowColor is opaque, you may change it as not opaque by reduce the alpha by one.
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.
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: