<span style="white-space:pre"> </span>public static Bitmap blurBitmap(Bitmap bitmap, Context context) {
// 用需要创建高斯模糊bitmap创建一个空的bitmap
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Config.ARGB_8888);
// 初始化Renderscript,这个类提供了RenderScript context,在创建其他RS类之前必须要先创建这个类,他控制RenderScript的初始化,资源管理,释放
RenderScript rs = RenderScript.create(context);
// 创建高斯模糊对象
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// 创建Allocations,此类是将数据传递给RenderScript内核的主要方法,并制定一个后备类型存储给定类型
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
// 设定模糊度
blurScript.setRadius(25.f);
// 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;
}然后是调用部分<span style="white-space:pre"> </span>public static Bitmap getBlurBitmap(View rootView, Context context) {
try {
if (rootView == null || context == null) {
return null;
}
rootView.setDrawingCacheEnabled(true);
Bitmap drawingCache = rootView.getDrawingCache();
Bitmap bgBitmap = Bitmap.createBitmap(drawingCache);
return BitmapUtil.blurBitmap(bgBitmap, context);
} catch (Exception e) {
e.printStackTrace();
}
return null;
}这样就可以就实现了高斯模糊效果,再将模糊效果显示在界面上就可以了。版权声明:本文为博主原创文章,未经博主允许不得转载。
原文:http://blog.csdn.net/yangxin_540/article/details/47207727