淺析Android高斯模糊實現(xiàn)方案
1、使用Glide
Glide.with(this)
.load(service.getImageUri())
.dontAnimate()
.error(R.drawable.error_img)
// 設置高斯模糊
.bitmapTransform(new BlurTransformation(this, 14, 3))
.into(imageview);
適用場景:動態(tài)配置的背景圖片
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);
// 創(chuàng)建一張渲染后的輸出圖片
Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
// 創(chuàng)建RenderScript內核對象
RenderScript rs = RenderScript.create(context);
// 創(chuàng)建一個模糊效果的RenderScript的工具對象
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// 由于RenderScript并沒有使用VM來分配內存,所以需要使用Allocation類來創(chuàng)建和分配內存空間
// 創(chuàng)建Allocation對象的時候其實內存是空的,需要使用copyTo()將數(shù)據(jù)填充進去
Allocation tmpIn = Allocation.createFromBitmap(rs, inputBitmap);
Allocation tmpOut = Allocation.createFromBitmap(rs, outputBitmap);
// 設置渲染的模糊程度, 25f是最大模糊度
blurScript.setRadius(blurRadius);
// 設置blurScript對象的輸入內存
blurScript.setInput(tmpIn);
// 將輸出數(shù)據(jù)保存到輸出內存中
blurScript.forEach(tmpOut);
// 將數(shù)據(jù)填充到Allocation中
tmpOut.copyTo(outputBitmap);
return outputBitmap;
}
}
不推薦:使用bitmap,頻繁操作的話比較耗性能。
3、使用高斯模糊遮罩,可以對指定區(qū)域進行模糊,不需要處理單張圖片(推薦?。。?/strong>
推薦一個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",這里設置成透明色,效果就如同直接對圖片進行高斯模糊。
總結
以上所述是小編給大家介紹的Android高斯模糊實現(xiàn)方案,希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
如果你覺得本文對你有幫助,歡迎轉載,煩請注明出處,謝謝!
相關文章
Android nativePollOnce函數(shù)解析
這篇文章主要介紹了Android nativePollOnce函數(shù)解析的相關資料,幫助大家更好的理解和學習使用Android,感興趣的朋友可以了解下2021-03-03
Android項目實戰(zhàn)手把手教你畫圓形水波紋loadingview
這篇文章主要為大家詳細介紹了Android項目實戰(zhàn)手把手教你畫圓形水波紋loadingview,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-01-01
Android Presentation雙屏異顯開發(fā)流程詳細講解
最近開發(fā)的一個項目,有兩個屏幕,需要將第二個頁面投屏到副屏上,這就需要用到Android的雙屏異顯(Presentation)技術了,研究了一下,這里做下筆記2023-01-01

