公司项目需要,要求实现一个具有高斯模糊背景的快捷对话框效果。以下是效果图:
首先第一步是截取系统屏幕截图:
记住是获取系统屏幕的截图(一般是需要root权限或者在服务器上编译调用系统的截屏方法,本文则采用后者)
View view = activity.getWindow().getDecorView();
以上这种方式只能截取当前应用的截图、并不是系统截屏,所以不可取,下面我们来查看下系统截屏的方法:
frameworks\base\packages\SystemUI\src\com\android\systemui\screenshot\GlobalScreenshot.java这个类中:void takeScreenshot(Runnable finisher, boolean statusBarVisible, boolean navBarVisible)的方法中:
mScreenBitmap = SurfaceControl.screenshot((int) dims[0], (int) dims[1]);
这样我们就能很一目了然的知道系统的截屏是通过SurfaceControl这个类进行实现的,而这个mScreenBitmap就是系统截取的图片。至此,实现系统截图就已完成,接下来是讲图片实现高斯模糊处理,我用的是网上的高斯模糊算法:
public static Bitmap blurBitmap(Bitmap bitmap, Context context) {
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(),
bitmap.getHeight(), Config.ARGB_8888);
RenderScript rs = RenderScript.create(context);
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs,
Element.U8_4(rs));
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
blurScript.setRadius(20.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;
}
public static Bitmap createBlurBitmap(Bitmap sentBitmap, int radius) {
Bitmap bitmap = sentBitmap.copy(sentBitmap.getConfig(), true);
if (radius < 1) {
return (null);
}
&nbs