实现本地图片背景高斯模糊
工具类
public class ActivityUtils {
/**
* 高斯模糊
* @param bitmap
* @return
*/
public static Bitmap blurBitmap(Bitmap bitmap, Context context) {
if (bitmap == null){
return null;
}
//Let's create an empty bitmap with the same size of the bitmap we want to blur
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
//Instantiate a new Renderscript
RenderScript rs = RenderScript.create(context);
//Create an Intrinsic Blur Script using the Renderscript
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
//Create the Allocations (in/out) with the Renderscript and the in/out bitmaps
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
//Set the radius of the blur
blurScript.setRadius(25.0f);
//Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
//Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
//recycle the original bitmap
bitmap.recycle();
//After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
}
}
使用
Bitmap bmp= BitmapFactory.decodeResource(resource res,int id);
//(mContext.getResources(), R.mipmap.xxx);
Bitmap blur= ActivityUtils.blurBitmap(bmp,mContext);
detail_background.setImageBitmap(blur);
关于Bitmap使用参考:Bitmap使用详解