Android教程網
  1. 首頁
  2. Android 技術
  3. Android 手機
  4. Android 系統教程
  5. Android 游戲

Android RenderScript 高斯模糊效果的實作

編輯:關於 Android 程式設計

在看程式碼的時候,我看到了一些以 .rs 結尾的檔案,還有 RenderScript 類,當時一頭霧水、不知所云,於是上網查了一下,收穫還真不少——這東西在圖形處理領域用途相當廣泛。

今天先來聊聊 ScriptIntrinsicBlur。這個類不需要自行定義 rs 檔,從 Intrinsic 這個單字不難看出,它是 API 17 以後內建的類,專門用來處理圖像、讓圖片變模糊的。

 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 >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 >blurredAllocation中</span> blurScript.forEach(blurredAllocation); //把腳本結果存入位圖中 因為為native層渲染,所以結果需要復制到上層 blurredAllocation.copyTo(bitmap); //Destroy everything to free memory allocation.destroy(); blurredAllocation.destroy(); blurScript.destroy(); t.destroy(); return bitmap; } 

以上就是本文的全部內容,希望對大家的學習有所幫助,也歡迎大家多多支持本站。

  1. 上一篇文章:
  2. 下一篇文章:
Copyright © Android教程網 All Rights Reserved