【问题标题】:Jetpack compose screenshot imageJetpack 组合屏幕截图
【发布时间】:2021-10-02 09:28:34
【问题描述】:

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

它目前几乎可以工作,但是屏幕上的玩家被拖动后的新位置不会出现在屏幕截图中。

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)
            }
        }

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

【问题讨论】:

    标签: android kotlin android-jetpack-compose android-jetpack


    【解决方案1】:

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

    幸运的是,它与offset by dp 配合得很好,所以你现在可以这样使用它:

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

    【讨论】:

    • 完美运行,谢谢
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2020-12-30
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-07-12
    • 2014-09-26
    相关资源
    最近更新 更多