package com.example.reflectionimageview;
import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.LinearGradient;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.graphics.Bitmap.Config;
import android.graphics.PorterDuff.Mode;
import android.graphics.PorterDuffXfermode;
import android.graphics.Shader.TileMode;
/***
* 图片倒影效果
*
* @author ccb
*
*/
public class CreateReflectionBitmap {
private final static int distance = 4;
public static Bitmap createReflectionBitmap(Bitmap normalBitmap) {
// 正常图片的宽高
int height = normalBitmap.getHeight();
int width = normalBitmap.getWidth();
// 翻转的bitmap对象
Bitmap reflectionBitmap = null;
// 获取矩阵操作对象
Matrix matrix = new Matrix();
// 设置矩阵操作,旋转180度
matrix.setRotate(180f);
// 为翻转位图赋值
reflectionBitmap = Bitmap.createBitmap(normalBitmap, 0, height / 2,
width, height / 2, matrix, false);
// 创建画板,传一个位图对象,用来设置画布大小
Bitmap canvasBmp = Bitmap.createBitmap(width, height + height / 2,
Config.ARGB_8888);
Canvas canvas = new Canvas(canvasBmp);
// 画笔,绘制两个位图之间的间隙
Paint defaultPaint = new Paint();
defaultPaint.setColor(android.R.color.white);
canvas.drawRect(0, height, width, height + distance, defaultPaint);
// 绘制正常的位图
canvas.drawBitmap(normalBitmap, 0, 0, null);
// 绘制翻转位图
canvas.drawBitmap(reflectionBitmap, 0, height + distance, null);
// 渐变效果,tile:渐变的模式
LinearGradient gradient = new LinearGradient(0, height + distance, 0,
canvasBmp.getHeight(), 0x70ffffff, 0x00ffffff, TileMode.MIRROR);
// 绘制渐变的画笔
Paint gradientPaint = new Paint();
gradientPaint.setShader(gradient);
gradientPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN));
// 绘制渐变
canvas.drawRect(0, height, width, canvasBmp.getHeight() + distance,
gradientPaint);
return canvasBmp;
}
}