首页 > 代码库 > Android中创建倒影效果的工具类
Android中创建倒影效果的工具类
一、有时候我们需要创建倒影的效果,我们接触最多的都是图片能够创建倒影,而布局依然可以创建倒影。
二、工具类代码
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; import android.view.View; /** * 创建图片倒影的方法 * * @author * */ public class ImageReflect { private static int reflectImageHeight = 90;//倒影的高度 //转化为bitmap public static Bitmap convertViewToBitmap(View paramView) { paramView.measure(View.MeasureSpec.makeMeasureSpec(0, 0), View.MeasureSpec.makeMeasureSpec(0, 0)); paramView.layout(0, 0, paramView.getMeasuredWidth(), paramView.getMeasuredHeight()); paramView.buildDrawingCache(); return paramView.getDrawingCache(); } public static Bitmap createCutReflectedImage(Bitmap paramBitmap, int paramInt) { int i = paramBitmap.getWidth(); int j = paramBitmap.getHeight(); Bitmap localBitmap2 = null; if (j <= paramInt + reflectImageHeight) { localBitmap2 = null; } else { Matrix localMatrix = new Matrix(); localMatrix.preScale(1.0F, -1.0F); // System.out.println(j - reflectImageHeight - // paramInt); Bitmap localBitmap1 = Bitmap.createBitmap(paramBitmap, 0, j - reflectImageHeight - paramInt, i, reflectImageHeight, localMatrix, true); localBitmap2 = Bitmap.createBitmap(i, reflectImageHeight, Bitmap.Config.ARGB_8888); Canvas localCanvas = new Canvas(localBitmap2); localCanvas.drawBitmap(localBitmap1, 0.0F, 0.0F, null); LinearGradient localLinearGradient = new LinearGradient(0.0F, 0.0F, 0.0F, localBitmap2.getHeight(), -2130706433, 16777215, TileMode.CLAMP); Paint localPaint = new Paint(); localPaint.setShader(localLinearGradient); localPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); localCanvas.drawRect(0.0F, 0.0F, i, localBitmap2.getHeight(), localPaint); if (!localBitmap1.isRecycled()) localBitmap1.recycle(); System.gc(); } return localBitmap2; } public static Bitmap createReflectedImage(Bitmap paramBitmap, int paramInt) { int i = paramBitmap.getWidth(); int j = paramBitmap.getHeight(); Bitmap localBitmap2; if (j <= paramInt) { localBitmap2 = null; } else { Matrix localMatrix = new Matrix(); localMatrix.preScale(1.0F, -1.0F); Bitmap localBitmap1 = Bitmap.createBitmap(paramBitmap, 0, j - paramInt, i, paramInt, localMatrix, true); localBitmap2 = Bitmap.createBitmap(i, paramInt, Bitmap.Config.ARGB_8888); Canvas localCanvas = new Canvas(localBitmap2); localCanvas.drawBitmap(localBitmap1, 0.0F, 0.0F, null); LinearGradient localLinearGradient = new LinearGradient(0.0F, 0.0F, 0.0F, localBitmap2.getHeight(), -2130706433, 16777215, TileMode.CLAMP); Paint localPaint = new Paint(); localPaint.setShader(localLinearGradient); localPaint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); localCanvas.drawRect(0.0F, 0.0F, i, localBitmap2.getHeight(), localPaint); } return localBitmap2; } public static Bitmap createReflectedImage(Bitmap originalImage) { final int reflectionGap = 4; int width = originalImage.getWidth(); int height = originalImage.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1); Bitmap reflectionImage = Bitmap.createBitmap(originalImage, 0, height / 2, width, height / 2, matrix, false); Bitmap bitmapWithReflection = Bitmap.createBitmap(width, (height + height / 2), Config.ARGB_8888); Canvas canvas = new Canvas(bitmapWithReflection); canvas.drawBitmap(originalImage, 0, 0, null); Paint defaultPaint = new Paint(); canvas.drawRect(0, height, width, height + reflectionGap, defaultPaint); canvas.drawBitmap(reflectionImage, 0, height + reflectionGap, null); Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, originalImage.getHeight(), 0, bitmapWithReflection.getHeight() + reflectionGap, 0x70ffffff, 0x00ffffff, TileMode.MIRROR); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(Mode.DST_IN)); canvas.drawRect(0, height, width, bitmapWithReflection.getHeight() + reflectionGap, paint); return bitmapWithReflection; } }
三、这里面有很多种方法,可以自己根据需求去选择。
调用示例:
ps:我这是创建多个倒影,所以用了一个for循环。循环操作的是两个图片数组。
for (int k = 0; k < REFLECT_COUNT; k++) { arrayOfImageView[k].setImageBitmap(ImageReflect.createCutReflectedImage(ImageReflect.convertViewToBitmap(imagereflect[k]), 0)); }
声明:以上内容来自用户投稿及互联网公开渠道收集整理发布,本网站不拥有所有权,未作人工编辑处理,也不承担相关法律责任,若内容有误或涉及侵权可进行投诉: 投诉/举报 工作人员会在5个工作日内联系你,一经查实,本站将立刻删除涉嫌侵权内容。