/** * 图片倒影 * * @param originalBitmap 原始图片 * @return 带倒影图片 */ public static Bitmap createReflectedImage(Bitmap originalBitmap, boolean isAppStart) { int refectionGap; if (isAppStart) { refectionGap = 0; } else { refectionGap = 4; } int width = originalBitmap.getWidth(); int height = originalBitmap.getHeight(); Matrix matrix = new Matrix(); matrix.preScale(1, -1);//x轴变为原来的1倍,y轴变为原来的-1倍 Bitmap reflectionBitmap = Bitmap.createBitmap(originalBitmap, 0, height >> 1, width, height >> 1, matrix, false); Bitmap withReflectionBitmap = Bitmap.createBitmap(width, (height + (height >> 1) + refectionGap), Bitmap.Config.ARGB_8888); Canvas canvas = new Canvas(withReflectionBitmap); //画原始图片 canvas.drawBitmap(originalBitmap, 0, 0, null); //画间隔 Paint defaultPaint = new Paint(); canvas.drawRect(0, height, width, height + refectionGap, defaultPaint); //画倒影 canvas.drawBitmap(reflectionBitmap, 0, height + refectionGap, null); //实现倒影 Paint paint = new Paint(); LinearGradient shader = new LinearGradient(0, originalBitmap.getHeight(), 0, withReflectionBitmap.getHeight() + 4, 0x70ffffff, 0x00ffffff, Shader.TileMode.MIRROR); paint.setShader(shader); paint.setXfermode(new PorterDuffXfermode(PorterDuff.Mode.DST_IN)); canvas.drawRect(0, height, width, withReflectionBitmap.getHeight(), paint); reflectionBitmap.recycle(); originalBitmap.recycle(); return withReflectionBitmap; } /** * 获取压缩比率 * * @param options * @param reqWidth 目标宽度 * @param reqHeight 目标高度 * @return 压缩比率 */ private static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { int height = options.outHeight; int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { int heightRatio = Math.round((float) height / (float) reqHeight); int widthRatio = Math.round((float) width / (float) reqWidth); inSampleSize = heightRatio < widthRatio ? heightRatio : widthRatio; } return inSampleSize; } /** * 对图片进行压缩 * * @param mResources Resources * @param imageId 图片资源ID * @param reqWidth 目标宽度 * @param reqHeight 目标高度 * @return bitmap */ public static Bitmap decodeSampleBitmapFromResource(Resources mResources, int imageId, int reqWidth, int reqHeight) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeResource(mResources, imageId, options); options.inSampleSize = calculateInSampleSize(options, reqWidth, reqHeight); options.inJustDecodeBounds = false; return BitmapFactory.decodeResource(mResources, imageId, options); }