Shadow effect for image in all sides

932 views Asked by At

I need to create shadow effect for an image .

private static Bitmap getDropShadow3(Bitmap bitmap) {

if (bitmap==null) return null;
int think = 6;
int w = bitmap.getWidth();
int h = bitmap.getHeight();

int newW = w - (think);
int newH = h - (think);

Bitmap.Config conf = Bitmap.Config.ARGB_8888;
Bitmap bmp = Bitmap.createBitmap(w, h, conf);
Bitmap sbmp = Bitmap.createScaledBitmap(bitmap, newW, newH, false);

Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
Canvas c = new Canvas(bmp);

// Right
Shader rshader = new LinearGradient(newW, 0, w, 0, Color.GRAY, Color.LTGRAY, Shader.TileMode.CLAMP);
paint.setShader(rshader);
c.drawRect(newW, think, w, newH, paint);

// Bottom
Shader bshader = new LinearGradient(0, newH, 0, h, Color.GRAY, Color.LTGRAY, Shader.TileMode.CLAMP);
paint.setShader(bshader);
c.drawRect(think, newH, newW  , h, paint);

//Corner
Shader cchader = new LinearGradient(0, newH, 0, h, Color.LTGRAY,    Color.LTGRAY, Shader.TileMode.CLAMP);
paint.setShader(cchader);
c.drawRect(newW, newH, w  , h, paint);
c.drawBitmap(sbmp, 0, 0, null);
return bmp;
}

i used the above code, and i get two sides (Right, bottom) shadow effect . How can i make the effect in all sides including (top,Left)?

2

There are 2 answers

0
A.S On

the method that you are using; may cause some problems with a certain type of images (irregular ones)

try this method instead (much much easier to understand and more flexible):

public Bitmap addShadowToBitmap(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;
}

call it through:

addShadowToBitmap(arg0, arg1, arg2, arg3, arg4, arg5, arg6);

and it will return you a bitmap.

hope this will help.

0
Fracdroid On

Ahmad's code was not quite right for me. I augmented his code to determine w/h of returned bitmap based on the dx/dy and shadow size. This code worked for me with a bitmap that was a signature of a user.

enter image description here


            public Bitmap addShadowToBitmap(final Bitmap bm, int color, int size, int dx, int dy) {

                int dstWidth = bm.getWidth() + dx + size/2;
                int dstHeight = bm.getHeight() + dy + size/2;

                final Bitmap mask = Bitmap.createBitmap(dstWidth, dstHeight, Bitmap.Config.ALPHA_8);

                final Canvas maskCanvas = new Canvas(mask);
                final Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);
                maskCanvas.drawBitmap(bm, 0, 0, paint);
                paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.SRC_OUT));
                maskCanvas.drawBitmap(bm, dx, dy, paint);

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

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