高斯模糊实现
效果图如下:
想要实现高斯模糊:首先要截取当前屏幕,获取标题栏并且移除掉,然后对Bitmap进行处理。接下来看代码:
/**
* 获取当前显示ui界面
* @return
*/
private Bitmap applyBlur() {
View view = getWindow().getDecorView();
view.setDrawingCacheEnabled(true); //自动把原来的Cache销毁
view.buildDrawingCache(true);
// view.measure(MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED), MeasureSpec.makeMeasureSpec(0, MeasureSpec.UNSPECIFIED));
// view.layout(0, 0, view.getMeasuredWidth(), view.getMeasuredHeight());
view.destroyDrawingCache(); //刷新缓存
/**
* 获取当前窗口快照,相当于截屏
*/
Bitmap bmp1 = view.getDrawingCache(); //安卓系统4.2 这里获取不到bitmap 把上面俩行代码取消注释就行了
int height = getOtherHeight(); //获取标题栏
/**
11. * 除去状态栏和标题栏
12. */
Bitmap bmp2 = Bitmap.createBitmap(bmp1, 0, height,bmp1.getWidth(), bmp1.getHeight() - height);
bmp2.recycle();
return blurBitmap(bmp2);
}
/**
* 获取系统状态栏和软件标题栏,部分软件没有标题栏,看自己软件的配置;
* @return
*/
private int getOtherHeight() {
Rect frame = new Rect();
getWindow().getDecorView().getWindowVisibleDisplayFrame(frame);
int statusBarHeight = frame.top;
int contentTop = getWindow().findViewById(Window.ID_ANDROID_CONTENT).getTop();
int titleBarHeight = contentTop - statusBarHeight;
return statusBarHeight + titleBarHeight;
}
/**
* 进行渲染
* @param bitmap
* @return
*/
public Bitmap blurBitmap(Bitmap bitmap){
//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(), Config.ARGB_8888);
//Instantiate a new Renderscript
RenderScript rs = RenderScript.create(getApplicationContext());
//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.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;
}
截止图片已经实现模糊,然后直接给布局赋背景就ok了。