Join us for ⁠#Android11: The Beta Launch Show on June 3!

ImageAsset

interface ImageAsset
androidx.ui.graphics.ImageAsset

Graphics object that represents a 2 dimensional array of pixel information represented as ARGB values

Summary

Public methods

abstract Unit

Builds caches associated with the ImageAsset that are used for drawing it.

abstract Unit
readPixels(buffer: IntArray, startX: Int = 0, startY: Int = 0, width: Int = this.width, height: Int = this.height, bufferOffset: Int = 0, stride: Int = width)

Copies the pixel data within the ImageAsset into the given array.

Extension functions

From androidx.ui.graphics
Bitmap

PixelMap
ImageAsset.toPixelMap(startX: Int = 0, startY: Int = 0, width: Int = this.width, height: Int = this.height, buffer: IntArray = IntArray(width * height), bufferOffset: Int = 0, stride: Int = width)

Convenience method to extract pixel information from the given ImageAsset into a PixelMap that supports for querying pixel information based on

Properties

abstract ColorSpace

ColorSpace the Image renders in

abstract ImageAssetConfig

Returns the current configuration of this Image, either:

abstract Boolean

Determines whether or not the ImageAsset contains an alpha channel

abstract Int

The number of image pixels along the ImageAssets's vertical axis.

abstract Int

The number of image pixels along the ImageAssets's horizontal axis.

Public methods

prepareToDraw

abstract fun prepareToDraw(): Unit

Builds caches associated with the ImageAsset that are used for drawing it. This method can be used as a signal to upload textures to the GPU to eventually be rendered

readPixels

abstract fun readPixels(
    buffer: IntArray,
    startX: Int = 0,
    startY: Int = 0,
    width: Int = this.width,
    height: Int = this.height,
    bufferOffset: Int = 0,
    stride: Int = width
): Unit

Copies the pixel data within the ImageAsset into the given array. Each value is represented as ARGB values packed into an Int. The stride parameter allows the caller to allow for gaps in the returned pixels array between rows. For normal packed, results, the stride value is equivalent to the width of the ImageAsset. The returned colors are non-premultiplied ARGB values in the ColorSpaces.Srgb color space.

Note this method can block so it is recommended to not invoke this method in performance critical code paths

import androidx.ui.graphics.PixelMap

val imageAsset = createImageAsset()

val buffer = IntArray(20 * 10)
imageAsset.readPixels(buffer = buffer,
    startX = 8,
    startY = 9,
    width = 20,
    height = 10)

val pixelmap = PixelMap(
    buffer = buffer,
    width = 20,
    height = 10,
    stride = 20,
    bufferOffset = 0
)

// create a histogram to count the number of occurrences of a color within the specified
// subsection of the provided ImageAsset
val histogram = HashMap<Color, Int>()
for (x in 0 until pixelmap.width) {
    for (y in 0 until pixelmap.height) {
        val color = pixelmap[x, y]
        val colorCount = histogram[color] ?: 0
        histogram[color] = (colorCount + 1)
    }
}
Parameters
buffer: IntArray The array to store the ImageAsset's colors. By default this allocates an IntArray large enough to store all the pixel information. Consumers of this API are advised to use the smallest IntArray necessary to extract relevant pixel information, that is the 2 dimensional area of the section of the ImageAsset to be queried.
startX: Int = 0 The x-coordinate of the first pixel to read from the ImageAsset
startY: Int = 0 The y-coordinate of the first pixel to read from the ImageAsset
width: Int = this.width The number of pixels to read from each row
height: Int = this.height The number of rows to read
bufferOffset: Int = 0 The first index to write into the buffer array, this defaults to 0
stride: Int = width The number of entries in buffer to skip between rows (must be >= width

Properties

colorSpace

abstract val colorSpace: ColorSpace

ColorSpace the Image renders in

config

abstract val config: ImageAssetConfig

Returns the current configuration of this Image, either:

hasAlpha

abstract val hasAlpha: Boolean

Determines whether or not the ImageAsset contains an alpha channel

height

abstract val height: Int

The number of image pixels along the ImageAssets's vertical axis.

width

abstract val width: Int

The number of image pixels along the ImageAssets's horizontal axis.