1、使用Glide
Glide.with(this)
.load(service.getImageUri())
.dontAnimate()
.error(R.drawable.error_img)
// 設(shè)置高斯模糊
.bitmapTransform(new BlurTransformation(this, 14, 3))
.into(imageview);
適用場景:動(dòng)態(tài)配置的背景圖片
2割笙、對(duì)圖片高斯模糊友瘤,需要先將圖片轉(zhuǎn)成bitmap對(duì)象
import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.Bitmap;
import android.os.Build;
import android.renderscript.Allocation;
import android.renderscript.Element;
import android.renderscript.RenderScript;
import android.renderscript.ScriptIntrinsicBlur;
public class BlurBitmapUtil {
// 圖片縮放比例(即模糊度)
private static final float BITMAP_SCALE = 0.4f;
/**
* @param context 上下文對(duì)象
* @param image 需要模糊的圖片
* @return 模糊處理后的Bitmap
*/
@TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1)
public static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) {
// 計(jì)算圖片縮小后的長寬
int width = Math.round(image.getWidth() * BITMAP_SCALE);
int height = Math.round(image.getHeight() * BITMAP_SCALE);
// 將縮小后的圖片做為預(yù)渲染的圖片
Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false);
// 創(chuàng)建一張渲染后的輸出圖片
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
// 創(chuàng)建RenderScript內(nèi)核對(duì)象
RenderScript rs = RenderScript.create(context);
// 創(chuàng)建一個(gè)模糊效果的RenderScript的工具對(duì)象
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// 由于RenderScript并沒有使用VM來分配內(nèi)存,所以需要使用Allocation類來創(chuàng)建和分配內(nèi)存空間
// 創(chuàng)建Allocation對(duì)象的時(shí)候其實(shí)內(nèi)存是空的,需要使用copyTo()將數(shù)據(jù)填充進(jìn)去
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
// 設(shè)置渲染的模糊程度, 25f是最大模糊度
blurScript.setRadius(blurRadius);
// 設(shè)置blurScript對(duì)象的輸入內(nèi)存
blurScript.setInput(tmpIn);
// 將輸出數(shù)據(jù)保存到輸出內(nèi)存中
blurScript.forEach(tmpOut);
// 將數(shù)據(jù)填充到Allocation中
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
}
不推薦:使用bitmap翠肘,頻繁操作的話比較耗性能。
3商佑、使用高斯模糊遮罩锯茄,可以對(duì)指定區(qū)域進(jìn)行模糊,不需要處理單張圖片(推薦2杳弧<∮摹)
推薦一個(gè)github上的項(xiàng)目,親測有效抓半。https://github.com/mmin18/RealtimeBlurView
<com.github.mmin18.widget.RealtimeBlurView
android:id="@+id/blurview"
android:layout_width="match_parent"
android:layout_height="210dp"
android:visibility="gone"
app:realtimeBlurRadius="5dp"
app:realtimeOverlayColor="#00000000" />
app:realtimeOverlayColor="#00000000"喂急,這里設(shè)置成透明色,效果就如同直接對(duì)圖片進(jìn)行高斯模糊笛求。