0

How can i make recycle view item like this(shadow) in android?

https://dribbble.com/shots/4020189-Scroll-and-sidebar-interaction

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30

1 Answers1

0

For adding shadow, you can use a code like this:

Paint forShadow = new Paint(); 

// radius=10, y-offset=2, color=black 

forShadow.setShadowLayer(10.0f, 0.0f, 2.0f, 0xFF000000); 

// in onDraw(Canvas) method 

canvas.drawBitmap(bitmap, 0.0f, 0.0f, forShadow);

Also for Honeycomb and above you need to invoke setLayerType(LAYER_TYPE_SOFTWARE, forShadow), otherwise you will not see your shadow!

SetShadowLayer does not work with hardware acceleration unfortunately so it greatly reduces performances.

Full code may look something like this in brief:

public class ShadowImage extends Drawable {

Bitmap bm;

@Override
public void draw(Canvas canvas) {

    Paint forShadow = new Paint();
    //trying for rectangle
    Rect rect = new Rect(0,0,bm.getWidth(), bm.getHeight());

    forShadow.setAntiAlias(true);
    forShadow.setShadowLayer(5.5f, 4.0f, 4.0f, Color.BLACK);

    canvas.drawRect(rect, forShadow);
    canvas.drawBitmap(bm, 0.0f, 0.0f, null);

}

public ShadowImage(Bitmap bitmap) {
    super();
    this.bm = bitmap;
} ... }
PradyumanDixit
  • 2,372
  • 2
  • 12
  • 20