实现图片模糊,高斯模糊

高斯模糊实现

效果图如下:
这里写图片描述
这里写图片描述
想要实现高斯模糊:首先要截取当前屏幕,获取标题栏并且移除掉,然后对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了。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值