淺析Android高斯模糊實(shí)現(xiàn)方案
1、使用Glide
Glide.with(this) .load(service.getImageUri()) .dontAnimate() .error(R.drawable.error_img) // 設(shè)置高斯模糊 .bitmapTransform(new BlurTransformation(this, 14, 3)) .into(imageview);
適用場(chǎng)景:動(dòng)態(tài)配置的背景圖片
2、對(duì)圖片高斯模糊,需要先將圖片轉(zhuǎn)成bitmap對(duì)象
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 上下文對(duì)象 * @param image 需要模糊的圖片 * @return 模糊處理后的Bitmap */ @TargetApi(Build.VERSION_CODES.JELLY_BEAN_MR1) public static Bitmap blurBitmap(Context context, Bitmap image, float blurRadius) { // 計(jì)算圖片縮小后的長(zhǎng)寬 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并沒(méi)有使用VM來(lái)分配內(nèi)存,所以需要使用Allocation類來(lái)創(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)行模糊,不需要處理單張圖片(推薦?。。?/strong>
推薦一個(gè)github上的項(xiàng)目,親測(cè)有效。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)行高斯模糊。
總結(jié)
以上所述是小編給大家介紹的Android高斯模糊實(shí)現(xiàn)方案,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
如果你覺(jué)得本文對(duì)你有幫助,歡迎轉(zhuǎn)載,煩請(qǐng)注明出處,謝謝!
相關(guān)文章
Android Dialog對(duì)話框用法實(shí)例詳解
這篇文章主要介紹了Android Dialog對(duì)話框用法,結(jié)合實(shí)例形式分析了Android使用Dialog對(duì)話框過(guò)程中所涉及的創(chuàng)建、保存、回復(fù)等操作相關(guān)技巧與注意事項(xiàng),需要的朋友可以參考下2016-07-07Android基礎(chǔ)之使用Fragment控制切換多個(gè)頁(yè)面
Android官方已經(jīng)提供了Fragment的各種使用的Demo例子,在我們SDK下面的API Demo里面就包含了Fragment的各種使用例子,需要看Demo的朋友,直接看API Demo那個(gè)程序就可以了,不用到處去找。里面分開(kāi)不同功能,實(shí)現(xiàn)了不同的類2013-07-07Android檢測(cè)url地址是否可達(dá)的兩種方法
今天小編就為大家分享一篇Android檢測(cè)url地址是否可達(dá)的兩種方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-01-01總結(jié)Android中MD風(fēng)格相關(guān)控件
自Android5.0發(fā)布以來(lái),谷歌推出全新的Material Desigen設(shè)計(jì)風(fēng)格,時(shí)過(guò)一年多了,在國(guó)內(nèi)也看到很多應(yīng)用在慢慢適應(yīng)MD設(shè)計(jì)風(fēng)格。今天小編給大家總結(jié)下Android中MD風(fēng)格相關(guān)控件的知識(shí),有需要的可以參考學(xué)習(xí)。2016-08-08Android之使用Android-query框架開(kāi)發(fā)實(shí)戰(zhàn)(一)
這篇文章主要介紹了Android之使用Android-query框架開(kāi)發(fā)實(shí)戰(zhàn)(一)的相關(guān)資料,需要的朋友可以參考下2015-10-10利用Android實(shí)現(xiàn)比較炫酷的自定義View
自定義View、多線程、網(wǎng)絡(luò),被認(rèn)為是Android開(kāi)發(fā)者必須牢固掌握的最基礎(chǔ)的三大基本功,這篇文章主要給大家介紹了關(guān)于如何利用Android實(shí)現(xiàn)比較炫酷的自定義View的相關(guān)資料,需要的朋友可以參考下2021-07-07Android 手機(jī)防止休眠的兩種實(shí)現(xiàn)方法
這篇文章主要介紹了Android 手機(jī)防止休眠方法的相關(guān)資料,一種是在Manifest.xml文件里面聲明,另外一種方法是在代碼里面修改LayoutParams的標(biāo)志位,需要的朋友可以參考下2017-08-08