Android RenderScript高斯模糊
看代碼的時(shí)候,看到了其中有.rs結(jié)尾的文件,不是很明白,還有RenderScript類,看的一臉蒙蔽,不知所云,然后百度了一下,收貨還真不少,這東西在圖形處理這塊用處挺大的。
今天先說說ScriptIntrinsicBlur,這個(gè)類不需要定義rs文件,從這個(gè)Intrinsic單詞可以看的出來,它是API17以后內(nèi)置的類,專門用來處理圖像的,讓圖片變模糊。
public static Bitmap blurBitmap(Bitmap bitmap, float radius, Context context) {
//創(chuàng)建渲染腳本上下文
RenderScript rs = RenderScript.create(context);
//為位圖分配內(nèi)存
Allocation allocation = Allocation.createFromBitmap(rs, bitmap);
Type t = allocation.getType();
//用同樣的類型創(chuàng)建內(nèi)存,一般用這兩種方式創(chuàng)建 <span style="font-family: Arial, Helvetica, sans-serif;">Allocation</span>
Allocation blurredAllocation = Allocation.createTyped(rs, t);
//創(chuàng)建高斯渲染腳本
ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs));
//設(shè)置模糊半徑 (maximum 25.0)
blurScript.setRadius(radius);
//為腳本設(shè)置輸入?yún)?shù)
blurScript.setInput(allocation);
//調(diào)用腳本 結(jié)果存入 <span style="font-family: Arial, Helvetica, sans-serif;">blurredAllocation中</span>
blurScript.forEach(blurredAllocation);
//把腳本結(jié)果存入位圖中 因?yàn)闉閚ative層渲染,所以結(jié)果需要復(fù)制到上層
blurredAllocation.copyTo(bitmap);
//Destroy everything to free memory
allocation.destroy();
blurredAllocation.destroy();
blurScript.destroy();
t.destroy();
return bitmap;
}
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Android自定義View仿QQ運(yùn)動步數(shù)效果
使用Fragment+ViewPager實(shí)現(xiàn)底部導(dǎo)航欄
Ubutu1604安裝colmap實(shí)現(xiàn)方法詳細(xì)教程
Android實(shí)現(xiàn)Recycleview懸浮粘性頭部外加右側(cè)字母導(dǎo)航
Android基于繪制緩沖實(shí)現(xiàn)煙花效果

