local screenWidth = SL:GetMetaValue("SCREEN_WIDTH", 0)
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT", 0)
local parent = GUI:Win_Create("zhuanpan", 0, 0, screenWidth, screenHeight, false, false, true, false, false, false, 1)
local
🤖 回答引擎: 前端Lua |
🕒 时间: 2026-05-02 14:10:25 |
👁️ 阅读: 4
要实现图片中心在屏幕正中间,需要设置图片的锚点为(0.5, 0.5),这样图片的定位点就会在图片中心,然后设置位置为屏幕中心。
```lua
local screenWidth = SL:GetMetaValue("SCREEN_WIDTH", 0)
local screenHeight = SL:GetMetaValue("SCREEN_HEIGHT", 0)
local parent = GUI:Win_Create("zhuanpan", 0, 0, screenWidth, screenHeight, false, false, true, false, false, false, 1)
local bg = GUI:Image_Create(parent, 'bg', 0, 0, 'res/yuding/tianshudonghua/1.png')
GUI:setContentSize(bg, screenWidth, screenHeight)
-- 设置锚点为图片中心
GUI:setAnchorPoint(bg, 0.50, 0.50)
-- 设置位置为屏幕中心
GUI:setPosition(bg, screenWidth/2, screenHeight/2)
```
关键点说明:
1. `GUI:setAnchorPoint(bg, 0.50, 0.50)` 将图片的锚点设置为图片的中心位置
2. `GUI:setPosition(bg, screenWidth/2, screenHeight/2)` 将图片的中心点定位到屏幕中心
这样图片就会以中心点为基准,完全居中显示在屏幕上。
← 返回问答库