获取图片倒影效果
View Code
1 /** 2 * 获取传入图片的倒影 3 * 4 * @param originalImage 原始图片 5 * @param reflectionGap 原始图片与倒影图片的间距 6 * @param percentage 倒影是原图的高度的百分比 . 0< percentage < 1 7 * @param onlyReflect true, 只返回倒影图片,倒影有蒙板效果; 8 * false, 返回原图 + 间距 + 倒影(从上到下的顺序), 间距和倒影都有蒙板效果 9 * @return Bitmap; 10 */ 11 public static Bitmap createReflectionImageWithOrigin(Bitmap originalImage, int reflectionGap, float percentage, boolean onlyReflect){ 12 13 int width = originalImage.getWidth(); 14 int height = originalImage.getHeight(); 15 int preHeight = (int)(height * percentage); 16 17 Matrix matrix = new Matrix(); 18 matrix.preScale(1, -1); // 图片矩阵变换(从低部向顶部的倒影) 19 20 Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, (height - preHeight), width, preHeight, matrix, false); // 截取原图下半部分 21 22 Bitmap reflectionBitmp = Bitmap.createBitmap(width, preHeight, Config.ARGB_8888); 23 24 Canvas canvas = new Canvas(reflectionBitmp);// 绘制倒影图 25 Paint defaultPaint = new Paint(); 26 canvas.drawRect(0, height / 5, width, height / 5, defaultPaint); 27 canvas.drawBitmap(reflectionImage, 0, 0, null); 28 29 //给倒影图片添加遮罩效果 30 Paint paint = new Paint(); 31 LinearGradient shader = new LinearGradient(0, 0, 0, reflectionBitmp.getHeight(), 0x70ffffff, 0x00ffffff, TileMode.CLAMP); 32 paint.setShader(shader); // 线性渐变效果 33 paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); // 倒影遮罩效果 34 canvas.drawRect(0, 0, width, reflectionBitmp.getHeight(), paint); // 绘制倒影的阴影效果 35 36 Bitmap tempReflectionBitmap = reflectionBitmp; 37 38 if (! onlyReflect) { 39 tempReflectionBitmap = Bitmap.createBitmap(width, (height + preHeight), Config.ARGB_8888); // 创建倒影图片 40 canvas = new Canvas(tempReflectionBitmap);// 绘制倒影图(原图 + 间距 + 倒影) 41 canvas.drawBitmap(originalImage, 0, 0, null); // 绘制原图 42 canvas.drawRect(0, height,width,height + reflectionGap, defaultPaint); // 绘制原图与倒影的间距 43 canvas.drawRect(0, height, width, height + reflectionGap, paint); // 绘制间距的阴影效果 44 canvas.drawBitmap(reflectionBitmp, 0, height + reflectionGap, null); // 绘制倒影图 45 } 46 47 return tempReflectionBitmap; 48 }