Search code examples
androidimageviewshadow

Android image view bottom shadow


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

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


Solution

  • 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;
    } ... }