Skip to content

Commit

Permalink
fix skia gif play
Browse files Browse the repository at this point in the history
  • Loading branch information
qdsfdhvh committed Jul 24, 2023
1 parent 0b8d55c commit 0862ab3
Show file tree
Hide file tree
Showing 3 changed files with 8 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,8 @@ import androidx.compose.runtime.Composable
import androidx.compose.runtime.LaunchedEffect
import androidx.compose.runtime.State
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.getValue
import androidx.compose.runtime.remember
import androidx.compose.runtime.withFrameNanos
import androidx.compose.runtime.withFrameMillis
import androidx.compose.ui.geometry.Size
import androidx.compose.ui.graphics.FilterQuality
import androidx.compose.ui.graphics.drawscope.DrawScope
Expand Down Expand Up @@ -68,8 +67,8 @@ fun rememberImageResultPainter(
if (painter is AnimationPainter) {
LaunchedEffect(painter) {
while (painter.isPlay()) {
withFrameNanos { nanoTime ->
painter.update(nanoTime)
withFrameMillis { frameTimeMillis ->
painter.update(frameTimeMillis)
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,5 +4,5 @@ interface AnimationPainter {

fun isPlay(): Boolean

fun update(nanoTime: Long)
fun update(frameTimeMillis: Long)
}
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ internal class GifPainter(
private val repeatCount: Int = Options.REPEAT_INFINITE,
) : Painter(), AnimationPainter, RememberObserver {

private val durations = codec.framesInfo.map { it.duration * 1_000_000 }
private val durations = codec.framesInfo.map { it.duration }
private val totalDuration = durations.sum()

private var startTime = -1L
Expand Down Expand Up @@ -55,11 +55,11 @@ internal class GifPainter(
return repeatCount == Options.REPEAT_INFINITE || loopIteration++ < repeatCount
}

override fun update(nanoTime: Long) {
override fun update(frameTimeMillis: Long) {
if (startTime == -1L) {
startTime = nanoTime
startTime = frameTimeMillis
}
frame = frameOf(time = (nanoTime - startTime) % totalDuration)
frame = frameOf(time = (frameTimeMillis - startTime) % totalDuration)
}

// WARNING: it is not optimal
Expand Down

0 comments on commit 0862ab3

Please sign in to comment.