类型 函数 库 graphics.* 返回值 蒙版 版本 版本 2024.3703 关键字 图像蒙版、遮罩、剪切、位蒙版 另请参阅 遮罩图像 (指南) object:setMask() object.isHitTestMasked object.maskScaleX object.maskScaleY
从图像文件创建位蒙版。图像在内部转换为灰度。位蒙版的白色像素允许所覆盖图像可见,而黑色像素隐藏(遮罩)所覆盖图像。蒙版外的区域必须用黑色像素填充,这些黑色像素遮住了蒙版图像外的区域。
该函数返回 Mask 对象,可以使用 object:setMask() 将该对象应用于任何显示对象。
有关如何使用图像蒙版的演练,请参阅 遮罩图像 指南。
蒙版图像的宽度和高度维度必须 能被 4 整除。
蒙版图像必须在所有四边至少留出 3 个像素 的黑色空间。可能需要增加蒙版图像的整体大小,以包括此边框。
如果蒙版图像小于目标图像,蒙版界限外的区域将被完全遮罩(透明)。
存在 3 级嵌套遮罩限制,因此在将遮罩对象插入到作为容器的其他遮罩对象(包括 display.newContainer、widget.newScrollView、widget.newTableView 或遮罩 display group)中时必须小心。其他使用遮罩的显示对象包括 display.newText、display.newEmbossedText 以及任何其他 遮罩 显示对象。例如,一个文本对象(一个遮罩)在一个容器内(一个遮罩)再在一个容器内(一个遮罩)将达到 3 个嵌套遮罩的限制,但不会超过该限制。
与使用 display.newImageRect() 显示的图像不同,当前,遮罩图像不会根据设备分辨率而动态选择。因此,如果你的遮罩非常详细,你需要为不同的设备选择合适分辨率的遮罩图像,则应选中 display.imageSuffix 并根据它的值使用相应的遮罩图像。有关更多详细信息,请参阅以下示例。
graphics.newMask( filename [, baseDir] )
字符串。 要从中创建蒙版的图像文件的名称。
常量。 指定包含蒙版图像的目录路径。可以是以下任一项
system.ResourceDirectory
(默认)system.DocumentsDirectory
system.ApplicationSupportDirectory
system.TemporaryDirectory
system.CachesDirectory
-- Create and position image to be masked local image = display.newImageRect( "image.png", 768, 1024 ) image:translate( display.contentCenterX, display.contentCenterY ) -- Create mask and apply to image local mask = graphics.newMask( "circlemask.png" ) image:setMask( mask ) -- Transform mask image.maskScaleX, image.maskScaleY = 2,2
local g = display.newGroup() -- Create and position image to be masked, and insert into group local image = display.newImageRect( g, "image.png", 768, 1024 ) -- Center the Display Group g:translate( display.contentCenterX, display.contentCenterY ) local mask = graphics.newMask( "circlemask.png" ) g:setMask( mask )
-- This example shows how to select a different mask image based on the dynamic image suffix -- It ensures that higher-resolution masks are used when the device resolution warrants it -- Create and position image to be masked local image = display.newImageRect( "image.png", 768, 1024 ) image:translate( display.contentCenterX, display.contentCenterY ) -- Create up-value reference for mask local mask -- Check the image suffix values and pick the correct mask if ( display.imageSuffix == "@4x" ) then --print( "using @4x" ) mask = graphics.newMask( "[email protected]" ) image:setMask( mask ) image.maskScaleX = 0.25 image.maskScaleY = 0.25 elseif ( display.imageSuffix == "@2x" ) then --print( "using @2x" ) mask = graphics.newMask( "[email protected]" ) image:setMask( mask ) image.maskScaleX = 0.5 image.maskScaleY = 0.5 else --print( "using @1x" ) mask = graphics.newMask( "circlemask.png" ) image:setMask( mask ) end