从一个小例子开始
Step1下载安装gomobile工具
首先安装1.5或更高版本的Go语言环境,所有这些都必须拥有。
然后需要通过科学上网方法去下载gomobile命令:
go get golang.org/x/mobile/cmd/gomobile
接着需要通过gomobile init去下载Android NDK的部分toolchain,执行下面的命令:
gomobile init
可以去android网站上下载一份最新的NDK以备用,将来遇上缺什么文件的话,就从NDK里面复制就是了。
下载地址在:https://developer.android.com/ndk/downloads/index.html
一切就绪了之后,先运行个例子试一试吧:
gomobile build -target=android golang.org/x/mobile/example/basic
成功之后,就生成了ba。
可以通过gomobile install命令安装这个apk:
gomobile install golang.org/x/mobile/example/basic
当然啦,apk都生成了,直接用adb install就是了。
basic例子的功能很简单,红底上画一个绿色渐变的三角形,随着手指的点击事件,三角形的直角顶点的位置跟着一起走。
step2basic例子分析
纯用Go写的Android代码,与Android NDK用C++写的代码异曲同工,都是用OpenGL/ES的命令直接作画的方式。
我们来看下这个不长的例子,涉及到Go语言相关或者是OpenGL相关的不理解的不要紧,后面我们都会介绍,我们先过几个例子找找感觉:
2.1引用包
第一步是引用了一堆要用的包,跟app相关,event相关,openGL相关。
package mainimport ( "encoding/binary""log"
"golang.org/x/mobile/app"
"golang.org/x/mobile/event/lifecycle"
"golang.org/x/mobile/event/paint"
"golang.org/x/mobile/event/size"
"golang.org/x/mobile/event/touch"
"golang.org/x/mobile/exp/app/debug"
"golang.org/x/mobile/exp/f32"
"golang.org/x/mobile/exp/gl/glutil"
"golang.org/x/mobile/gl")
2.2定义变量
基本是OpenGL相关的几个值,还有颜色,X和Y的位置。
var (images *glu
fps *debug.FPS
program gl.Program
position gl.Attrib
offset gl.Uniform
color gl.Uniform
buf gl.Buffer
green float32
touchX float32
touchY float32
)
2.3主函数
先向a中注册我们的主函数。
func main() {a(func(a a) { var glctx gl.Context
var sz
下面就是一个典型的消息驱动的循环,app将事件传给我们的主函数,我们根据事件来处理。GUI系统的代码的框架都差不多哈。
for e := range a.Events() { switch e := a.Filter(e).(type) { case li: switch e.Crosse) { case li:glctx, _ = e.DrawContext.)
onStart(glctx)
a.Send{}) case li:
onStop(glctx)
glctx = nil
} case :
sz = e
touchX = float32 / 2)
touchY = float32 / 2) case : if glctx == nil || e.External { // As we are actively painting as fast as
// we can (usually 60 FPS), skip any paint
// events sent by the system.
continue
}
onPaint(glctx, sz)
a.Publish() // Drive the animation by preparing to paint the next frame
// after this one is shown.
a.Send{}) case :
touchX = e.X
touchY = e.Y
}
}
})
}
后面基本上就需要一些OpenGL的知识了:
func onStart(glctx gl.Context) { var err error
通过glu创建一个程序,OpenGL里程序是将顶点着色器和片段着色器绑定在一起的实体。
program, err = glu(glctx, vertexShader, fragmentShader) if err != nil { log.Printf("error creating GL program: %v", err) return}
buf = glc()
glc, buf)
glc, triangleData, gl.STATIC_DRAW)
position = glc(program, "position")
color = glc(program, "color")
offset = glc(program, "offset")
images = glu(glctx)
fps = debug.NewFPS(images)
}
func onStop(glctx gl.Context) {
glc(program)
glc(buf)
()
images.Release()
}
onPaint的过程,是个标准的OpenGL绘制三角形的流程:
func onPaint(glctx gl.Context, sz ) {glc(1, 0, 0, 1)
glc)
glc(program)
green += 0.01
if green > 1 {
green = 0
}
glc(color, 0, green, 0, 1)
glc(offset, touchX/float32), touchY/float32))
glc, buf)
glc(position)
glc(position, coordsPerVertex, gl.FLOAT, false, 0, 0)
glc, 0, vertexCount)
glc(position)
(sz)
}
下面是三角形的三个顶点的坐标:
var triangleData = , 0.0, 0.4, 0.0, // top left0.0, 0.0, 0.0, // bottom left
0.4, 0.0, 0.0, // bottom right)const (
coordsPerVertex = 3
vertexCount = 3)
最后是我们创建顶点着色器时所使用的GLSL:
const vertexShader = `#version 100
uniform vec2 offset;attribute vec4 position;void main() { // offset comes in with x/y values between 0 and 1.
// position bounds are -1 to 1.
vec4 offset4 = vec4*o, 1.0-2.0*o, 0, 0);
gl_Position = position + offset4;
}`
const fragmentShader = `#version 100
precision mediump float;uniform vec4 color;void main() { gl_FragColor = color;}`
step3对照C++所写的native activity
我们来对照下,Android用NDK来写这种native的activity都需要做什么吧。总的代码比Go版本的还是长不少,我们就挑个Android的主函数看一下:
void android_main(struct android_app* state) { struct engine engine; // Make sure glue isn't stripped.app_dummy(); memset(&engine, 0, sizeof(engine));
state->userData = &engine;
state->onAppCmd = engine_handle_cmd;
state->onInputEvent = engine_handle_input;
engine.app = state;
... if (state->savedState != NULL) { // We are starting with a previous saved state; restore from it.
engine.state = *(struct saved_state*)state->savedState;
} // loop waiting for stuff to do.
while (1) { // Read all pending events.
int ident; int events; struct android_poll_source* source; // If not animating, we will block forever waiting for events.
// If animating, we loop until all events are read, then continue
// to draw the next frame of animation.
while ((ident=ALooper_pollAll ? 0 : -1, NULL, &events,
(void**)&source)) >= 0) { // Process this event.
if (source != NULL) {
source->process(state, source);
}
... // Check if we are exiting.
if (state->destroyRequested != 0) {
engine_term_display(&engine); return;
}
} if ) { // Done with events; draw next animation frame.
engine. += .01f; if (engine. > 1) {
engine. = 0;
} // Drawing is throttled to the screen update rate, so there
// is no need to do timing here.
engine_draw_frame(&engine);
}
}
}//END_INCLUDE(all)
Android例子中用的OpenGL比上面Go的例子要简单一些:
static void engine_draw_frame(struct engine* engine) { if (engine->display == NULL) { // No display.return;
} // Just fill the screen with a color.
glClearColor(((float)engine->)/engine->width, engine->,
((float)engine->)/engine->height, 1);
glClear(GL_COLOR_BUFFER_BIT);
eglSwapBuffers(engine->display, engine->surface);
}
小结
上面的例子,除去了OpenGL的知识以外,我们不需要知道任何跟Android系统相关的知识。
别小看了这段小小的代码,在gomobile的支持下,它可是跨平台的哟。
下一期,我们讨论更加激动人心的话题,如何从Android的Java代码调用Go的代码。
更多深度技术内容,请关注云栖社区微信公众号:yunqiinsight。