memoryBitmap.*

类型
修订版本 版本 2024.3703
关键词 memoryBitmap,位图
平台 Android,iOS,macOS,Windows,tvOS
示例 https://github.com/coronalabs/plugins-sample-memoryBitmap

概述

此插件提供了创建和操作内存中位图的功能。

语法

local memoryBitmap = require( "plugin.memoryBitmap" )

函数

memoryBitmap.newTexture()

类型

MemoryBitmap

项目设置

要使用此插件,请在 `build.settings` 的 `plugins` 表中添加一个条目。添加后,构建服务器将在构建阶段集成该插件。

settings =
{
    plugins =
    {
        ["plugin.memoryBitmap"] =
        {
            publisherId = "com.coronalabs"
        },
    },
}

示例

设置/获取像素
local memoryBitmap = require( "plugin.memoryBitmap" )

-- Create bitmap texture
local tex = memoryBitmap.newTexture(
    {
        width = 100,
        height = 100,
        -- format = "rgba"  -- (default)
        -- format = "rgb"
        -- format = "mask"
    })

-- Create image using the bitmap texture
local bitmap = display.newImageRect( tex.filename, tex.baseDir, 100, 100 )
bitmap.x = display.contentCenterX
bitmap.y = display.contentCenterY

-- Set a pixel color in the bitmap
tex:setPixel( 10, 10, 1, 0, 0, 1 )  -- Set pixel at (10,10) to be red
-- tex:setPixel( 10, 10, {1,0,0,1} )  -- Same using table syntax for RGB+A color

-- Get a pixel color from the bitmap
print( tex:getPixel( 10, 10 ) )  --> 1 0 0 1

-- Submit texture to be updated
tex:invalidate()
设置多个像素
local memoryBitmap = require( "plugin.memoryBitmap" )

-- Create bitmap texture
local tex = memoryBitmap.newTexture(
    {
        width = 100,
        height = 100,
        -- format = "rgba"  -- (default)
        -- format = "rgb"
        -- format = "mask"
    })

-- Create image using the bitmap texture
local bitmap = display.newImageRect( tex.filename, tex.baseDir, 100, 100 )
bitmap.x = display.contentCenterX
bitmap.y = display.contentCenterY

-- Loop through all pixels and set green channel to 1
for y = 1,tex.height do
    for x = 1,tex.width do
        tex:setPixel( x, y, nil, 1, nil, nil )
        tex:invalidate()
    end
end

-- If no more modifications are required, release the texture
tex:releaseSelf()