▼Androidメモ▼
はじめてのRenderScript
RenderScriptの簡単なプログラムを作成する。
ソースコード
HelloRS.java package net.npaka.hellors; import net.npaka.hellors.ScriptC_helloworld; import android.app.Activity; import android.content.Context; import android.os.Bundle; import android.renderscript.Allocation; import android.renderscript.RSSurfaceView; import android.renderscript.RenderScriptGL; import android.view.MotionEvent; import android.view.Window; import net.npaka.hellors.R; //はじめてのレンダースクリプト public class HelloRS extends Activity { private MyRSSurfaceView rsView; //アクティビティ生成時に呼ばれる @Override public void onCreate(Bundle bundle) { super.onCreate(bundle); requestWindowFeature(Window.FEATURE_NO_TITLE); //レンダースクリプトビューの生成 rsView=new HelloRSSurfaceView(this); setContentView(rsView); } //レンダースクリプトビュー public class MyRSSurfaceView extends RSSurfaceView { private RenderScriptGL rs; private ScriptC_helloworld script; private Allocation alloc; //コンストラクタ public MyRSSurfaceView(Context context) { super(context); ensureRenderScript(); } //レンダースクリプトの初期化 private void ensureRenderScript() { RenderScriptGL.SurfaceConfig sc=new RenderScriptGL.SurfaceConfig(); rs=createRenderScriptGL(sc); script=new ScriptC_helloworld(rs,getResources(),R.raw.helloworld); rs.bindRootScript(script); //文字列の指定 alloc=Allocation.createFromString(rs, "Hello RenderScript!", Allocation.USAGE_SCRIPT); script.set_gText(alloc); } //ウィンドウのアタッチ @Override protected void onAttachedToWindow() { super.onAttachedToWindow(); ensureRenderScript(); } //ウィンドウのデタッチ @Override protected void onDetachedFromWindow() { script=null; if (rs!=null) { rs=null; destroyRenderScriptGL(); } } //タッチ時に呼ばれる @Override public boolean onTouchEvent(MotionEvent ev) { int action=ev.getAction(); if (action==MotionEvent.ACTION_DOWN || action==MotionEvent.ACTION_MOVE) { //X座標とY座標の指定 script.set_gTouchX((int)ev.getX()); script.set_gTouchY((int)ev.getY()); return true; } return false; } } }
helloworld.rs //RenderScriptバージョンの指定 #pragma version(1) //パッケージ名の指定 #pragma rs java_package_name(net.npaka.hellors) //ヘッダのインクルード #include "rs_graphics.rsh" //グローバル変数 int gTouchX; int gTouchY; rs_allocation gText; //初期化 void init(){ gTouchX=0; gTouchY=24; } //実処理 int root() { //背景色の指定 rsgClearColor(1.0f,1.0f,1.0f,1.0f); //文字列色の指定 rsgFontColor(0.0f,0.0f,0.0f,1.0f); //文字列の描画 rsgDrawText(gText,gTouchX,gTouchY); return 50; }
−戻る−