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

Android 使用 RenderScript 實作動態高斯模糊效果

編輯:關於 Android 程式設計

寫在前面

如今越來越多的 App 都加入了模糊效果,也就是所謂的高斯模糊。大家都知道,在 Android 平台上進行模糊渲染是一項相當耗 CPU、也相當耗時的操作,一旦處理不好,畫面卡頓幾乎在所難免。一般來說,從效率角度考量,渲染圖片最好的方式首選 OpenGL,其次是 C/C++,而用 Java 實作的效率最低、速度也最慢。

不過自從 Android 推出 RenderScript 之後,我們便多了一個選擇。實測顯示,RenderScript 的渲染效率與 C/C++ 相當,但相較於使用 JNI,RenderScript 的開發卻簡單許多。同時,Android 團隊也提供了 RenderScript 的支援庫,讓較低版本的 Android 平台同樣能使用它。

在正式使用前,還有一點需要留意:對圖片做模糊處理時,盡量不要直接使用原始解析度的圖片,最好先按比例縮小,這樣渲染效率更高、速度也更快。

什麼是 RenderScript

RenderScript 是一種低階的高效能程式語言,主要用於 3D 渲染與運算密集型的計算工作。長久以來,Android 在繪圖效能上的表現一直差強人意,直到引入 NDK 才有所改善;而在 Honeycomb 版本中,RenderScript 這個重量級特性於 Framework 層問世,大幅提升了 Android 執行原生程式碼的效能與運算能力。目前網路上介紹 RenderScript 的文章還不多,若有興趣深入了解,建議直接查閱官方文件。

動態模糊的實作

使用前,需先在 Module 的 build.gradle 中做如下設定:

MainActivity.java

 package com.jackie.blurimage;
 import android.graphics.Bitmap;
 import android.graphics.BitmapFactory;
 import android.os.Bundle;
 import android.support.v7.app.AppCompatActivity;
 import android.widget.ImageView;
 import android.widget.SeekBar;
 import android.widget.TextView;
 public class MainActivity extends AppCompatActivity {
 private ImageView mBlurImage, mOriginImage;
 private SeekBar mSeekBar;
 private TextView mSeekProgress;
 private int mAlpha;
 @Override protected void onCreate(Bundle savedInstanceState) {
 super.onCreate(savedInstanceState);
 setContentView(R.layout.activity_main);
 initView();
 initData();
 initEvent();
 }
 private void initView() {
 mBlurImage = (ImageView) findViewById(R.id.blur_image);
 mOriginImage = (ImageView) findViewById(R.id.origin_image);
 mSeekBar = (SeekBar) findViewById(R.id.seek_bar);
 mSeekProgress = (TextView) findViewById(R.id.seek_progress);
 }
 private void initData() {
 // 獲取圖片
 Bitmap originBitmap = BitmapFactory.decodeResource(getResources(), R.drawable.blur);
 Bitmap blurBitmap = BlurUtils.blur(this, originBitmap);
 // 填充模糊後的圖像和原圖
 mBlurImage.setImageBitmap(blurBitmap);
 mOriginImage.setImageBitmap(originBitmap);
 }
 private void initEvent() {
 mSeekBar.setMax(100);
 mSeekBar.setOnSeekBarChangeListener(new SeekBar.OnSeekBarChangeListener() {
 @Override public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
 mAlpha = progress;
 mOriginImage.setAlpha((int) (255 - mAlpha * 2.55));
 mSeekProgress.setText(String.valueOf(mAlpha));
 }
 @Override public void onStartTrackingTouch(SeekBar seekBar) {
 }
 @Override public void onStopTrackingTouch(SeekBar seekBar) {
 }
 });
 }
 } 

