我有问题。我想要一个带有渐变颜色的 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!?
);
有人知道该怎么做吗
最佳答案
我遇到了完全相同的问题。 我设法通过扩展 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);
}
但是,如果您想在渐变中使用具有透明度的颜色,此方法可能不起作用。
关于android - TextView 添加渐变和阴影,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7330941/