Fyne-Canvas
包canvas包含构成Fyne GUI的所有基本CanvasObjects。 此包中实现的类型用作构建块,以构建更高阶的功能。通过设计,这些类型被设计为非交互式的。如果需要额外的功能,这通常意味着这种类型应该作为自定义小部件的一部分使用。 ```go import "fyne.io/fyne/v2/canvas" ``` ## Circle 圆 ### 定义 ```go type Circle struct { // 圆的左上角位置和右下角位置 Position1 fyne.Position Position2 fyne.Position // 是否可见 Hidden bool // 填充颜色和边框颜色 FillColor color.Color StrokeColor color.Color // 边框宽度 StrokeWidth float32 } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/canvas" "image/color" ) func main() { App := app.New() Window := App.NewWindow("test") c := canvas.NewCircle(color.RGBA{0, 255, 255, 255}) c.StrokeWidth = 10 c.StrokeColor = color.RGBA{255, 255, 0, 255} Window.SetContent(c) Window.Resize(fyne.NewSize(300, 200)) Window.Show() App.Run() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoCanvas1.png) ## Line 线 ### 定义 ```go type Line struct { // 线的左上角位置和右下角位置 Position1 fyne.Position Position2 fyne.Position // 是否可见 Hidden bool // 填充颜色 StrokeColor color.Color // 线宽 StrokeWidth float32 } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/canvas" "image/color" ) func main() { App := app.New() Window := App.NewWindow("test") l := canvas.NewLine(color.RGBA{255, 255, 0, 255}) Window.SetContent(l) Window.Resize(fyne.NewSize(300, 200)) Window.Show() App.Run() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoCanvas2.png) ## Rectangle 矩形 ### 定义 ```go type Rectangle struct { // 填充颜色 FillColor color.Color // 边框颜色 StrokeColor color.Color // 边框宽度 StrokeWidth float32 } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/canvas" "image/color" ) func main() { App := app.New() Window := App.NewWindow("test") l := canvas.NewRectangle(color.RGBA{255, 255, 0, 255}) Window.SetContent(l) Window.Resize(fyne.NewSize(300, 200)) Window.Show() App.Run() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoCanvas3.png) ## Text 文本 ### 定义 ```go type Text struct { // 文本对齐方式 Alignment fyne.TextAlign // 文本颜色 Color color.Color // 文本内容 Text string // 字体大小 TextSize float32 // 文本样式 TextStyle fyne.TextStyle } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/canvas" "image/color" ) func main() { App := app.New() Window := App.NewWindow("test") l := canvas.NewText("hello", color.RGBA{255, 255, 255, 255}) Window.SetContent(l) Window.Resize(fyne.NewSize(300, 200)) Window.Show() App.Run() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoCanvas4.png) ## Image 图像 ### 定义 ```go type Image struct { // 文件路径 File string // 从内存中加载图像资源 Resource fyne.Resource // 使用已加载的图像 Image image.Image // 设置透明度 Translucency float64 // 填充方式 FillMode ImageFill // 缩放方式 ScaleMode ImageScale } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/canvas" ) func main() { App := app.New() Window := App.NewWindow("test") l := canvas.NewImageFromFile("E:\\ohh\\goohh\\fyneStudy\\canvas\\cat.png") Window.SetContent(l) Window.Resize(fyne.NewSize(300, 200)) Window.Show() App.Run() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoCanvas5.png) ## LinearGradient 线性渐变 ### 定义 ```go type LinearGradient struct { // 起始颜色 StartColor color.Color // 结束颜色 EndColor color.Color // 旋转角度 Angle float64 } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/canvas" "image/color" ) func main() { App := app.New() Window := App.NewWindow("test") l := canvas.NewLinearGradient(color.RGBA{255, 255, 255, 255}, color.RGBA{0, 0, 0, 255}, 45) Window.SetContent(l) Window.Resize(fyne.NewSize(300, 200)) Window.Show() App.Run() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoCanvas6.png) ## RadialGradient 径向渐变 ### 定义 ```go type RadialGradient struct { // 起始颜色 StartColor color.Color // 结束颜色 EndColor color.Color // 中心偏移量 CenterOffsetX, CenterOffsetY float64 } ``` ### 示例 ```go package main import ( "fyne.io/fyne/v2" "fyne.io/fyne/v2/app" "fyne.io/fyne/v2/canvas" "image/color" ) func main() { App := app.New() Window := App.NewWindow("test") l := canvas.NewRadialGradient(color.RGBA{255, 255, 255, 255}, color.RGBA{0, 0, 0, 255}) Window.SetContent(l) Window.Resize(fyne.NewSize(300, 200)) Window.Show() App.Run() } ``` ![](https://blog.coolcooltomato.com/uploads/article/demoCanvas7.png)
创建时间:2023-08-30
|
最后修改:2023-12-27
|
©允许规范转载
酷酷番茄
首页
文章
友链