Sunday, 21 April 2013

Reflection Photo Effect for android



Please use below class and pass the bitmap to this function and get return bitmap with Reflection effect.



public class NewReflection {

      public static final Bitmap applyReflection(Bitmap bitmap) {
            // gap space between original and reflected
            final int reflectionGap = 4;
            // get image size
            int width = bitmap.getWidth();
            int height = bitmap.getHeight();

            // this will not scale but will flip on the Y axis
            Matrix matrix = new Matrix();
            matrix.preScale(1, -1);

            // create a Bitmap with the flip matrix applied to it.
            // we only want the bottom half of the image
            Bitmap reflectionImage = Bitmap.createBitmap(bitmap, 0, height / 2,
                        width, height / 2, matrix, false);

            // create a new bitmap with same width but taller to fit reflection
            Bitmap bitmapWithReflection = Bitmap.createBitmap(width,
                        (height + height / 2), Config.ARGB_8888);

            // create a new Canvas with the bitmap that's big enough for
            // the image plus gap plus reflection
            Canvas canvas = new Canvas(bitmapWithReflection);
            // draw in the original image
            canvas.drawBitmap(bitmap, 0, 0, null);
            // draw in the gap
            Paint defaultPaint = new Paint();
            canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint);
            // draw in the reflection
            canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null);

            // create a shader that is a linear gradient that covers the reflection
            Paint paint = new Paint();
            LinearGradient shader = new LinearGradient(0, bitmap.getHeight(), 0,
                        bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff,
                        0x00ffffff, TileMode.CLAMP);
            // set the paint to use this shader (linear gradient)
            paint.setShader(shader);
            // set the Transfer mode to be porter duff and destination in
            paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
            // draw a rectangle using the paint with our linear gradient
            canvas.drawRect(0, height, width, bitmapWithReflection.getHeight()
                        + reflectionGap, paint);

            return bitmapWithReflection;
      }

}








By: NovaRadix Technology.



No comments:

Post a Comment