如何更改图像视图的半径模糊

How to change the radius blur of image view

我在 build.gradle 中添加了这个依赖来模糊我的 imageView :

dependencies {
    implementation 'com.jackandphantom.android:blurimage:1.2.0'
}

以及我的 MainActivity.java 中的以下代码:

    bgImage = (ImageView) findViewById(R.id.imageView);
   Bitmap bitmap = BlurImage.with(getApplicationContext()).load(R.drawable.profilbackground).intensity(25).getImageBlur();
    bgImage.setImageBitmap(bitmap);

问题是这种方式只是模糊到 25 半径,我希望它达到 50 或 100。那么,如何更改图像视图的半径模糊?请给我一个方法。

根据文档它说

这个库有不同的方法可以用来保持你的图像模糊。

   BlurImage.with(getApplicationContext()).load(R.drawable.myImage).intensity(20).Async(true).into(imageView);

BlurImage.with(getApplicationContext()).load(bitmap_Image).intensity(20).Async(true).into(imageView);

方法(强度):-强度(整数值)

{ Increase Blur and limit of value is in between 0 to 25 }

最大限制是25,如果你想改变这个值,你需要自己fork那个特性,或者请求库的作者。

我会告诉你一个解决方法,

  • 将图像缩小 7 倍
  • 运行 再次模糊 25
  • 将图像缩小到原始大小

使用 Glide 轻松实现模糊:

dependencies {

       implementation 'com.github.bumptech.glide:glide:3.7.0'
}

在你的Activity/Fragment

Glide.with(getActivity()).load(loginDetails.getUserdetail().getImage()).asBitmap().into(new SimpleTarget<Bitmap>() {
            @Override
            public void onResourceReady(Bitmap resource, GlideAnimation<? super Bitmap> glideAnimation) {
                profileImageView.setImageBitmap(BlurImageView.blur(resource));
            }
        });

BlurImageView.java

public class BlurImageView {
    private static final float BITMAP_SCALE = 0.4f;
    private static final float BLUR_RADIUS = 25f;

    public static Bitmap blur(Bitmap bitmap) {
        Bitmap u8_4Bitmap;
        if (bitmap.getConfig() == Bitmap.Config.ARGB_8888) {
            u8_4Bitmap = bitmap;
        } else {
            u8_4Bitmap = bitmap.copy(Bitmap.Config.ARGB_8888, true);
        }
        int width = Math.round(u8_4Bitmap.getWidth() * BITMAP_SCALE);
        int height = Math.round(u8_4Bitmap.getHeight() * BITMAP_SCALE);
        Bitmap inputBitmap = Bitmap.createScaledBitmap(u8_4Bitmap, width, height, false);
        Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
        RenderScript rs = RenderScript.create(UpasargaApplication.getAppContext());
        ScriptIntrinsicBlur theIntrinsic = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
        Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
        Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
        theIntrinsic.setRadius(BLUR_RADIUS);
        theIntrinsic.setInput(tmpIn);
        theIntrinsic.forEach(tmpOut);
        tmpOut.copyTo(outputBitmap);
        return outputBitmap;

    }
}

implementation 'com.squareup.picasso:picasso:2.5.2'

<ImageView
    android:id="@+id/iv_profilepic"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
     android:src="@mipmap/ic_launcher"/>

Target target = new Target() {
@Override
public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {        imageViewBackground.setImageBitmap(BlurImage.fastblur(bitmap, 1f, BLUR_PRECENTAGE));
    }
    @Override
    public void onBitmapFailed(Drawable errorDrawable) {
        imageViewBackground.setImageResource(R.mipmap.ic_launcher);
    }
    @Override
    public void onPrepareLoad(Drawable placeHolderDrawable) {

    }
};