{"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
\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<\/figure>\n<\/p>\n