Jetpack 撰写屏幕截图图像

Jetpack compose screenshot image

我正在尝试截取以下可组合项。

目前已经差不多可以了,但是屏幕上的玩家被拖拽后新位置上的玩家没有出现在截图中。

FourFourTwoFourFiveOneFormation 可组合项包含 Player 可组合项。

var capturingViewBounds by remember { mutableStateOf<Rect?>(null) }

val context = LocalContext.current
var view = LocalView.current

Column(
        modifier = Modifier
            .padding(top = 8.dp)
            .height(390.dp)
            .width(300.dp)
            .onGloballyPositioned {
                capturingViewBounds = it.boundsInRoot()
            },
        horizontalAlignment = Alignment.CenterHorizontally
    ) {

        when (currentFormation) {

            1 -> {
                FourFourTwoFormation(
                    state = state,
                    events = events
                )
            }

            2 -> {
                FourFiveOneFormation(
                    state = state,
                    events = events
                )
            }
        }

val bounds = capturingViewBounds ?: return@clickable
bitmap = Bitmap.createBitmap(
    bounds.width.roundToInt(), bounds.height.roundToInt(),
    Bitmap.Config.ARGB_8888
).applyCanvas {
    translate(-bounds.left, -bounds.top)
    view.draw(this)
}

我的播放器组合看起来像这样

Column(
    modifier = Modifier
        .offset { IntOffset(offsetX.value.roundToInt(), offsetY.value.roundToInt()) }
        .pointerInput(Unit) {
            detectDragGestures { change, dragAmount ->
                change.consumeAllChanges()
                offsetX.value = (offsetX.value + dragAmount.x)

                offsetY.value = (offsetY.value + dragAmount.y)
            }
        }

我希望屏幕截图看起来像左边,但现在看起来像右边

这看起来像一个组合错误:似乎 view.draw 忽略 offsetIntOffset 以及 graphicsLayer。我已经reported了,请star一下,多多关注

幸运的是,它与 dpoffset 一起工作得很好,所以你现在可以这样使用它:

val density = LocalDensity.current
// ...
.offset(
    x = with(density) { offsetX.value.toDp() },
    y = with(density) { offsetY.value.toDp() },
)