LoginSignup
25
24

More than 5 years have passed since last update.

SupportLibrary-v8のRenderScript APIを使う

Posted at

RenderScriptにAPI level17から提供されたScriptIntrinsicBlurを下位バージョンでも利用しようとしたときのメモ。

RenderScriptって

RenderScriptについては、ぼくはこのブログ記事でなんとなーく雰囲気をつかんだ程度です。
GPUで処理してくれる描画エンジン。

[翻訳]RenderScriptについてAndroidDeveloperBlogを読んでみた。 | superdry memorandum :-D

画像処理に使えるAPIもあって、今回はScriptIntrinsicBlurを使いたかったのです。

RenderScript自体はAPI level11から使えるけど、Android Developersによると

We strongly recommend using the Support Library APIs for accessing RenderScript because they include the latest improvements to the RenderScript compute framework and provide a wider range of device compatibility.

とのことなので、Honeycomb以上をターゲットにする場合でもSupport-Library経由で利用した方が幸せなようです。

ScriptIntrinsicBlurの使い方

実際に使用したコードは、
Android: fast bitmap blur? | stackoverflow.com
で見つけた、以下の様なものです。

final RenderScript rs = RenderScript.create( myAndroidContext );
final Allocation input = Allocation.createFromBitmap( rs, photo, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT );
final Allocation output = Allocation.createTyped( rs, input.getType() );
final ScriptIntrinsicBlur script = ScriptIntrinsicBlur.create( rs, Element.U8_4( rs ) );
script.setRadius( myBlurRadius /* e.g. 3.f */ );
script.setInput( input );
script.forEach( output );
output.copyTo( photo );

Bitmapをぼや~っとさせることが簡単かつ高速(たぶん)にできます。

SupportLibrary-v8を使う

RenderScript - Accessing RenderScript APIs | Android Developer

を見ながら進めていきます。

ライブラリーの追加

ライブラリは実は追加しなくてもいいかもしれない(後述)のですが、
自分が見た限りeclipse上でのビルドパスが通らなかったので、この手段を使いました。
公式ドキュメントでは特に記載がありません。

/build-tools/19.0.0/renderscript/lib/renderscript-v8.jar

をプロジェクトのlibsへコピー。

project.propertiesの編集

以下の項目を追加します。

renderscript.target=19
renderscript.support.mode=true
sdk.buildtools=19

renderscript.target

Specifies the bytecode version to be generated. We recommend you set this value the highest available API level and set renderscript.support.mode to true. Valid values for this setting are any integer value from 11 to the most recently released API level. If your minimum SDK version specified in your application manifest is set to a higher value, this value is ignored and the target value is set to the minimum SDK version.

その時点で一番新しいAPIレベルを指定することをおすすめするぜ!とのことなので19(KitKat)を設定。
ただし、AndroidManifestにminiumSDKversionの指定がある場合は、この値は無視して、minimumSDKの値が使用されるようなので、19である必要はなく、むしろおとなしくManifestに合わせた値にすればいいようにも思います。

renderscript.support.mode

Specifies that the generated bytecode should fall back to a compatible version if the device it is running on does not support the target version.

RenderScript非対応バージョンのデバイスで実行時に、互換bytecodeを生成する設定。
SupportLibraryを利用するのでtrueに設定。

sdk.buildtools

The version of the Android SDK build tools to use. This value should be set to 18.1.0 or higher. If this option is not specified, the highest installed build tools version is used. You should always set this value to ensure the consistency of builds across development machines with different configurations.

利用するビルドツールのバージョン。18.1.0以上である必要があるようです。
設定しないとインストール済みの最新のものを利用するみたい。
ここは現時点で最新の19を指定。

v8のパッケージを追加する

利用するクラスにインポート宣言を追加。
当然、使っているクラスに応じて変わります。
すでにフレームワークのRenderScriptを利用している場合は置き換える。

import android.support.v8.renderscript.*;

libsに追加する必要はあるのか問題

プロジェクトのlibsにライブラリを自分で追加したけど、apkをビルドする時はクラスパスに自動的に追加しているような動きがantのログには出ていた。
なので、環境によっては追加しなくてもいいのかもしれない。。

-build-setup:
     [echo] Resolving Build Target for sample-project...
[gettarget] Project Target:   Android 4.3
[gettarget] API level:        18
-- 中略 -- 
[dependency] ------------------
[dependency] Renderscript support mode: Adding renderscript-v8.jar to the classpath.
25
24
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
25
24