A small utility library for Jetpack Compose to capture Composable content to Android Bitmap.
The way this work is we listen to the coordinates of the Composable through Modifier.onGloballyPositioned
& crop the bitmap to the coordinates we get from the callback from the nearest root composable attached to LocalView.current
.
The working is similar to SemanticsNodeInteraction.captureToImage()
which we use for screenshot testing.
Check the sample in /app directory which demonstrates the full usage of the library.
In your module's build.gradle
, include the dependency.
dependencies {
implementation "io.github.kaustubhpatange:kapture:$version"
}
Check the release notes for latest version here.
ScreenshotController
class. You can obtain it as follows,@Composable
fun TestScreen() {
val screenshotController = rememberScreenshotController()
...
}
Modifier
extension function attachController(...)
. Through this we can capture the content for this composable including it's child heirarchy.@Composable
fun TestScreen() {
val screenshotController = rememberScreenshotController() // <--
Column(modifier = Modifier
.attachController(screenshotController)) { // <--
Text(...)
Icon(...)
}
}
ScreenshotController.captureToBitmap()
. It is a suspending function so make sure to run it in a coroutine scope or in an implicit scope provided by LaunchedEffect
.@Composable
fun TestScreen() {
val screenshotController = rememberScreenshotController() // <--
Column(modifier = Modifier
.attachController(screenshotController)) { // <--
Text(...)
Icon(...)
}
LaunchedEffect(...) { // implicit coroutine scope
val bitmap: Result<Bitmap> = screenshotController.captureToBitmap(
config = Bitmap.Config.ARGB_8888 // optional
)
...
}
}
kotlin.Result<Bitmap>
which has a bitmap if successful otherwise a throwable.If you want to contribute to this project, you're always welcome! See Contributing Guidelines.
Copyright 2022 Kaustubh Patange
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
https://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.