get Reflected Bitmap - Android Graphics

Android examples for Graphics:Bitmap Reflection

Description

get Reflected 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;
import android.graphics.PorterDuffXfermode;

import android.graphics.Shader;
import android.graphics.Shader.TileMode;

public class Main {

    public static Bitmap getReflectedBitmap(Bitmap mBitmap) {
        int width = mBitmap.getWidth();
        int height = mBitmap.getHeight();

        Matrix matrix = new Matrix();
        matrix.preScale(1, -1);/* w  w w . jav a  2  s. c om*/

        Bitmap mInverseBitmap = Bitmap.createBitmap(mBitmap, 0, 0, width,
                height, matrix, false);
        Bitmap mReflectedBitmap = Bitmap.createBitmap(width, height * 2,
                Config.ARGB_8888);
        Canvas mCanvas = new Canvas(mReflectedBitmap);
        mCanvas.drawBitmap(mBitmap, 0, 0, null);
        mCanvas.drawBitmap(mInverseBitmap, 0, height, null);

        Paint mPaint = new Paint();
        Shader mShader = new LinearGradient(0, height, 0,
                mReflectedBitmap.getHeight(), 0x70ffffff, 0x00ffffff,
                TileMode.MIRROR);
        mPaint.setShader(mShader);
        mPaint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN));
        mCanvas.drawRect(0, height, width, mReflectedBitmap.getHeight(),
                mPaint);

        return mReflectedBitmap;
    }
}

Related Tutorials