1、使用Glide
Glide.with(this) .load(service.getImageUri()) .dontAnimate() .error(R.drawable.error_img) // 設定高斯模糊 .bitmapTransform(new BlurTransformation(this, 14, 3)) .into(imageview);
適用場景:動態配置的背景圖片
2、對圖片高斯模糊,需要先將圖片轉成bitmap物件
mport 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 背景關系物件 * @param image 需要模糊的圖片 * @return 模糊處理后的Bitmap */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) { // 計算圖片縮小后的長寬 int width = Math.round(image.getWidth() * BITMAP_SCALE); int height = Math.round(image.getHeight() * BITMAP_SCALE); // 將縮小后的圖片做為預渲染的圖片 Bitmap inputBitmap = Bitmap.createScaledBitmap(image, width, height, false); // 創建一張渲染后的輸出圖片 Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap); // 創建RenderScript內核物件 RenderScript rs = RenderScript.create(context); // 創建一個模糊效果的RenderScript的工具物件 ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); // 由于RenderScript并沒有使用VM來分配記憶體,所以需要使用Allocation類來創建和分配記憶體空間 // 創建Allocation物件的時候其實記憶體是空的,需要使用copyTo()將資料填充進去 Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap); Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap); // 設定渲染的模糊程度, 25f是最大模糊度 blurScript.setRadius(blurRadius); // 設定blurScript物件的輸入記憶體 blurScript.setInput(tmpIn); // 將輸出資料保存到輸出記憶體中 blurScript.forEach(tmpOut); // 將資料填充到Allocation中 tmpOut.copyTo(outputBitmap); return outputBitmap; }}
不推薦:使用bitmap,頻繁操作的話比較耗性能,
3、使用高斯模糊遮罩,可以對指定區域進行模糊,不需要處理單張圖片(推薦!!)
推薦一個github上的專案,親測有效,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",這里設定成透明色,效果就如同直接對圖片進行高斯模糊,
轉載請註明出處,本文鏈接:https://www.uj5u.com/yidong/53689.html
標籤:Android
上一篇:1w+的心路歷程
下一篇:More than one file was found with OS independent path 'lib/armeabi-v7a/libgnustl_shared.so'
