Long Tran
08/29/2022, 10:06 AMloadOrNull { loadResourceAnimatedImage(path) }?.animate() ?: ImageBitmap.Blank
@Composable
actual fun AnimatedImage.animate(): ImageBitmap {
when (codec.frameCount) {
0 -> return ImageBitmap.Blank // No frames at all
1 -> {
// Just one frame, no animation
val bitmap = remember(codec) { Bitmap().apply { allocPixels(codec.imageInfo) } }
remember(bitmap) {
codec.readPixels(bitmap, 0)
}
return bitmap.asComposeImageBitmap()
}
else -> {
val transition = rememberInfiniteTransition()
val frameIndex by transition.animateValue(
initialValue = 0,
targetValue = codec.frameCount - 1,
Int.VectorConverter,
animationSpec = infiniteRepeatable(
animation = keyframes {
durationMillis = 0
for ((index, frame) in codec.framesInfo.withIndex()) {
index at durationMillis
val frameDuration = calcFrameDuration(frame)
durationMillis += frameDuration
}
}
)
)
val bitmap = remember(codec) { Bitmap().apply { allocPixels(codec.imageInfo) } }
remember(bitmap, frameIndex) {
codec.readPixels(bitmap, frameIndex)
}
return bitmap.asComposeImageBitmap()
}
}
}