Lua Corona:无法将图像居中并填满整个屏幕

Lua Corona: unable to center image and have it fill the full screen

所以我尝试了所有方法,但似乎无法让图像占据全屏并居中。我附上了图片,以及我 运行 在 corona 上时的样子。

我试过了,得到了我所附的东西:

local background = display.newImageRect( "bg.png", 
display.contentCenterX, display.contentCenterY)

在此先感谢您的帮助!

background image

how it looks on corona

根据 documentation,您需要传递 widthheight,而不是 x 和 y。试试这个:

--take screen width and height, so image can take whole screen
local width = display.contentWidth
local height = display.contentHeight
local background = display.newImageRect( "bg.png", width, height)
--setting coordinates to center
background.x = display.contentCenterX
background.y = display.contentCenterY

编辑:如果您使用letterbox缩放模式,您应该调整宽度和高度

width = display.contentWidth - 2 * display.screenOriginX
height = display.contentHeight - 2 * display.screenOriginY

例如您的 config.lua 如下所示

application = {
    content = {
        width = 640,
        height = 960, 
        scale = "letterBox",
        fps = 30,
    },
}

然后使用如下方式在全屏

中显示图像
local background = display.newImageRect( "bg.png", 
640 , 960 )
// then center it like below
background.x = display.contentCenterX
background.y = display.contentCenterY

如果你想保持图像的纵横比,试试

local _CX = display.contentCenterX
local _CY = display.contentCenterY
local imgW = 440 -- width of image 
local imgH = 623-- height of image
local imgR = imgH / imgW
local screenW = display.contentWidth - 2 * display.screenOriginX 
local screenH = display.contentHeight - 2 * display.screenOriginY 
local screenR = screenH / screenW
local factor = imgR > screenR and screenW / imgW or screenH / imgH

local background = display.newImageRect( 'bg.jpg', imgW * factor, imgH * factor )
background .x, background .y = _CX, _CY

我针对 letterbox 模式测试了代码。

来自 Corona 论坛的其他解决方案 How to make an image full screen?

if you want to fill the screen with an image, it's better to make the image larger than any potential screen aspect ratio, then accept some amount of clipping on certain screens when the image goes beyond the edges.