14

I have drawn a rectangle with canvas and I wonder if there is any property or way of giving a small shadow.

protected void onDraw(Canvas canvas) {
    super.onDraw(canvas);
    Paint pincel1 = new Paint();
    pincel1.setColor(Color.rgb(151, 217, 69));
    RectF rectangle = new RectF(30, 20,200,100);
    canvas.drawRoundRect (rectangle, 6, 6, pincel1);
}

Thanks

1 Answers1

38

This question contained the following code :

    Paint mShadow = new Paint();  
// radius=10, y-offset=2, color=black  
mShadow.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000);  
// in onDraw(Canvas)  
canvas.drawBitmap(bitmap, 0.0f, 0.0f, mShadow); 

So customize it a bit to your needs and that will do the trick.

In your case just add pincel1.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000); to your code.

Community
  • 1
  • 1
eric.itzhak
  • 15,752
  • 26
  • 89
  • 142
  • Thanks for answering so quickly,not be as I add this code to my code, i edit the post and add my onDraw() method –  May 23 '12 at 10:01
  • 18
    Note that paint.setShadowLayer() does not work with hardware acceleration (see http://developer.android.com/guide/topics/graphics/hardware-accel.html#unsupported) – Thierry Roy Jan 24 '13 at 20:28
  • @Thierry-DimitriRoy What can you use in it's place instead of this then? It would seem as though you might have an answer and a new answer to this questions could be helpful for those using hardware acceleration. – Jay Snayder Jul 25 '14 at 16:14
  • 2
    @JaySnayder You have to use PNGs to simulate drop shadows. Note that the L preview add support for drop shadows with hardware acceleration. – Thierry Roy Aug 02 '14 at 10:48
  • I am able to draw shadow around text but it is very light.how to make shadow layer dark? – Muhammad Umair Shafique Dec 31 '15 at 06:13
  • this method is for text only. On some devices it fails – Evgenii Vorobei May 23 '19 at 15:42
  • with setShader I am applying gradient colors . so shadow also is changing to the gradient color, any solution for that? – Giteeka Sawlani May 06 '20 at 07:29