类型 数字 对象 BitmapPaint 库 display.* 版本 2024.3703 版 另请参见 paint.scaleX BitmapPaint ImageSheetPaint
定义 BitmapPaint 图像的y 缩放比例,例如,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 Y axis
rect.fill.scaleY = 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)