-
Notifications
You must be signed in to change notification settings - Fork 1.2k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Support for animated gif images. #153
Comments
Not sure if animated GIFs work even on Android. Maybe there's third-party library doing the same? |
it's worth noting that gifs are hugely bandwidth/latency inefficient (https://rigor.com/blog/optimizing-animated-gifs-with-html5-video/) which becomes a much bigger consideration for mobile devices. For an animation like this one in particular, it might be worth generating/drawing the bubble explosion directly rather than having a pre-generated resource file (something like https://github.com/adibfara/composeclock). That would allow you to programmatically control things like animation velocity, number of bubbles, etc. It would also allow you to have an animation that could run continuously/forever without jumping, one that visually doesn't ever repeat, etc. Just food for thought. |
This seems interesting, I'll try and make a sample of this. |
This is a sample of naive GIF rendering with compose. https://gist.github.com/Dominaezzz/cd51f8821162a149ee2a5fb69a702e7f (Can PR a sample if maintainers are interested) |
@Dominaezzz awesome.. Compiling it on JDK 15 gives the following error even though JDK has the
Should I explicitly add the XML API dependencies for |
Ha, I guess so, I ran it with JDK 11 and the IntelliJ compose project setup and it just worked. |
It's really weird that only fully qualified
From the logs, it's clear that JDK has
|
|
A bit optimized version:
|
@samuelprince77 the issue is because of Int overflow happening in this line private val durations = codec.framesInfo.map { it.duration * 1_000_000 }
private val totalDuration = durations.sum() // int overflow here The return type of duration is |
By the way, fun draw(canvas: Canvas) {
codec.readPixels(bitMap, currFrame)
canvas.drawImage(Image.makeFromBitmap(bitMap), 0f, 0f)
} |
It would be great if the |
Hello @nthily . I think that Accompanist is Android-only. I don't there is desktop(or multiplatform) support yet. |
Slightly shorter. import androidx.compose.animation.core.*
import androidx.compose.desktop.Window
import androidx.compose.foundation.Canvas
import androidx.compose.foundation.layout.size
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.asImageBitmap
import androidx.compose.ui.unit.dp
import org.jetbrains.skija.*
import java.net.URL
fun main() = Window {
val codec = remember {
val bytes = URL(
"https://raw.githubusercontent.com/JetBrains/skija/ccf303ebcf926e5ef000fc42d1a6b5b7f1e0b2b5/examples/scenes/images/codecs/animated.gif"
).readBytes()
Codec.makeFromData(Data.makeFromBytes(bytes))
}
GifAnimation(codec, Modifier.size(100.dp))
}
@Composable
fun GifAnimation(codec: Codec, modifier: Modifier) {
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
durationMillis += frame.duration
}
}
)
)
val bitmap = remember { Bitmap().apply { allocPixels(codec.imageInfo) } }
Canvas(modifier) {
codec.readPixels(bitmap, frameIndex)
drawImage(bitmap.asImageBitmap())
}
} |
Can this issue be closed now? |
We'll probably publish this as part of https://github.com/JetBrains/compose-jb/tree/master/components |
Oooh, I'll make a PR then. (I might ping you on slack for help) |
Awesome! Thanks :) |
#802 is closed, as there is no activity. We still plan to make it as a separate component in https://github.com/JetBrains/compose-jb/tree/master/components, when we find time to do it. |
Why gif support is a separate component? Shouldn't the image itself support Gif. |
Maybe, but it requires a lot more effort to make it
I don't have an answer yet, why One of the things that stops me now - it is how animatedVectorResource was designed in Compose for Android. Here we retrieve each frame separately, |
Quite some time passed since Aug 2021... @igordmn may I ask for an update, please? |
There is no update. Feel free to make a PR, adding a component to ”components” folder. |
@JetpackDuba implemented this component here. Thanks! The component will be available in the next 1.2.0-build:
|
Compile Error: Unresolved reference: Unresolved reference: skija |
What about ios? |
Was this originally intended to be just for desktop? It would be great to have support across Android & iOS as well. |
Please check the following ticket on YouTrack for follow-ups to this issue. GitHub issues will be closed in the coming weeks. |
Trying to display the following animated gif image displays only the first frame because it supports only the Bitmap.
The text was updated successfully, but these errors were encountered: