我试图用编程的方式制作不同梯度的按钮。我使用ShapeDrawable,它的功能就像一种魅力。
RoundRectShape rs = new RoundRectShape(new float[] { 12f, 12f, 12f, 12f, 12f, 12f, 12f, 12f }, null, null);
ShapeDrawable sd = new ShapeDrawable(rs);
ShapeDrawable.ShaderFactory sf = new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient lg = new LinearGradient(0, 0, 0, height,
new int[] {
Color.parseColor("#feccb1"),
Color.parseColor("#f17432"),
Color.parseColor("#e86320"),
Color.parseColor("#f96e22") },
new float[] {
0, 0.50f, 0.50f, 1 },
Shader.TileMode.REPEAT);
return lg;
}
};
sd.setShaderFactory(sf);
myBtn.setBackgroundDrawable(sd);不过,我想在按钮中添加一个阴影,而不是以编程方式添加按钮文本。任何帮助都将不胜感激。
发布于 2012-12-26 19:21:01
然而,我想添加一个阴影到按钮,而不是按钮文本编程。
我想你想要的阴影背后的电流,你建立了。如果是,那么与另一个将充当阴影的LayerDrawable (放在第一个位置)一起创建一个Drawable:
RoundRectShape rss = new RoundRectShape(new float[] { 12f, 12f, 12f,
12f, 12f, 12f, 12f, 12f }, null, null);
ShapeDrawable sds = new ShapeDrawable(rss);
sds.setShaderFactory(new ShapeDrawable.ShaderFactory() {
@Override
public Shader resize(int width, int height) {
LinearGradient lg = new LinearGradient(0, 0, 0, height,
new int[] { Color.parseColor("#e5e5e5"),
Color.parseColor("#e5e5e5"),
Color.parseColor("#e5e5e5"),
Color.parseColor("#e5e5e5") }, new float[] { 0,
0.50f, 0.50f, 1 }, Shader.TileMode.REPEAT);
return lg;
}
});
LayerDrawable ld = new LayerDrawable(new Drawable[] { sds, sd });
ld.setLayerInset(0, 5, 5, 0, 0); // inset the shadow so it doesn't start right at the left/top
ld.setLayerInset(1, 0, 0, 5, 5); // inset the top drawable so we can leave a bit of space for the shadow to use
b.setBackgroundDrawable(ld);发布于 2017-06-16 16:37:13
一个更干净的方法是使用Paint.setShadowLayer,就像在Jack's answer中一样。
https://stackoverflow.com/questions/14042251
复制相似问题