rongwei84n 版主
帖子
4110精华
0无忧币
7205 发短消息家园好友他的博客他的资源Android RenderScript初步发表于 2013-7-16 21:23 | 来自
51CTO网页[只看他] 楼主
(一) 前言
RenderScript是Android平台的一种类C脚本语言,Google虽然一直在之前的各个Android版本内置的动态墙纸中使用该技术实现3D图形特效,但一直未将其集成在公开发布的SDK中。
至Android3.0版本开始,SDK中已将RenderScript技术集成了进来,开发者可在Eclipse下开发基于RenderScript的3D应用,并在Android3.0版本以上的平板电脑中运行。
RenderScript是一种3D运行时框架,其提供构造3D场景的API函数,同时为最大程度的优化性能,还支持平台无关的shade语言。开发者不仅可以采用RenderScript技术来开发3D应用,还可以用来作特定的高性能数据处理。
关于RenderScript的详细内容大家可参见SDK:
http://developer.android.com/guide/topics/renderscript/index.htmlhttp://developer.android.com/guide/topics/renderscript/graphics.htmlhttp://developer.android.com/guide/topics/renderscript/compute.html(二) RenderScript如何使用
本文以Google提供的api demo HelloWorld为例,进行说明。这个程序的效果是根据用户在界面上的点击位置,绘制出"Hello World!" 字符串
首先,看下它的文件结构:
从上面可以看出,主要包含4个文件,那么我们就来一个个分析这4个文件。
1. helloworld.rs 自己的注释会写在里面01#pragma version(1)
02
03// Tell which java package name the reflected files should belong to
04// 这个rs 文件在gen目录下自动生成的包路径,
05//这里的话就是在gen目录,生成包com.example.android.rs.helloworld,然后生成相应的java文件
06#pragma rs java_package_name(com.example.android.rs.helloworld)
07
08// Built-in header with graphics API's
09// 导入库文件
10#include "rs_graphics.rsh"
11
12// gTouchX and gTouchY are variables that will be reflected for use
13// by the java API. We can use them to notify the script of touch events.
14// 定义字符串绘制的x,y值
15int gTouchX;
16int gTouchY;
17
18// This is invoked automatically when the script is created
19// 初始化方法,自动调用,初始时设置x,y为50,50
20void init() {
21 gTouchX = 50.0f;
22 gTouchY = 50.0f;
23}
24
25// 绘制方法,有点像draw方法或者libgdx的render方法
26int root(int launchID) {
27
28 // Clear the background color
29//清理背景颜色,参数估计是rgb,最后一个透明度值
30 rsgClearColor(0.0f, 0.0f, 0.0f, 0.0f);
31 // Tell the runtime what the font color should be
32//设置字体颜色和透明度
33 rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
34 // Introuduce ourselves to the world by drawing a greeting
35 // at the position user touched on the screen
36//根据x,y,写出"Hello World!" 字符串
37 rsgDrawText("Hello World!", gTouchX, gTouchY);
38
39 // Return value tells RS roughly how often to redraw
40//这个,不知道。。。
41 // in this case 20 ms
42 return 20;
43}
ok.. 我们编写这个文件之后,就会在gen目录下,生成一个ScriptC_helloworld.java文件,如:
2. 编写HelloWorldRS.java文件01package com.example.android.rs.helloworld;
02
03import android.content.res.Resources;
04import android.renderscript.*;
05
06// This is the renderer for the HelloWorldView
07public class HelloWorldRS {
08private Resources mRes;
09private RenderScriptGL mRS;
10
11//这是对上面写的helloworld.rs对象的封装
12private ScriptC_helloworld mScript;
13
14public HelloWorldRS() {
15}
16
17// This provides us with the renderscript context and resources that
18// allow us to create the script that does rendering
19public void init(RenderScriptGL rs, Resources res) {
20mRS = rs;
21mRes = res;
22initRS();
23}
24
25//设置一个接口,以便改变helloworld.rs的x,y值
26public void onActionDown(int x, int y) {
27mScript.set_gTouchX(x);
28mScript.set_gTouchY(y);
29}
30
31private void initRS() {
32mScript = new ScriptC_helloworld(mRS, mRes, R.raw.helloworld);
33mRS.bindRootScript(mScript);
34}
35}
第2步的话,我觉得我们可以简单的理解成对第1步生成ScriptC_helloworld.java对象的封装
3. HelloWorldView.java
我们Android里面显示的话,肯定是View对象,RenderScript为我们编写了一个类RSSurfaceView.java,然后我们自己的View只要扩展自这个RSSurfaceView.java即可。01package com.example.android.rs.helloworld;
02
03import android.renderscript.RSSurfaceView;
04import android.renderscript.RenderScriptGL;
05
06import android.content.Context;
07import android.view.MotionEvent;
08
09public class HelloWorldView extends RSSurfaceView {
10 // Renderscipt context
11 private RenderScriptGL mRS;
12 // Script that does the rendering
13 private HelloWorldRS mRender; //刚才第2步定义的HelloWorldRS对象
14
15 public HelloWorldView(Context context) {
16 super(context);
17 ensureRenderScript();
18 }
19
20 private void ensureRenderScript() {
21 if (mRS == null) {
22 // Initialize renderscript with desired surface characteristics.
23 // In this case, just use the defaults
24 RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
25 mRS = createRenderScriptGL(sc);
26 // Create an instance of the script that does the rendering
27 mRender = new HelloWorldRS();
28 mRender.init(mRS, getResources());
29 }
30 }
31
32 <a href="
http://home.51cto.com/index.php?s=/space/5017954" target="_blank">@Override</a>
33 protected void onAttachedToWindow() {
34 super.onAttachedToWindow();
35 ensureRenderScript();
36 }
37
38 <a href="
http://home.51cto.com/index.php?s=/space/5017954" target="_blank">@Override</a>
39 protected void onDetachedFromWindow() {
40 // Handle the system event and clean up
41 mRender = null;
42 if (mRS != null) {
43 mRS = null;
44 destroyRenderScriptGL();
45 }
46 }
47
48//onTouchEvent方法,当用户点击屏幕的时候,把点击的参数x,y告诉helloworld.rs的x,y值
49 <a href="
http://home.51cto.com/index.php?s=/space/5017954" target="_blank">@Override</a>
50 public boolean onTouchEvent(MotionEvent ev) {
51 // Pass touch events from the system to the rendering script
52 if (ev.getAction() == MotionEvent.ACTION_DOWN) {
53 mRender.onActionDown((int)ev.getX(), (int)ev.getY());
54 return true;
55 }
56
57 return false;
58 }
59}
这里,主要是定义了一个自己的View,然后重写了一些生命周期方法,最后是onTouchEvent方法,当用户点击屏幕的时候,把点击的x,y值告诉helloworld.rs的x,y值
第4步
到了最后一步了,当然是定义我们的Activity01package com.example.android.rs.helloworld;
02
03import android.app.Activity;
04import android.os.Bundle;
05
06// Renderscript activity
07public class HelloWorld extends Activity {
08
09 // Custom view to use with RenderScript
10 private HelloWorldView mView;
11
12 <a href="
http://home.51cto.com/index.php?s=/space/5017954" target="_blank">@Override</a>
13 public void onCreate(Bundle icicle) {
14 super.onCreate(icicle);
15
16 // Create our view and set it as the content of our Activity
17 mView = new HelloWorldView(this); //实例化我们定义的View
18 setContentView(mView); //把设置成界面显示
19 }
20
21 <a href="
http://home.51cto.com/index.php?s=/space/5017954" target="_blank">@Override</a>
22 protected void onResume() {
23 // Ideally an app should implement onResume() and onPause()
24 // to take appropriate action when the activity loses focus
25 super.onResume();
26 mView.resume();
27 }
28
29 <a href="
http://home.51cto.com/index.php?s=/space/5017954" target="_blank">@Override</a>
30 protected void onPause() {
31 // Ideally an app should implement onResume() and onPause()
32 // to take appropriate action when the activity loses focus
33 super.onPause();
34 mView.pause();
35 }
36
37}
这一步也是非常好理解,把View设置成Activity显示的界面,然后就是一些生命周期方法。
嗯,到这里就全部结束了。。
这个例子是Google自带的sample, 但是发现好像使用RenderScript的人比较少。是不是还不太流行呢。。
本帖最后由 rongwei84n 于 2013-7-17 10:35 编辑
更多
楼主关注
apk应用服务器地址【提问】如何在Linux下编译Android项目而生成一个apkFragmentPagerAdapter中的getItem()方法 第二次进入该fragment的时候 不调用了 这是怎么回事?android板块的兄弟姐们们新手求助!!请大人赐教进程间通讯版主推荐
在Service如何保存数据?续:Laucher2 桌面icon 未读消息刷新 思路Android桌面悬浮窗效果实现,仿360手机卫士悬浮窗效果Android Activity定制需要的Title遇到问题了:已经强制关闭socket了,in.read(buffer)一直阻塞,怎么办?Android学习之 AsyncTask工信部-弱电项目经理技术资格认证 |
51CTO公开课,招讲师喽! |
【有奖活动】职场五日谈rongwei84n 版主
帖子
4110精华
0无忧币
7205 发短消息家园好友他的博客他的资源发表于 2013-7-16 21:26 | 来自
51CTO网页[只看他] 沙发
RenderScript听说是一个老外牛人写的,后来被Google收购了,好像用的比较少。。:)1