fill.scaleX

类型 数字
对象 BitmapPaint
display.*
修订版 2024.3703 版
另请参阅 paint.scaleY
BitmapPaint
ImageSheetPaint

概述

定义了 BitmapPaint 图像的x 比例因子,例如,1 等于 100%,0.5 等于 50%,2 等于 200%。

难点

当缩放位图填充以跨越不同大小(与填充图像相比)的显示对象时,可能需要在填充图像周围加入一个 2 像素的透明边框,以防止最外侧的像素溢出。填充图像的纵横比应与父级相同,否则会出现拉伸。请参阅下面的示例代码,了解如何维护图像纵横比。

示例

-- Create a vector rectangle
local rect = display.newRect( 200, 200, 300, 300 )

-- Set the fill (paint) to use the bitmap image
local paint = {
    type = "image",
    filename = "texture1.png"
}

-- Fill the rectangle
rect.fill = paint

-- Scale the fill on the X axis
rect.fill.scaleX = 2

加载图像并保持其纵横比

-- Create a vector rectangle
local rect = display.newRoundedRect( 0, 0, 150, 50, 12 )

-- Set the fill (paint) to use the bitmap image
local paint = {
    type = "image",
    filename = "image1.png",
    baseDir = system.TemporaryDirectory,
}

-- Fill the rectangle
rect.fill = paint

-- Draw the temporary image to get the width and height. This will be removed later.
local image = display.newImage( "image1.png" , system.TemporaryDirectory )

-- Calculate the image ratio
local imageRatio = image.width / image.height
local rectRatio = rect.width / rect.height 

-- Apply the scale correctly 
if imageRatio ~= rectRatio then
    if imageRatio < rectRatio then
        -- Make height higher
        rect.fill.scaleY = rectRatio / imageRatio
    else
        -- Make width wider
        rect.fill.scaleX =  imageRatio / rectRatio
    end
end

-- Remove the image
display.remove(image)