Android RenderScript 的使用基礎篇

 準備上4.0項目,最近接觸API 的時候看到了RenderScript 。經過運行google 那幾個項目發現這個真是一個很是不錯的API,可是google 所提供的文檔很是少,有助於瞭解計算機圖形計算,雖然咱們作應用編程的不必定要涉及這塊,但涉獵一下老是不錯的。java

RenderScript 是Android 平臺上的一種類C腳本語言。要在咱們程序上實現RenderScript 渲染功能,最少須要實現的幾個功能以下,以一個Hello Wold 爲例:android

 

  1. helloworld.rs 實現RenderScript 的代碼
  2. RenderScriptRS.java 一個輔助類,簡化操做RenderScript (非必要) 
  3. RenderScriptView.java  一個繼承於RSSurfaceView的類,用於顯示RenderScript 的渲染或者用來處理用戶的觸摸等視圖
  4. RenderScriptHelloWorldActivity.java 這個不用多說,Android 應用程序的界面類

     

當建立一個RenderScript 的項目的時候,必須在包名下建立一個以rs爲後綴的文件(位於咱們Activity 同包名下),如本篇文章使用的helloworld.rs文件.編程

#pragma version(1)
#pragma rs java_package_name(com.xuzhi.rs.helloworld)


#include "rs_graphics.rsh"

int gTouchX ;
int gTouchY ;

void init(){
    gTouchX=50.0f ;
    gTouchY=50.0f ;
}

//application main 
int root(void){
    rsgClearColor(0.0f,1.0f,0.0f,0.0f) ;//顏色更改成no red,full green,no blue,no opacity ,的RGBA值
    rsgFontColor(1.0f,0.0f,1.0f,1.0f) ;//設置字體顏色
    rsgDrawText("my first renderscript application",gTouchX,gTouchY) ;//根據應用傳上來的x,y 將字畫在屏幕對應的座標上
    rsDebug("======my renderscript debug========",rsUptimeMillis()) ;//打印日誌
    return 20 ;

 註解:app

#pragma:簡單的能夠理解告訴或者通知編譯器編譯這個文件時須要的參數或者指定編譯的版本等,跟編譯器有關。ide

void init():這個函數做一些初始化動做函數

int root(viod):程序入口,根據返回值(ms)做刷新。好比上面寫的20至關於每20毫秒刷新一次。post

      root 函數下rsgClearColor,rsgFontColor,rsgDrawText都位於rs_graphics.rsh頭文件下。字體

       rsDebug是打印日誌函數,位於rs_core.rsh頭文件。this

   在上面的代碼中,日誌將己每20毫秒打印一次這樣的日誌:03-01 11:09:18.289: D/RenderScript(1838): ======my renderscript debug======== 600462  0x9298egoogle

    rsUptimeMillis則定義在rs_time.rsh頭文件中。

目前有關於RenderScript調用的函數位於咱們SDK目錄下(platforms/android-*[目前11-15]/renderscript/include)目錄裏面的頭文件中。 

 

好了,rs文件己經編寫完成,接着往下走。 

 下一步,爲了方便調用RenderScript 這裏建立了一個輔助類,RenderScriptRS.java。

 

  /**
 * Render Script help 
 *  @author  terry
 *
 
*/
public  class RenderScriptRS {

     private RenderScriptGL mRS;
    
     private ScriptC_hellowold mScript;
    
     public RenderScriptRS(RenderScriptGL rs,Resources resource) {
         //  TODO Auto-generated constructor stub
        mRS=rs;
        mScript= new ScriptC_helloworld(mRS, resource, R.raw.hellowold);
        mRS.bindRootScript(mScript);  //綁定腳本
    }
    
     /**
     * 傳入座標
     * 
@param  x
     * 
@param  y
     
*/
     public  void onActionDown( int x, int y){
        mScript.set_gTouchX(x); 
        mScript.set_gTouchY(y);
    }
}

目前咱們所作的一切都是要使用到的RenderScriptGL(RenderScript 圖形的衍生),實例化RenderScriptGL並綁定腳本。若是你的開發ADT高於或者是1.5的話Eclipse 會根據你的rs文件生成ScriptC_你的rs文件爲名的java類。如上面代碼的ScriptC_helloworld同樣,而且在資源目錄res/raw生成一個.bc的文件,在實例化ScriptC_hellowold會須要傳進去。

 

 下一步,建立RenderScriptView.java,讓其能夠顯示在界面上。

import android.content.Context;
import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScriptGL;
import android.renderscript.RenderScriptGL.SurfaceConfig;
import android.view.MotionEvent;

public  class RenderScriptView  extends RSSurfaceView {

     private RenderScriptGL mRS;

     private RenderScriptRS mRender;

     public RenderScriptView(Context context) {
         super(context);
         //  TODO Auto-generated constructor stub
        initRenderScript();
    }

     /**
     * 初始化RenderScriptRS
     
*/
     private  void initRenderScript() {
         if (mRS ==  null) {
            RenderScriptGL.SurfaceConfig config =  new SurfaceConfig();
            mRS = createRenderScriptGL(config);

            mRender =  new RenderScriptRS(mRS, getResources());

        }
    }

    @Override
     protected  void onDetachedFromWindow() {
         //  Handle the system event and clean up
        mRender =  null;
         if (mRS !=  null) {
            mRS =  null;
            destroyRenderScriptGL();
        }
    }

    @Override
     protected  void onAttachedToWindow() {
         //  TODO Auto-generated method stub
         super.onAttachedToWindow();
        initRenderScript();
    }

    @Override
     public  boolean onTouchEvent(MotionEvent event) {
         //  TODO Auto-generated method stub
         /**
         * 傳入點擊座標
         
*/
         if (event.getAction() == MotionEvent.ACTION_DOWN) {
            mRender.onActionDown(( int) event.getX(), ( int) event.getY());
             return  true;
        }

         return  false;
    }

 

 RSSurfaceView擴展自SurfaceView,用來顯示這些須要渲染的圖形界面。

 上述代碼的initRenderScript方法,若是RenderScriptGL 未實例化,就須要建立一個SurfaceConfig(這是用來控制深度的顏色,深度緩衝區等等,這裏使用默認),而且讓它實現自身的onTouchEvent事件,傳入x,y座標軸進入RenderScript,改變文本顯示的位置(跟隨鼠標的點擊點變化)。

最後的界面只須要讓它顯示出來便可:

public  class RenderScriptHelloWorldActivity  extends Activity {
     /**  Called when the activity is first created.  */
    @Override
     public  void onCreate(Bundle savedInstanceState) {
         super.onCreate(savedInstanceState);
        setContentView( new RenderScriptView( this));
    }

 這一系列的步驟能夠用一張圖來作說明,詳細的規劃了RenderScript 的圖形概述:

 這是一個簡單的DEMO,介紹了RenderScript 使用的一些基本步驟,詳細的使用後面會慢慢提到。最後經過上面的代碼能夠看到的運行效果以下:

 

 

 另外,後文還會提供一些詳細的文檔和例子。好比:

  • rs_graphics
  • rs_core
  • rs_math
  • rs_time

 上面這四個的頭文件的API。

代碼能夠在Android 4.0中找到,RenderScript->Helloworld。 注:模擬器沒法運行。

相關文章
相關標籤/搜索