Jetpack Compose 中动画可绘制的替代方案是什么

问题描述 投票:0回答:2

我正在尝试在 jetpack Compose 中实现帧动画。
我知道在android视图系统中,可以通过使用AnimationDrawable来实现。
但是如何在 jetpack Compose 中正确使用动画可绘制类呢?

android kotlin android-layout android-jetpack-compose
2个回答
0
投票

使用

AndroidView
包裹动画
ImageView

AndroidView(factory = { ctx ->
    ImageView(ctx).apply {
        setBackgroundResource(R.drawable.my_frame_animation);
        val animationDrawable : AnimationDrawable = background as AnimationDrawable
        animationDrawable.start()
    }
}

-1
投票

最后我找到了一个解决方案。我通过以编程方式创建动画可绘制对象并将其分配给图像作为可绘制对象,在jetpack compose中使用动画可绘制对象实现了帧动画。最后使用动画可绘制对象引用来控制动画。我在下面发布了示例代码。 .

val animationDrawable = AnimationDrawable()
//add your images in animationDrawable by giving information of duration etc like you gave in xml file..
Image(
    painter = rememberDrawablePainter(animationBlast),
    contentDescription = null,
    Modifier
        .offset {
            IntOffset(
                offsetX.toInt() - (if (size == 250) 300 else 0),
                offsetY.toInt() - (if (size == 250) 300 else 0)
            )
        }
        .size(size.dp), contentScale = ContentScale.Crop
)

animationDrawable?.start()
© www.soinside.com 2019 - 2024. All rights reserved.