BitmapKt

Added in 1.1.0

public final class BitmapKt


Summary

Public methods

static final @NonNull Bitmap
applyCanvas(
    @NonNull Bitmap receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull CanvasUnit> block
)

Creates a new Canvas to draw on this bitmap and executes the specified block on the newly created canvas.

static final boolean
contains(@NonNull Bitmap receiver, @NonNull Point p)

Returns true if the specified point is inside the bitmap.

static final boolean

Returns true if the specified point is inside the bitmap.

static final @NonNull Bitmap
createBitmap(int width, int height, @NonNull Bitmap.Config config)

Returns a mutable bitmap with the specified width and height.

static final @NonNull Bitmap
@RequiresApi(value = 26)
createBitmap(
    int width,
    int height,
    @NonNull Bitmap.Config config,
    boolean hasAlpha,
    @NonNull ColorSpace colorSpace
)

Returns a mutable bitmap with the specified width and height.

static final int
get(@NonNull Bitmap receiver, int x, int y)

Returns the value of the pixel at the specified location.

static final @NonNull Bitmap
scale(@NonNull Bitmap receiver, int width, int height, boolean filter)

Creates a new bitmap, scaled from this bitmap, when possible.

static final void
set(@NonNull Bitmap receiver, int x, int y, @ColorInt int color)

Writes the specified color int into the bitmap (assuming it is mutable) at the specified (x, y) coordinate.

Public methods

applyCanvas

public static final @NonNull Bitmap applyCanvas(
    @NonNull Bitmap receiver,
    @ExtensionFunctionType @NonNull Function1<@NonNull CanvasUnit> block
)

Creates a new Canvas to draw on this bitmap and executes the specified block on the newly created canvas. Example:

return Bitmap.createBitmap(…).applyCanvas {
drawLine(…)
translate(…)
drawRect(…)
}

contains

public static final boolean contains(@NonNull Bitmap receiver, @NonNull Point p)

Returns true if the specified point is inside the bitmap. A point is contained if: 0 <= x < width and 0 <= y < height. An empty bitmap never contains any point.

contains

public static final boolean contains(@NonNull Bitmap receiver, @NonNull PointF p)

Returns true if the specified point is inside the bitmap. A point is contained if: 0 <= x < width and 0 <= y < height. An empty bitmap never contains any point.

createBitmap

public static final @NonNull Bitmap createBitmap(int width, int height, @NonNull Bitmap.Config config)

Returns a mutable bitmap with the specified width and height. A config can be optionally specified. If not, the default config is Bitmap.Config.ARGB_8888.

Parameters
int width

The new bitmap's desired width

int height

The new bitmap's desired height

@NonNull Bitmap.Config config

The new bitmap's desired config

Returns
@NonNull Bitmap

A new bitmap with the specified dimensions and config

createBitmap

@RequiresApi(value = 26)
public static final @NonNull Bitmap createBitmap(
    int width,
    int height,
    @NonNull Bitmap.Config config,
    boolean hasAlpha,
    @NonNull ColorSpace colorSpace
)

Returns a mutable bitmap with the specified width and height. The config, transparency and color space can optionally be specified. They respectively default to Bitmap.Config.ARGB_8888, true and sRGB.

Parameters
int width

The new bitmap's desired width

int height

The new bitmap's desired height

@NonNull Bitmap.Config config

The new bitmap's desired config

boolean hasAlpha

Whether the new bitmap is opaque or not

@NonNull ColorSpace colorSpace

The new bitmap's color space

Returns
@NonNull Bitmap

A new bitmap with the specified dimensions and config

get

public static final int get(@NonNull Bitmap receiver, int x, int y)

Returns the value of the pixel at the specified location. The returned value is a color int in the sRGB color space.

scale

public static final @NonNull Bitmap scale(@NonNull Bitmap receiver, int width, int height, boolean filter)

Creates a new bitmap, scaled from this bitmap, when possible. If the specified width and height are the same as the current width and height of this bitmap, this bitmap is returned and no new bitmap is created.

Parameters
int width

The new bitmap's desired width

int height

The new bitmap's desired height

boolean filter

true if the source should be filtered (true by default)

Returns
@NonNull Bitmap

The new scaled bitmap or the source bitmap if no scaling is required.

set

public static final void set(@NonNull Bitmap receiver, int x, int y, @ColorInt int color)

Writes the specified color int into the bitmap (assuming it is mutable) at the specified (x, y) coordinate. The specified color is converted from sRGB to the bitmap's color space if needed.