看代碼的時候,看到了其中有.rs結尾的檔案,不是很明白,還有RenderScript類,看的一臉蒙蔽,不知所云,然後百度了一下,收貨還真不少,這東西在圖形處理這塊用處挺大的。
今天先說說ScriptIntrinsicBlur,這個類不需要定義rs檔案,從這個Intrinsic單詞可以看的出來,它是API17以後內建的類,專門用來處理映像的,讓圖片變模糊。
public static Bitmap blurBitmap(Bitmap bitmap, float radius, Context context) { //建立渲染指令碼上下文 RenderScript rs = RenderScript.create(context); //為位元影像分配記憶體 Allocation allocation = Allocation.createFromBitmap(rs, bitmap); Type t = allocation.getType(); //用同樣的類型建立記憶體,一般用這兩種方式建立 <span style="font-family: Arial, Helvetica, sans-serif;">Allocation</span> Allocation blurredAllocation = Allocation.createTyped(rs, t); //建立高斯渲染指令碼 ScriptIntrinsicBlur blurScript = ScriptIntrinsicBlur.create(rs, Element.U8_4(rs)); //設定模糊半徑 (maximum 25.0) blurScript.setRadius(radius); //為指令碼設定輸入參數 blurScript.setInput(allocation); //呼叫指令碼 結果存入 <span style="font-family: Arial, Helvetica, sans-serif;">blurredAllocation中</span> blurScript.forEach(blurredAllocation); //把指令碼結果存入位元影像中 因為為native層渲染,所以結果需要複製到上層 blurredAllocation.copyTo(bitmap); //Destroy everything to free memory allocation.destroy(); blurredAllocation.destroy(); blurScript.destroy(); t.destroy(); return bitmap; }
以上就是本文的全部內容,希望對大家的學習有所協助,也希望大家多多支援雲棲社區。