【问题标题】:Lua Corona: unable to center image and have it fill the full screenLua Corona:无法居中图像并使其充满全屏
【发布时间】:2017-09-19 17:54:49
【问题描述】:

所以我尝试了所有似乎无法让图像占据全屏并居中的方法。我附上了图像,以及在电晕上运行时的外观。

我试过这个并得到了我所附的:

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

提前感谢您的帮助!

background image

how it looks on corona

【问题讨论】:

    标签: lua coronasdk


    【解决方案1】:

    例如您的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
    

    【讨论】:

      【解决方案2】:

      根据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
      

      【讨论】:

      • 取决于缩放方法,屏幕宽度并不总是等于display.contentWidth。对于letterbox 模式,我使用display.contentWidth - 2 * display.screenOriginX 作为宽度。这同样适用于高度。
      【解决方案3】:

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

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

      如果你想用图像填充屏幕,最好使 图像大于任何潜在的屏幕纵横比,然后接受一些 当图像超出范围时,某些屏幕上的剪辑量 边缘。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-07-30
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-20
        相关资源
        最近更新 更多