Stay organized with collections
Save and categorize content based on your preferences.
Classes
Extension functions summary
suspend Bitmap |
Suspend function that captures an image of the underlying window into a Bitmap .
|
ListenableFuture<Bitmap> |
A ListenableFuture variant of captureRegionToBitmap intended for use from Java.
|
suspend Bitmap |
Suspend function for capturing an image of the underlying view into a Bitmap .
|
ListenableFuture<Bitmap> |
A ListenableFuture variant of captureToBitmap intended for use from Java.
|
Extension functions
suspend fun Window.captureRegionToBitmap(boundsInWindow: Rect? = null): Bitmap
Suspend function that captures an image of the underlying window into a Bitmap
.
For devices below Build.VERSION_CODES#O the image is obtained using View#draw on the windows decorView. 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.
This API must be called from the UI thread.
The resulting image is captured after forcing the View to redraw, and waiting for the draw to operation complete. This is done as a means to improve the stability of the resulting image - especially in cases where hardware rendering drawing is off initially.
suspend fun View.captureToBitmap(rect: Rect? = null): Bitmap
Suspend function for capturing an image of the underlying view into a Bitmap
.
For devices below Build.VERSION_CODES#O, the image is obtained using View#draw. Otherwise, PixelCopy
is used. Note when PixelCopy is used, the resulting image will be taken from the View's window, then cropped to the approximate location of the View in the window. So depending on window content you may see content from other View's within the resulting image.
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, it's recommended to use Espresso's captureToBitmap action 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 must be called on the View's handler thread. If you're calling this from another context, eg directly from the test thread, you can use something like
{@code
runBlocking(view.handler.asCoroutineDispatcher()) {
withTimeout(10.seconds) {
view.captureToBitmap(rect)
}
}
}
The resulting image is captured after forcing the View to redraw, and waiting for the draw to operation complete. This is done as a means to improve the stability of the resulting image - especially in cases where hardware rendering drawing is off initially.
Content and code samples on this page are subject to the licenses described in the Content License. Java and OpenJDK are trademarks or registered trademarks of Oracle and/or its affiliates.
Last updated 2025-05-15 UTC.
[[["Easy to understand","easyToUnderstand","thumb-up"],["Solved my problem","solvedMyProblem","thumb-up"],["Other","otherUp","thumb-up"]],[["Missing the information I need","missingTheInformationINeed","thumb-down"],["Too complicated / too many steps","tooComplicatedTooManySteps","thumb-down"],["Out of date","outOfDate","thumb-down"],["Samples / code issue","samplesCodeIssue","thumb-down"],["Other","otherDown","thumb-down"]],["Last updated 2025-05-15 UTC."],[],[],null,["# androidx.test.core.view\n=======================\n\n*** ** * ** ***\n\nKotlin \\|[Java](/reference/androidx/test/core/view/package-summary \"View this page in Java\")\n\nClasses\n-------\n\n|------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------|\n| [MotionEventBuilder](/reference/kotlin/androidx/test/core/view/MotionEventBuilder) | A helper builder for creating [MotionEvent](https://developer.android.com/reference/android/view/MotionEvent.html)'s. |\n| [PointerCoordsBuilder](/reference/kotlin/androidx/test/core/view/PointerCoordsBuilder) | Builder for [PointerCoords](https://developer.android.com/reference/android/view/MotionEvent.PointerCoords.html). |\n| [PointerPropertiesBuilder](/reference/kotlin/androidx/test/core/view/PointerPropertiesBuilder) | Builder for [PointerProperties](https://developer.android.com/reference/android/view/MotionEvent.PointerProperties.html). |\n\nExtension functions summary\n---------------------------\n\n|----------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------|\n| `suspend `[Bitmap](https://developer.android.com/reference/android/graphics/Bitmap.html) | [Window](https://developer.android.com/reference/android/view/Window.html)`.`[captureRegionToBitmap](/reference/kotlin/androidx/test/core/view/package-summary#(android.view.Window).captureRegionToBitmap(android.graphics.Rect))`(boundsInWindow: `[Rect](https://developer.android.com/reference/android/graphics/Rect.html)`?)` Suspend function that captures an image of the underlying window into a [Bitmap](https://developer.android.com/reference/android/graphics/Bitmap.html). |\n| [ListenableFuture](https://guava.dev/releases/18.0/api/docs/com/google/common/util/concurrent/ListenableFuture.html)`\u003c`[Bitmap](https://developer.android.com/reference/android/graphics/Bitmap.html)`\u003e` | [Window](https://developer.android.com/reference/android/view/Window.html)`.`[captureRegionToBitmapAsync](/reference/kotlin/androidx/test/core/view/package-summary#(android.view.Window).captureRegionToBitmapAsync(android.graphics.Rect))`(boundsInWindow: `[Rect](https://developer.android.com/reference/android/graphics/Rect.html)`?)` A ListenableFuture variant of captureRegionToBitmap intended for use from Java. |\n| `suspend `[Bitmap](https://developer.android.com/reference/android/graphics/Bitmap.html) | [View](https://developer.android.com/reference/android/view/View.html)`.`[captureToBitmap](/reference/kotlin/androidx/test/core/view/package-summary#(android.view.View).captureToBitmap(android.graphics.Rect))`(rect: `[Rect](https://developer.android.com/reference/android/graphics/Rect.html)`?)` Suspend function for capturing an image of the underlying view into a [Bitmap](https://developer.android.com/reference/android/graphics/Bitmap.html). |\n| [ListenableFuture](https://guava.dev/releases/18.0/api/docs/com/google/common/util/concurrent/ListenableFuture.html)`\u003c`[Bitmap](https://developer.android.com/reference/android/graphics/Bitmap.html)`\u003e` | [View](https://developer.android.com/reference/android/view/View.html)`.`[captureToBitmapAsync](/reference/kotlin/androidx/test/core/view/package-summary#(android.view.View).captureToBitmapAsync(android.graphics.Rect))`(rect: `[Rect](https://developer.android.com/reference/android/graphics/Rect.html)`?)` A ListenableFuture variant of captureToBitmap intended for use from Java. |\n\nExtension functions\n-------------------\n\n### captureRegionToBitmap\n\nArtifact: [androidx.test:core](/jetpack/androidx/releases/test) \n[View Source](https://cs.android.com/search?q=file:androidx/test/core/view/WindowCapture.kt+function:captureRegionToBitmap) \n\n```\nsuspend fun Window.captureRegionToBitmap(boundsInWindow: Rect? = null): Bitmap\n```\n\nSuspend function that captures an image of the underlying window into a [Bitmap](https://developer.android.com/reference/android/graphics/Bitmap.html).\n\nFor devices below Build.VERSION_CODES#O the image is obtained using View#draw on the windows decorView. Otherwise, [PixelCopy](https://developer.android.com/reference/android/view/PixelCopy.html) is used.\n\nThis method will also enable HardwareRendererCompat#setDrawingEnabled(boolean) if required.\n\nThis API is primarily intended for use in lower layer libraries or frameworks. For test authors, its recommended to use espresso or compose's captureToImage.\n\nThis API must be called from the UI thread.\n\nThe resulting image is captured after forcing the View to redraw, and waiting for the draw to operation complete. This is done as a means to improve the stability of the resulting image - especially in cases where hardware rendering drawing is off initially. \n\n### captureRegionToBitmapAsync\n\nArtifact: [androidx.test:core](/jetpack/androidx/releases/test) \n[View Source](https://cs.android.com/search?q=file:androidx/test/core/view/WindowCapture.kt+function:captureRegionToBitmapAsync) \n\n```\nfun Window.captureRegionToBitmapAsync(boundsInWindow: Rect? = null): ListenableFuture\u003cBitmap\u003e\n```\n\nA ListenableFuture variant of captureRegionToBitmap intended for use from Java. \n\n### captureToBitmap\n\nArtifact: [androidx.test:core](/jetpack/androidx/releases/test) \n[View Source](https://cs.android.com/search?q=file:androidx/test/core/view/ViewCapture.kt+function:captureToBitmap) \n\n```\nsuspend fun View.captureToBitmap(rect: Rect? = null): Bitmap\n```\n\nSuspend function for capturing an image of the underlying view into a [Bitmap](https://developer.android.com/reference/android/graphics/Bitmap.html).\n\nFor devices below Build.VERSION_CODES#O, the image is obtained using View#draw. Otherwise, [PixelCopy](https://developer.android.com/reference/android/view/PixelCopy.html) is used. Note when PixelCopy is used, the resulting image will be taken from the View's window, then cropped to the approximate location of the View in the window. So depending on window content you may see content from other View's within the resulting image.\n\nThis method will also enable HardwareRendererCompat#setDrawingEnabled(boolean) if required.\n\nThis API is primarily intended for use in lower layer libraries or frameworks. For test authors, it's recommended to use Espresso's captureToBitmap action or Compose's captureToImage.\n\nIf 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.\n\nThis API must be called on the View's handler thread. If you're calling this from another context, eg directly from the test thread, you can use something like \n\n```\n{@code\nrunBlocking(view.handler.asCoroutineDispatcher()) {\n withTimeout(10.seconds) {\n view.captureToBitmap(rect)\n }\n}\n}\n```\n\nThe resulting image is captured after forcing the View to redraw, and waiting for the draw to operation complete. This is done as a means to improve the stability of the resulting image - especially in cases where hardware rendering drawing is off initially. \n\n### captureToBitmapAsync\n\nArtifact: [androidx.test:core](/jetpack/androidx/releases/test) \n[View Source](https://cs.android.com/search?q=file:androidx/test/core/view/ViewCapture.kt+function:captureToBitmapAsync) \n\n```\nfun View.captureToBitmapAsync(rect: Rect? = null): ListenableFuture\u003cBitmap\u003e\n```\n\nA ListenableFuture variant of captureToBitmap intended for use from Java."]]