使用 Glide 轻松实现模糊:
dependencies {
implementation 'com.github.bumptech.glide:glide:3.7.0'
}
在您的活动/片段中
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;
}
}