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

用RenderScript快速打造Android高斯模糊效果

編輯:關於 Android 程式設計

前言

近年來,模糊效果已經成為眾多App界面設計中的常客,而這背後靠的正是高斯模糊。眾所周知,在Android平台上進行模糊渲染是一件相當吃CPU又頗耗時的任務,稍有不慎就會造成畫面卡頓。一般來說,兼顧效率的話,渲染圖片首選OpenGL,其次才是C/C++,而用純Java代碼實現的 Performance 最不理想、速度也最慢。

所幸自從Google推出RenderScript之後,開發者便多了一個不錯的選擇。實測表明,RenderScript的渲染效率與C/C++旗鼓相當,但相較於走JNI那套流程,使用RenderScript要簡單得多。更貼心的是,Android團隊還提供了RenderScript的支持庫,讓低版本的系統也能享受這項能力。

動手之前有一點值得留意:處理模糊時盡量別直接拿原尺寸分辨率的圖片下手,先把圖片按比例縮小,這樣渲染效率更高、速度也更快。

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; } } 

最終的效果如下圖所示:

以上就是本文的全部內容,希望對大家的學習有所幫助。

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