实现图片高斯模糊

前两个效果很慢:

一.效果慢

Gilde配合一个依赖实现:

compile 'com.github.bumptech.glide:glide:3.7.0'
compile 'jp.wasabeef:glide-transformations:2.0.1'

Glide.with(this).load(R.drawable.bb).bitmapTransform(new BlurTransformation(this, 25)).into(welcomeImg);
25是模糊值1-25;


二.效果慢

app的build.gradle中,

defaultConfig里边加入
renderscriptTargetApi 24   //这个版本号是11到目前更新的最大的版本
renderscriptSupportModeEnabled true
代码实现高斯方法:

public static Bitmap blurBitmap(Context context, Bitmap bitmap) {
        //用需要创建高斯模糊bitmap创建一个空的bitmap
        Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.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);
        //设定模糊度(注:Radius最大只能设置25.f)
        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;
}
实现:
BitmapFactory.Options options = new BitmapFactory.Options();
Bitmap bitmap = BitmapFactory.decodeResource(getResources(), R.drawable.bb, options);
Bitmap bitmap1 = blurBitmap(this, bitmap);
welcomeImg.setImageBitmap(bitmap1);




你可能感兴趣的:(实现图片高斯模糊)