Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jetpack Compose take screenshot of composable function?

I want to take screenshot of specific composable function on Jetpack Compose. How can I do this? Please, anyone help me. I want to take screenshot of composable function and share with other applications.

Example of my function:

@Composable
fun PhotoCard() {
    Stack() {
        Image(imageResource(id = R.drawable.background))
        Text(text = "Example")
    }
}

How to take screenshot of this function?

like image 333
Mehmet Peker Avatar asked Sep 12 '20 13:09

Mehmet Peker


People also ask

What is composable in jetpack compose?

Jetpack Compose is built around composable functions. These functions let you define your app's UI programmatically by describing how it should look and providing data dependencies, rather than focusing on the process of the UI's construction (initializing an element, attaching it to a parent, etc.).

How do I screen size in jetpack compose?

If you want to get the size in pixels: val screenDensity = configuration. densityDpi / 160f and multiply with dp, for example val screenHeight = configuration. screenHeightDp.

Is jetpack easier to compose?

Jetpack Compose is a modern declarative UI Toolkit for Android. Compose makes it easier to write and maintain your app UI by providing a declarative API that allows you to render your app UI without imperatively mutating frontend views.

Why is jetpack compose better?

Jetpack Compose is a modern toolkit that allows us to build our screens in a declarative approach writing less code. Android UI Development is now more powerful and more decoupled. Before Jetpack Compose, we were using XML layouts to build the native UI.


1 Answers

You can create a test, set the content to that composable and then call composeTestRule.captureToImage(). It returns an ImageBitmap.

Example of usage in a screenshot comparator: https://github.com/android/compose-samples/blob/e6994123804b976083fa937d3f5bf926da4facc5/Rally/app/src/androidTest/java/com/example/compose/rally/ScreenshotComparator.kt

like image 87
Jose Alcérreca Avatar answered Oct 10 '22 19:10

Jose Alcérreca