Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Android RiveView capture not working #333

Open
blueparkyanadoocorp opened this issue Aug 9, 2024 · 2 comments
Open

Android RiveView capture not working #333

blueparkyanadoocorp opened this issue Aug 9, 2024 · 2 comments

Comments

@blueparkyanadoocorp
Copy link

Hello
How do I Capture RiveView in Android?

Here is my code.

[Compose Button]
@composable
fun MyRoomShareView(
modifier: Modifier = Modifier,
uiState: MyRoomUiState,
onShare: (Uri) -> Unit = {},
) {
val context = LocalContext.current
val coroutineScope = rememberCoroutineScope()
Box(
modifier = modifier
.padding(top = 8.dp, start = 8.dp, end = 8.dp, bottom = 32.dp)
) {
Column(
modifier = modifier
.fillMaxWidth()
.wrapContentHeight()
) {

        val screenshotableComposable = screenshotableComposable(
            content = {
                MyRoomShareMyProfileView(
                    uiState = uiState
                )
            }
        )

        Box(modifier = Modifier.weight(1f, fill = true))

        MyTextButton(
            modifier = Modifier
                .fillMaxWidth(),
            text = stringResource(id = R.string.str_share_my_room),
            textColor = White,
            contentColor = Blue500,
            onClick = {
                Timber.d("onClick~~")
                    val captureBitmap = screenshotableComposable.invoke()
                    // Image Quality 80% 

                    val bmp = ImageUtils.compressBitmap(captureBitmap, ImageUtils.IMAGE_QUALITY)
                    val folder = FileUtils.getFolderPath(context, FileUtils.MY_FOLDER)
                    val newFile = ImageUtils.saveBitmapToJpg(bmp, folder, "capture_${
                        SystemClock.elapsedRealtime().toString().plus("_").plus(UUID.randomUUID().toString().uppercase(
                            Locale.getDefault()
                        ).replace("-", "_").plus(".jpg"))}", ImageUtils.IMAGE_QUALITY)


                    val selectedMediaUri = FileUtils.file2Uri(context, newFile)

                    Timber.d("newFile :  ${newFile.name}, ${newFile.path}")

                    Timber.d("uri = ${selectedMediaUri}")
                    onShare.invoke(selectedMediaUri)
            }
        )
    }
}

}

@composable
fun screenshotableComposable(content: @composable () -> Unit): () -> Bitmap {
val context = LocalContext.current
val composeView = remember { ComposeView(context = context) }
fun captureBitmap(): Bitmap = composeView.drawToBitmap()
AndroidView(
factory = {
composeView.apply {
setContent {
content()
}
}
},
modifier = Modifier
.fillMaxWidth()
.wrapContentHeight() // Make sure to set unbounded true to draw beyond screen area
)
return ::captureBitmap
}

Thank you

@HayesGordon
Copy link
Contributor

Hi @blueparkyanadoocorp, I'm not familiar with an example for Android that does this, but one thing you can try to see if it works with your current implementation is to use the Canvas renderers (instead of Skia or Rive). See our docs on choosing a renderer.

@mrrobot97
Copy link

mrrobot97 commented Sep 3, 2024

RiveAnimationView is a TextureView so you can't get a capture by normal view's way. Instead you can use TextureView.getBitmap() which is more convenient.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants