Android實現(xiàn)圖片的高斯模糊(兩種方式)
在市面上很多的APP都使用了對圖片作模糊化處理后作為背景的效果,來使得整個頁面更具有整體感。如下就是網(wǎng)易云音樂的音樂播放頁面:

很明顯這個頁面的背景是由中間的小圖片模糊化后而來的。最常用的模糊化處理就是高斯模糊。
高斯模糊的幾種實現(xiàn)方式:
(1)RenderScript
RenderScript是Google在Android 3.0(API 11)中引入的一個高性能圖片處理框架。
使用RenderScriprt實現(xiàn)高斯模糊:
首先在在build.gradle的defaultConfig中添加RenderScript的使用配置
renderscriptTargetApi 24
renderscriptSupportModeEnabled true
renderscriptTargetApi :
指定要生成的字節(jié)碼版本。我們(Goole官方)建議您將此值設置為最低API級別能夠提供所有的功能,你使用和設置renderscriptSupportModeEnabled為true。此設置的有效值是從11到
最近發(fā)布的API級別的任何整數(shù)值。
renderscriptSupportModeEnabled:
指定生成的字節(jié)碼應該回落到一個兼容的版本,如果運行的設備不支持目標版本。
下面就是使用RenderScriprt實現(xiàn)高斯模糊的方法:
public static Bitmap blurBitmap(Context context, Bitmap bitmap) {
//用需要創(chuàng)建高斯模糊bitmap創(chuàng)建一個空的bitmap
Bitmap outBitmap = Bitmap.createBitmap(bitmap.getWidth(), bitmap.getHeight(), Bitmap.Config.ARGB_8888);
// 初始化Renderscript,該類提供了RenderScript context,創(chuàng)建其他RS類之前必須先創(chuàng)建這個類,其控制RenderScript的初始化,資源管理及釋放
RenderScript rs = RenderScript.create(context);
// 創(chuàng)建高斯模糊對象
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
// 創(chuàng)建Allocations,此類是將數(shù)據(jù)傳遞給RenderScript內(nèi)核的主要方 法,并制定一個后備類型存儲給定類型
Allocation allIn = Allocation.createFromBitmap(rs, bitmap);
Allocation allOut = Allocation.createFromBitmap(rs, outBitmap);
//設定模糊度(注:Radius最大只能設置25.f)
blurScript.setRadius(15.f);
// Perform the Renderscript
blurScript.setInput(allIn);
blurScript.forEach(allOut);
// Copy the final bitmap created by the out Allocation to the outBitmap
allOut.copyTo(outBitmap);
// recycle the original bitmap
// bitmap.recycle();
// After finishing everything, we destroy the Renderscript.
rs.destroy();
return outBitmap;
}
(2)Glide實現(xiàn)高斯模糊
Glide是一個比較強大也是比較常用的一個圖片加載庫,Glide中的Transformations用于在圖片顯示前對圖片進行處理。glide-transformations 這個庫為Glide提供了多種多樣的 Transformations實
現(xiàn),其中就包括高斯模糊的實現(xiàn)BlurTransformation
compile 'com.github.bumptech.glide:glide:3.7.0' compile 'jp.wasabeef:glide-transformations:2.0.1'
通過這兩個庫的結合使用,就可以使用其中的BlurTransformation實現(xiàn)圖片的高斯模糊
Glide.with(context).load(R.drawable.defalut_photo).bitmapTransform(new BlurTransformation(context, radius)).into(mImageView);
其中radius的取值范圍是1-25,radius越大,模糊度越高。
以上所述是小編個大家介紹的Android實現(xiàn)圖片的高斯模糊,希望對大家有所幫助,如果大家有任何疑問歡迎給我留言,小編會及時回復大家的!
相關文章
Android自定義ViewGroup實現(xiàn)受邊界限制的滾動操作(3)
這篇文章主要為大家詳細介紹了Android自定義ViewGroup實現(xiàn)受邊界限制的滾動操作,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
Android串口開發(fā)之使用JNI實現(xiàn)ANDROID和串口通信詳解
這篇文章主要給大家介紹了關于Android串口開發(fā)之使用JNI實現(xiàn)ANDROID和串口通信的相關資料,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧。2018-01-01
Android React-Native通信數(shù)據(jù)模型分析
這篇文章主要介紹了Android React-Native通信數(shù)據(jù)模型分析的相關資料,需要的朋友可以參考下2016-10-10

