Android RenderScript
上回泰勞介紹了處理圖片特效的方法,這回要來介紹如何提升效能,也許很多人第一時間會想到 JNI,那確實是個不錯的選擇,不過泰勞想介紹一個有點不一樣 、 使用場合跟 JNI 不盡相同的 API,那就是 RenderScript! 參考資料: [Developers]RenderScript Overview [Developers]Type [Developers]Element [Developers]Allocation 使用 RenderScript 最大的優勢在於它可以實現「 平行運算 」,在擁有多核心處理器的裝置上速度一定會快上許多,同時它也支援 GPU。但因為是平行運算,有些場合是不能使用的,當然也有些場合特別適合,像是圖像處理就非常適合使用 RenderScript。 延續上一回的範例,這次改用 RenderScript 來實作負片效果,先準備一個簡單的 Context,作為主程式調用 RenderScript 的中繼站。 RenderScriptContext.java public class RenderScriptContext { // for RenderScript private RenderScript rs; // ScriptC 和底線為格式所需,Invert 是我的 RS 腳本名稱 private ScriptC_Invert scriptC; // 用於設定 RS 物件類型 private Type rType; // 將 Java 物件傳給 RS 處理的媒介 private Allocation allocIn; private Allocation allocOut; // private Bitmap outputBitmap; public RenderScriptContext(Context context, int width, int height) { // 初始化 RS 的 Context rs = RenderScript.create(con...