ViewCapture


public final class ViewCapture


Summary

Public methods

static final @NonNull ListenableFuture<@NonNull Bitmap>

Asynchronously captures an image of the underlying view into a Bitmap.

static final @NonNull ListenableFuture<@NonNull Void>

Trigger a redraw of the given view.

Public methods

captureToBitmap

@ExperimentalTestApi
public static final @NonNull ListenableFuture<@NonNull BitmapcaptureToBitmap(@NonNull View receiver, Rect rect)

Asynchronously captures an image of the underlying view into a Bitmap.

For devices below Build.VERSION_CODES#O (or if the view's window cannot be determined), the image is obtained using View#draw. Otherwise, PixelCopy is used.

This method will also enable HardwareRendererCompat#setDrawingEnabled(boolean) if required.

This API is primarily intended for use in lower layer libraries or frameworks. For test authors, its recommended to use espresso or compose's captureToImage.

If a rect is supplied, this will further crop locally from the bounds of the given view. For example, if the given view is at (10, 10 - 30, 30) and the rect is (5, 5 - 10, 10), the final bitmap will be a 5x5 bitmap that spans (15, 15 - 20, 20). This is particularly useful for Compose, which only has a singular view that contains a hierarchy of nodes.

This API is currently experimental and subject to change or removal.

forceRedraw

@ExperimentalTestApi
public static final @NonNull ListenableFuture<@NonNull VoidforceRedraw(@NonNull View receiver)

Trigger a redraw of the given view.

Should only be called on UI thread.

Returns
@NonNull ListenableFuture<@NonNull Void>

a ListenableFuture that will be complete once ui drawing is complete