Add shadow in ShapeDrawable software

I'm trying to program the buttons with different gradients. I use ShapeDrawable and works like a charm.

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); 

However, I would like to add a drop shadow to a button, no text softkey. Any help would be appreciated.

+4
source share
2 answers

However, I would like to add a drop shadow to a button, rather than the text of the soft button.

I assume that you want to shadow you, that you have built. If yes, do LayerDrawable with other Drawable (placed first), which will act as a shadow:

  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); [] {12f, 12f, 12f,  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); 12f}, null, null);  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); ;  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); height) {  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); # e5e5e5"),  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); , new float [] {  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); [] {sds, sd});  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); start right at the left / top  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); leave a bit of space for the shadow to use  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); 
+7
source

A cleaner way to do this - use Paint.setShadowLayer , in Jack's response

-1
source

All Articles