Browse Source

Fix AnimatedImage component (#2174)

* Fix and optimize image animation for 0 and 1 frames

* Change default frame duration to 10fps to match those of a browser

* Fix zero-size image with ImageBitmap.Blank
pull/2183/head
Ilya Ryzhenkov 2 years ago committed by GitHub
parent
commit
7fbbdd8eca
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
  1. 24
      components/AnimatedImage/library/src/desktopMain/kotlin/org/jetbrains/compose/animatedimage/DesktopAnimatedImage.kt

24
components/AnimatedImage/library/src/desktopMain/kotlin/org/jetbrains/compose/animatedimage/DesktopAnimatedImage.kt

@ -10,7 +10,7 @@ import org.jetbrains.skia.Codec
import java.net.MalformedURLException import java.net.MalformedURLException
import java.net.URL import java.net.URL
private const val DEFAULT_FRAME_DURATION = 50 private const val DEFAULT_FRAME_DURATION = 100
actual class AnimatedImage(val codec: Codec) actual class AnimatedImage(val codec: Codec)
@ -26,6 +26,17 @@ actual suspend fun loadResourceAnimatedImage(path: String): AnimatedImage {
@Composable @Composable
actual fun AnimatedImage.animate(): ImageBitmap { 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 transition = rememberInfiniteTransition()
val frameIndex by transition.animateValue( val frameIndex by transition.animateValue(
initialValue = 0, initialValue = 0,
@ -52,16 +63,13 @@ actual fun AnimatedImage.animate(): ImageBitmap {
return bitmap.asComposeImageBitmap() return bitmap.asComposeImageBitmap()
} }
}
}
private fun calcFrameDuration(frame: AnimationFrameInfo): Int { private fun calcFrameDuration(frame: AnimationFrameInfo): Int {
var frameDuration = frame.duration
// If the frame does not contain information about a duration, set a reasonable constant duration // If the frame does not contain information about a duration, set a reasonable constant duration
if (frameDuration == 0) { val frameDuration = frame.duration
frameDuration = DEFAULT_FRAME_DURATION return if (frameDuration == 0) DEFAULT_FRAME_DURATION else frameDuration
}
return frameDuration
} }
/** /**

Loading…
Cancel
Save