package io.hefuyi.listener.util; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.drawable.BitmapDrawable; import android.graphics.drawable.Drawable; import android.support.v8.renderscript.RenderScript; import java.io.ByteArrayInputStream; import java.io.ByteArrayOutputStream; public class ImageUtil { public static Drawable createBlurredImageFromBitmap(Bitmap bitmap, Context context, int inSampleSize) { RenderScript rs = RenderScript.create(context); final BitmapFactory.Options options = new BitmapFactory.Options(); options.inSampleSize = inSampleSize; ByteArrayOutputStream stream = new ByteArrayOutputStream(); bitmap.compress(Bitmap.CompressFormat.JPEG, 100, stream); byte[] imageInByte = stream.toByteArray(); ByteArrayInputStream bis = new ByteArrayInputStream(imageInByte); Bitmap blurTemplate = BitmapFactory.decodeStream(bis, null, options); final android.support.v8.renderscript.Allocation input = android.support.v8.renderscript.Allocation.createFromBitmap(rs, blurTemplate); final android.support.v8.renderscript.Allocation output = android.support.v8.renderscript.Allocation.createTyped(rs, input.getType()); final android.support.v8.renderscript.ScriptIntrinsicBlur script = android.support.v8.renderscript.ScriptIntrinsicBlur.create(rs, android.support.v8.renderscript.Element.U8_4(rs)); script.setRadius(4f); script.setInput(input); script.forEach(output); output.copyTo(blurTemplate); return new BitmapDrawable(context.getResources(), blurTemplate); } }