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

问题描述:

因此,我尝试了一切似乎无法使图像占用全屏并居中的图像。我附上了图像,以及当我在电晕上运行时的外观。提前Lua Corona:无法将图像居中并将其填满整个屏幕

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

感谢您的帮助:

我尝试这样做,我得到了什么连接!

background image

how it looks on corona

根据documentation需要传递宽度高度,而不是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缩放模式,你应该ajust宽度和高度

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

取决于缩放方法屏幕的宽度不总是等于'display.contentWidth'。对于'letterbox'模式,我使用'display.contentWidth - 2 * display.screenOriginX'来表示宽度。这同样适用于身高。 – ldurniat

比如你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?其他解决方案:

,如果你想用图像来填充屏幕,这是更好地使 图像比任何潜在的屏幕宽高比越大,则接受一些 修剪量在某些屏幕上当图像超出 边缘时。