CanvasOverlay


@UnstableApi
public abstract class CanvasOverlay extends BitmapOverlay


A TextOverlay that is backed by a Canvas.

Use this class when the size of the drawing Canvas is known, or when drawing to the entire video size is desied.

Summary

Public constructors

CanvasOverlay(boolean useInputFrameSize)

Creates a new CanvasOverlay.

Public methods

void
configure(Size videoSize)

Set up resources for the overlay given the input video’s dimensions.

Bitmap
getBitmap(long presentationTimeUs)

Returns the overlay bitmap displayed at the specified timestamp.

abstract void
onDraw(Canvas canvas, long presentationTimeUs)

Perform custom drawing onto the Canvas.

void

Releases all resources.

void
setCanvasSize(int width, int height)

Sets the size of the Canvas.

Inherited methods

From androidx.media3.effect.BitmapOverlay
static BitmapOverlay

Creates a BitmapOverlay that shows the overlayBitmap in the same position and size throughout the whole video.

static BitmapOverlay
createStaticBitmapOverlay(
    Bitmap overlayBitmap,
    StaticOverlaySettings overlaySettings
)

Creates a BitmapOverlay that shows the overlayBitmap in the same throughout the whole video.

static BitmapOverlay
createStaticBitmapOverlay(
    Context context,
    Uri overlayBitmapUri,
    StaticOverlaySettings overlaySettings
)

Creates a BitmapOverlay that shows the input at overlayBitmapUri with the same StaticOverlaySettings throughout the whole video.

int
getTextureId(long presentationTimeUs)

Returns the overlay texture identifier displayed at the specified timestamp.

Size
getTextureSize(long presentationTimeUs)

Returns the pixel width and height of the overlay texture displayed at the specified timestamp.

float[]
getVertexTransformation(long presentationTimeUs)

Returns a 4x4 OpenGL matrix, controlling how the vertices of the overlay are displayed at the specified timestamp.

From androidx.media3.effect.TextureOverlay
OverlaySettings
getOverlaySettings(long presentationTimeUs)

Returns the OverlaySettings controlling how the overlay is displayed at the specified timestamp.

Public constructors

CanvasOverlay

public CanvasOverlay(boolean useInputFrameSize)

Creates a new CanvasOverlay.

Parameters
boolean useInputFrameSize

Whether to create the Canvas to match the input frame size, if false, setCanvasSize must be set before the first invocation to onDraw.

Public methods

configure

public void configure(Size videoSize)

Set up resources for the overlay given the input video’s dimensions.

This method will be called before drawing the first frame and before drawing subsequent frames with different input dimensions.

Parameters
Size videoSize

The width and height of the input video, in pixels.

getBitmap

public Bitmap getBitmap(long presentationTimeUs)

Returns the overlay bitmap displayed at the specified timestamp.

Parameters
long presentationTimeUs

The presentation timestamp of the current frame, in microseconds.

Throws
androidx.media3.common.VideoFrameProcessingException

If an error occurs while processing or drawing the frame.

onDraw

public abstract void onDraw(Canvas canvas, long presentationTimeUs)

Perform custom drawing onto the Canvas.

Parameters
Canvas canvas

The Canvas to draw onto.

long presentationTimeUs

The presentation timestamp, in microseconds.

release

public void release()

Releases all resources.

Throws
androidx.media3.common.VideoFrameProcessingException

If an error occurs while releasing resources.

setCanvasSize

public void setCanvasSize(int width, int height)

Sets the size of the Canvas.

The default canvas size will be of the same size as the video frame.

The size will be applied on the next invocation of onDraw.