0

I want to add a shadow to an ImageView (right and bottom side).
Below I posted 2 images with a shadow effect.
I want the same shadow effect.

How can I apply this shadow effect to my ImageView?

Image with right side shadow
enter image description here

image with right and bottom shadow
enter image description here

Phantômaxx
  • 37,901
  • 21
  • 84
  • 115
MurugananthamS
  • 2,395
  • 4
  • 20
  • 49

2 Answers2

6

Set padding size Right and bottom and background set your shadow

<ImageView
    android:id="@+id/imageview"
    android:background="@drawable/drop_shadow"
    <!--android:background="#660000"--> This breaks the syntax highlight
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"        
    android:paddingRight="10px"
    android:paddingBottom="10px"
    android:src="@drawable/pic1"
/>
sasikumar
  • 12,540
  • 3
  • 28
  • 48
5

Try to use Bitmap

    public Bitmap imgshadow(final Bitmap bm, final int dstHeight, final int dstWidth, int color, int size, float dx, float dy) {
        final Bitmap mask = Bitmap.createBitmap(dstWidth, dstHeight, Config.ALPHA_8);

        final Matrix scaleToFit = new Matrix();
        final RectF src = new RectF(0, 0, bm.getWidth(), bm.getHeight());
        final RectF dst = new RectF(0, 0, dstWidth - dx, dstHeight - dy);
        scaleToFit.setRectToRect(src, dst, ScaleToFit.CENTER);

        final Matrix dropShadow = new Matrix(scaleToFit);
        dropShadow.postTranslate(dx, dy);

        final Canvas maskCanvas = new Canvas(mask);
        final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
        maskCanvas.drawBitmap(bm, scaleToFit, paint);
        paint.setXfermode(new PorterDuffXfermode(Mode.SRC_OUT));
        maskCanvas.drawBitmap(bm, dropShadow, paint);

        final BlurMaskFilter filter = new BlurMaskFilter(size, Blur.NORMAL);
        paint.reset();
        paint.setAntiAlias(true);
        paint.setColor(color);
        paint.setMaskFilter(filter);
        paint.setFilterBitmap(true);

        final Bitmap ret = Bitmap.createBitmap(dstWidth, dstHeight, Config.ARGB_8888);
        final Canvas retCanvas = new Canvas(ret);
        retCanvas.drawBitmap(mask, 0,  0, paint);
        retCanvas.drawBitmap(bm, scaleToFit, null);
        mask.recycle();
        return ret;
    }



 final Bitmap src = BitmapFactory.decodeResource(getResources(), R.drawable.ic_launcher);
    final Bitmap shadows = imgshadow(src, src.getHeight(), src.getWidth(), Color.BLACK, 3, 1, 3);
    final ImageView iv = (ImageView)findViewById(R.id.imageview1);
    iv.setImageBitmap(shadows);
Aditya Vyas-Lakhan
  • 13,409
  • 16
  • 61
  • 96