RenderScript实现图片模糊

注意:ScriptIntrinsicBlur的相关方法只支持API 17及以上版本的系统,为了兼容旧版本,Google 提供了android.support.v8.renderscript兼容包。android.support.v8.renderscript兼容包的使用不需要在app/build.gradle文件中额外添加依赖,而是在defaultConfig配置中添加如下两行代码:

defaultConfig {
    ......
    renderscriptTargetApi 19
    renderscriptSupportModeEnabled  true
}

模糊方法:

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: 0 < radius <= 25
    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;   
}
©著作权归作者所有,转载或内容合作请联系作者
【社区内容提示】社区部分内容疑似由AI辅助生成,浏览时请结合常识与多方信息审慎甄别。
平台声明:文章内容(如有图片或视频亦包括在内)由作者上传并发布,文章内容仅代表作者本人观点,简书系信息发布平台,仅提供信息存储服务。

相关阅读更多精彩内容

友情链接更多精彩内容