{"id":8732,"date":"2023-09-14T11:07:18","date_gmt":"2023-09-14T03:07:18","guid":{"rendered":"https:\/\/www.zhidianwl.com\/zhidian\/?p=8732"},"modified":"2023-09-14T11:07:18","modified_gmt":"2023-09-14T03:07:18","slug":"golang%e5%8f%af%e4%bb%a5%e5%bc%80%e5%8f%91%e5%ae%89%e5%8d%93app%e5%90%97%ef%bc%9f","status":"publish","type":"post","link":"https:\/\/www.zhidianwl.com\/zhidian\/2023\/8732\/","title":{"rendered":"golang\u53ef\u4ee5\u5f00\u53d1\u5b89\u5353app\u5417\uff1f"},"content":{"rendered":"

Go\u8bed\u8a00\u53ef\u4ee5\u5f00\u53d1\u5b89\u5353\u5e94\u7528\u7a0b\u5e8f\uff0c\u4e3b\u8981\u501f\u52a9\u4e8e\u7b2c\u4e09\u65b9<\/p>\n

<\/figure>\n<\/p>\n

\u5e93\u548c\u5de5\u5177\uff0c\u5176\u4e2d\u8f83\u4e3a\u5e38\u7528\u7684\u662fgomobile\u548cgomobile bind\u3002<\/p>\n

gomobile\u662fGo\u8bed\u8a00\u5b98\u65b9\u63d0\u4f9b\u7684\u79fb\u52a8\u5f00\u53d1\u5de5\u5177\uff0c\u5b83\u5141\u8bb8Go\u5f00\u53d1\u4eba\u5458\u4f7f\u7528Go\u8bed\u8a00\u7f16\u5199\u5e94\u7528\u7a0b\u5e8f\uff0c\u5e76\u5c06\u5176\u7f16\u8bd1\u4e3aARM\u548cx86\u67b6\u6784\u7684\u672c\u5730\u5e93\uff0c\u7136\u540e\u901a\u8fc7jni\u8c03\u7528\u8be5\u672c\u5730\u5e93\uff0c\u5b9e\u73b0\u4e0eJava\u4ee3\u7801\u7684\u4e92\u64cd\u4f5c\u6027\u3002<\/p>\n

\u81f3\u4e8egomobile bind\uff0c\u5219\u53ef\u4ee5\u5c06Go\u8bed\u8a00\u7684\u51fd\u6570\u6216\u7ed3\u6784\u4f53\u7ed1\u5b9a\u4e3aJava\u6216Objective-C\u7684\u7c7b\u6216\u63a5\u53e3\uff0c\u4f7f\u5f97Java\u6216Objective-C\u7a0b\u5e8f\u53ef\u4ee5\u76f4\u63a5\u8bbf\u95eeGo\u8bed\u8a00\u7684\u529f\u80fd\u3002<\/p>\n

\u4ee5\u4e0b\u662f\u4e00\u4e2a\u7b80\u5355\u7684gomobile\u793a\u4f8b\uff1a<\/p>\n

“`<\/p>\n

package main<\/p>\n

import (<\/p>\n

“fmt”<\/p>\n

“golang.org\/x\/mobile\/app”<\/p>\n

“golang.org\/x\/mobile\/event\/lifecycle”<\/p>\n

“golang.org\/x\/mobile\/event\/paint”<\/p>\n

“golang.org\/x\/mobile\/event\/size”<\/p>\n

“golang.org\/x\/mobile\/gl”<\/p>\n

)<\/p>\n

func main() {<\/p>\n

app.Main(func(a app.App) {<\/p>\n

var glctx gl.Context<\/p>\n

var sz size.Event<\/p>\n

for e := range a.Events() {<\/p>\n

switch e := a.Filter(e).(type) {<\/p>\n

case lifecycle.Event:<\/p>\n

switch e.Crosses(lifecycle.StageVisible) {<\/p>\n

case lifecycle.CrossOn:<\/p>\n

glctx, _ = e.DrawContext.(gl.Context)<\/p>\n

onSurfaceCreated()<\/p>\n

a.Send(paint.Event{})<\/p>\n

case lifecycle.CrossOff:<\/p>\n

onSurfaceDestroyed()<\/p>\n

glctx = nil<\/p>\n

}<\/p>\n

case size.Event:<\/p>\n

sz = e<\/p>\n

case paint.Event:<\/p>\n

if glctx == nil || e.External {<\/p>\n

continue<\/p>\n

}<\/p>\n

onDrawFrame(sz)<\/p>\n

a.Publish()<\/p>\n

a.Send(paint.Event{})<\/p>\n

}<\/p>\n

}<\/p>\n

})<\/p>\n

}<\/p>\n

func onSurfaceCreated() {<\/p>\n

gl.ClearColor(1, 1, 1, 1)<\/p>\n

}<\/p>\n

func onSurfaceDestroyed() {<\/p>\n

}<\/p>\n

func onDrawFrame(sz size.Event) {<\/p>\n

gl.Clear(gl\u5b89\u5353app<\/a>.COLOR_BUFFER_BIT)<\/p>\n

w, h := sz.WidthPx, sz.HeightPx<\/p>\n

\u5b89\u5353app\u5f00\u53d1\u5de5\u5177<\/a> u := float32(w) \/ float32(h)<\/p>\n

gl.Viewport(0, 0, w, h)<\/p>\n

gl.MatrixMode(gl.PROJECTION)<\/p>\n

gl.LoadIdentity()<\/p>\n

gl.Orthof(-u, u, -1, 1, -1, 1)<\/p>\n

gl.MatrixMode(gl.MODELVIEW)<\/p>\n

gl.LoadIdentity()<\/p>\n

gl.Rotatef(float32(app.TouchX()), 0, 0, -1)<\/p>\n

gl.Rotatef(float32(app.TouchY()), 0, -1, 0)<\/p>\n

triangle.Draw()<\/p>\n

}<\/p>\n

“`<\/p>\n

\u4ee5\u4e0a\u4ee3\u7801\u4e3b\u8981\u901a\u8fc7\u8c03\u7528gomobile\u548cgl\u5e93\uff0c\u5b9e\u73b0\u4e86\u4e09\u89d2\u5f62\u7684\u7ed8\u5236\u3002\u5177\u4f53\u4f7f\u7528\u65b9\u6cd5\u53ef\u4ee5\u53c2\u8003gomobile\u7684\u6587\u6863\u548c\u793a\u4f8b\u3002<\/p>\n","protected":false},"excerpt":{"rendered":"

Go\u8bed\u8a00\u53ef\u4ee5\u5f00\u53d1\u5b89\u5353\u5e94\u7528\u7a0b\u5e8f\uff0c\u4e3b\u8981\u501f\u52a9\u4e8e\u7b2c\u4e09\u65b9\u5e93\u548c\u5de5\u5177\uff0c\u5176\u4e2d\u8f83\u4e3a\u5e38\u7528\u7684\u662fgomobile\u548cgomobile bind\u3002gomobile\u662fGo\u8bed\u8a00\u5b98\u65b9\u63d0\u4f9b\u7684\u79fb\u52a8\u5f00\u53d1\u5de5\u5177\uff0c\u5b83\u5141\u8bb8Go\u5f00\u53d1\u4eba\u5458\u4f7f\u7528Go\u8bed\u8a00<\/p>\n","protected":false},"author":15,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[1],"tags":[15275,15276,14,5309,84],"class_list":{"0":"post-8732","1":"post","2":"type-post","3":"status-publish","4":"format-standard","5":"hentry","6":"category-zuoapp","7":"tag-app","9":"tag-14","10":"tag-5309","11":"tag-84"},"_links":{"self":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/8732","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/users\/15"}],"replies":[{"embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/comments?post=8732"}],"version-history":[{"count":0,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/posts\/8732\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/media?parent=8732"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/categories?post=8732"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.zhidianwl.com\/zhidian\/wp-json\/wp\/v2\/tags?post=8732"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}