【Go系列】Go的UI框架GIO

        其实主要我是要花一个折线图,但是使用Fyne貌似画不出来,使用plot也没法动态生成,听说Gio可以,那就先介绍一下什么是Gio把。

GIO(gioui.org)是一个用于Go语言的跨平台GUI库,旨在为开发人员提供构建现代图形用户界面的工具。以下是关于GIO的一些关键点:

设计哲学

  • 跨平台:GIO旨在支持多个操作系统,包括Linux、Windows、macOS、Android和iOS。
  • 性能:GIO注重性能,目标是提供流畅的用户界面。
  • 声明式API:GIO使用声明式API来构建用户界面,这有助于简化代码和维护。

核心特性

  • 图形和绘图:GIO提供了丰富的图形和绘图工具,允许开发者绘制自定义图形、形状和文本。
  • 用户界面组件:GIO提供了多种UI组件,如按钮、文本框、滑动条等,用于构建交互式界面。
  • 事件处理:GIO能够处理各种用户输入事件,如鼠标点击、键盘输入和触摸操作。
  • 布局:GIO支持灵活的布局管理,允许开发者创建自适应的界面设计。
  • 动画和过渡:GIO支持动画效果,可以用来增强用户界面的交互性和视觉吸引力。

如何使用GIO

  • 安装:使用Go的包管理器go get来安装GIO库。

    bash

    复制

    go get -u gioui.org/io@latest
    
  • 构建应用:GIO应用通常从创建一个窗口开始,然后定义UI组件和布局,最后处理事件循环。

这边需要注意,新版的GIO改了很多接口,需要Go1.22开始才能运行。

示例代码

package main

import (
	"fmt"
	"image/color"
	"log"
	"os"

	"gioui.org/app"
	"gioui.org/op"
	"gioui.org/text"
	"gioui.org/widget/material"
)

func main() {
	go func() {
		window := new(app.Window)
		err := run(window)
		if err != nil {
			log.Fatal(err)
		}
		os.Exit(0)
	}()
	app.Main()
}

func run(window *app.Window) error {
	theme := material.NewTheme()
	theme.Bg = color.NRGBA{R: 0, G: 0, B: 0, A: 255}
	var ops op.Ops
	for {
		switch e := window.Event().(type) {
		case app.DestroyEvent:
			fmt.Println("destroy")
			return e.Err
		case app.FrameEvent:
			// This graphics context is used for managing the rendering state.
			gtx := app.NewContext(&ops, e)

			title := material.H1(theme, "Hello, Gio")

			// Change the color of the label.
			maroon := color.NRGBA{R: 127, G: 0, B: 0, A: 255}
			title.Color = maroon

			// Change the position of the label.
			title.Alignment = text.Middle

			// Draw the label to the graphics context.
			title.Layout(gtx)

			// Pass the drawing operations to the GPU.
			e.Frame(gtx.Ops)
		}
	}
}

  • 4
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值