get Reflection With Origin Bitmap - Android Graphics

Android examples for Graphics:Bitmap Effect

Description

get Reflection With Origin Bitmap

Demo Code


//package com.java2s;

import android.graphics.Bitmap;

import android.graphics.Bitmap.Config;

import android.graphics.Canvas;

import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;

import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;

import android.graphics.Shader.TileMode;

public class Main {

    public static Bitmap getReflectionWithOriginBitmap(Bitmap src, int gap,
            boolean recycleSrc) {
        int width = src.getWidth();
        int height = src.getHeight();

        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);//from  w ww. j  av  a  2s .c  o m

        Bitmap reflectionImage = Bitmap.createBitmap(src, 0, height / 2,
                width, height / 2, matrix, false);
        Bitmap dst = Bitmap.createBitmap(width, (height + height / 2),
                Config.ARGB_8888);
        Canvas canvas = new Canvas(dst);
        canvas.drawBitmap(src, 0, 0, null);
        Paint deafalutPaint = new Paint();
        canvas.drawRect(0, height, width, height + gap, deafalutPaint);
        canvas.drawBitmap(reflectionImage, 0, height + gap, null);
        reflectionImage.recycle();
        Paint paint = new Paint();
        LinearGradient shader = new LinearGradient(0, src.getHeight(), 0,
                dst.getHeight() + gap, 0x70ffffff, 0x00ffffff,
                TileMode.CLAMP);
        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, dst.getHeight() + gap, paint);
        if (recycleSrc) {
            src.recycle();
        }
        return dst;
    }
}

Related Tutorials