LoginSignup
21
21

More than 5 years have passed since last update.

RenderScriptのv8 Support LibraryをAndroid Studioで使う

Last updated at Posted at 2014-10-21

2015年1月7日時点では公式ドキュメントによるとAndroid Studioではv8 Support Library (RenderScriptの下位互換ライブラリー)はサポートされていないことになっていますが、実は使えるようになってました!
方法は下のように、android.defaultConfigブロックに1行付け加えるだけ。

android {
    ...
    defaultConfig {
        ...
        renderscriptSupportModeEnabled true
    }
}

これでビルドすれば android.support.v8.renderscript パッケージが使えるようになります。

さくっと動作確認したい人のためのサンプル

Activity

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.os.Bundle;
import android.support.v8.renderscript.Allocation;
import android.support.v8.renderscript.Element;
import android.support.v8.renderscript.RenderScript;
import android.support.v8.renderscript.ScriptIntrinsicBlur;
import android.view.View;
import android.widget.ImageView;


public class MyActivity extends Activity {
    RenderScript rs;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_my);
        rs = RenderScript.create(this);
    }

    public void process(View img) {
        Bitmap bmp = ((BitmapDrawable) ((ImageView) img).getDrawable()).getBitmap();
        Allocation alloc = Allocation.createFromBitmap(rs, bmp);
        ScriptIntrinsicBlur blur = ScriptIntrinsicBlur.create(rs, Element.RGBA_8888(rs));
        blur.setInput(alloc);
        blur.forEach(alloc);
        alloc.copyTo(bmp);
        ((ImageView) img).setImageBitmap(bmp);
    }
}

res/layout/activity_my.xml

<ImageView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:src="@drawable/ic_launcher"
    android:onClick="process"
    android:layout_gravity="center" />

Before
After.png

21
21
1

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
21
21