activity_main.xml

 <?xml version="1.0" encoding="utf-8"?>
 <LinearLayout xmlns:android="https://schemas.android.com/apk/res/android"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:orientation="vertical">
 <FrameLayout
 android:layout_width="match_parent"
 android:layout_weight="1"
 android:layout_height="0dp">
 <ImageView
 android:id="@+id/blur_image"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:scaleType="centerCrop"
 android:src="@drawable/blur"/>
 <ImageView
 android:id="@+id/origin_image"
 android:layout_width="match_parent"
 android:layout_height="match_parent"
 android:scaleType="centerCrop"/>
 </FrameLayout>
 <LinearLayout
 android:layout_width="match_parent"
 android:layout_height="80dp"
 android:orientation="vertical">
 <SeekBar
 android:id="@+id/seek_bar"
 android:layout_width="match_parent"
 android:layout_height="wrap_content"
 android:layout_marginLeft="16dp"
 android:layout_marginRight="16dp"
 android:layout_marginTop="@dimen/activity_vertical_margin"/>
 <TextView
 android:id="@+id/seek_progress"
 android:layout_width="wrap_content"
 android:layout_height="wrap_content"
 android:layout_gravity="center"
 android:text="0"
 android:textSize="24sp"/>
 </LinearLayout>
 </LinearLayout> 

從上面的程式碼不難看出,我們在 FrameLayout 上疊放了兩張圖片,一張是模糊後的底圖,一張是原圖,再透過動態調整原圖的透明度,營造出模糊的漸變動態效果。

BlurUtils.java

 package com.jackie.blurimage;
 import android.content.Context;
 import android.graphics.Bitmap;
 import android.renderscript.Allocation;
 import android.renderscript.Element;
 import android.renderscript.RenderScript;
 import android.renderscript.ScriptIntrinsicBlur;
 /**
 * Created by Jackie on 2017/1/21.
 * 高斯模糊工具類
 */
 public class BlurUtils {
 /** 圖片縮放比例 */
 private static final float SCALE_DEGREE = 0.4f;
 /** 最大模糊度(在0.0到25.0之間) */
 private static final float BLUR_RADIUS = 25f;
 /** 模糊圖片
 * @param context 上下文
 * @param bitmap 需要模糊的圖片
 * @return 模糊處理後的圖片 */
 public static Bitmap blur(Context context,Bitmap bitmap) {
 //計算圖片縮小的長寬
 int width = Math.round(bitmap.getWidth() * SCALE_DEGREE);
 int height = Math.round(bitmap.getHeight() * SCALE_DEGREE);
 //將縮小後的圖片作為預渲染的圖片
 Bitmap inputBitmap = Bitmap.createScaledBitmap(bitmap, width, height, false);
 //創建一張渲染後的輸入圖片
 Bitmap outputBitmap = Bitmap.createBitmap(inputBitmap);
 //創建RenderScript內核對象
 RenderScript renderScript = RenderScript.create(context);
 //創建一個模糊效果的RenderScript的工具對象
 ScriptIntrinsicBlur scriptIntrinsicBlur = ScriptIntrinsicBlur.create(renderScript, Element.U8_4(renderScript));
 /**
 * 由於RenderScript並沒有使用VM來分配內存,所以需要使用Allocation類來創建和分配內存空間。
 * 創建Allocation對象的時候其實內存是空的,需要使用copyTo()將數據填充進去。
 */
 Allocation inputAllocation = Allocation.createFromBitmap(renderScript, inputBitmap);
 Allocation outputAllocation = Allocation.createFromBitmap(renderScript, outputBitmap);
 //設置渲染的模糊程度,25f是最大模糊度
 scriptIntrinsicBlur.setRadius(BLUR_RADIUS);
 //設置ScriptIntrinsicBlur對象的輸入內存
 scriptIntrinsicBlur.setInput(inputAllocation);
 //將ScriptIntrinsicBlur輸出數據保存到輸出內存中
 scriptIntrinsicBlur.forEach(outputAllocation);
 //將數據填充到Allocation中
 outputAllocation.copyTo(outputBitmap);
 return outputBitmap;
 }
 } 

效果如下圖所示:

透過 RenderScript,我們得以兼顧開發的簡潔與渲染的高效,讓高斯模糊效果在 Android 應用中輕鬆落地。希望這份分享能對大家有所幫助。

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