Skip to content

Commit 53405fa

Browse files
authored
Merge pull request #49 from r-ralph/loop_and_repeat_count_getter
Add getter of currentRepeatCount and currentFrameIndex
2 parents 18c18d0 + ac84e08 commit 53405fa

File tree

1 file changed

+18
-10
lines changed

1 file changed

+18
-10
lines changed

apng-drawable/src/main/kotlin/com/linecorp/apng/ApngDrawable.kt

Lines changed: 18 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -106,11 +106,14 @@ class ApngDrawable @VisibleForTesting internal constructor(
106106
*/
107107
val isRecycled: Boolean = apngState.apng.isRecycled
108108

109-
private val paint: Paint = Paint(Paint.FILTER_BITMAP_FLAG or Paint.DITHER_FLAG)
110-
private val animationCallbacks: MutableList<Animatable2Compat.AnimationCallback> = arrayListOf()
111-
private val repeatAnimationCallbacks: MutableList<RepeatAnimationCallback> = arrayListOf()
112-
private val currentRepeatCount: Int
113-
get() = (animationElapsedTimeMillis / durationMillis).toInt() + 1
109+
/**
110+
* The number indicating the current loop number.
111+
* The first loop is `1` and last loop is same with [loopCount]
112+
*/
113+
val currentRepeatCount: Int
114+
get() =
115+
if (currentRepeatCountInternal > loopCount) loopCount else currentRepeatCountInternal
116+
114117
/**
115118
* [currentFrameIndex] is the index to indicate which frame should show at that time.
116119
* [currentFrameIndex] is calculated with APNG meta data and elapsed time after animation
@@ -121,13 +124,18 @@ class ApngDrawable @VisibleForTesting internal constructor(
121124
* If this image isn't infinite looping image and [animationElapsedTimeMillis] is larger than
122125
* total duration of this image's animation, returns always last frame index.
123126
*/
124-
private val currentFrameIndex: Int
127+
val currentFrameIndex: Int
125128
get() = if (loopCount != LOOP_FOREVER && exceedsRepeatCountLimitation()) {
126129
frameCount - 1
127130
} else {
128131
(animationElapsedTimeMillis % durationMillis * frameCount / durationMillis).toInt()
129132
}
130133

134+
private val currentRepeatCountInternal: Int
135+
get() = (animationElapsedTimeMillis / durationMillis).toInt() + 1
136+
private val paint: Paint = Paint(Paint.FILTER_BITMAP_FLAG or Paint.DITHER_FLAG)
137+
private val animationCallbacks: MutableList<Animatable2Compat.AnimationCallback> = arrayListOf()
138+
private val repeatAnimationCallbacks: MutableList<RepeatAnimationCallback> = arrayListOf()
131139
private var scaledWidth: Int = apngState.width
132140
private var scaledHeight: Int = apngState.height
133141
private var isStarted: Boolean = false
@@ -292,7 +300,7 @@ class ApngDrawable @VisibleForTesting internal constructor(
292300
frameChanged
293301
) {
294302
repeatAnimationCallbacks.forEach {
295-
it.onRepeat(this, currentRepeatCount + 1)
303+
it.onRepeat(this, currentRepeatCountInternal + 1)
296304
}
297305
}
298306
}
@@ -308,20 +316,20 @@ class ApngDrawable @VisibleForTesting internal constructor(
308316

309317
private fun isLastFrame(): Boolean = currentFrameIndex == frameCount - 1
310318

311-
private fun isFirstLoop(): Boolean = currentRepeatCount == 1
319+
private fun isFirstLoop(): Boolean = currentRepeatCountInternal == 1
312320

313321
private fun hasNextLoop(): Boolean {
314322
if (loopCount == LOOP_FOREVER) {
315323
return true
316324
}
317-
return currentRepeatCount < loopCount
325+
return currentRepeatCountInternal < loopCount
318326
}
319327

320328
private fun exceedsRepeatCountLimitation(): Boolean {
321329
if (loopCount == LOOP_FOREVER) {
322330
return false
323331
}
324-
return currentRepeatCount > loopCount
332+
return currentRepeatCountInternal > loopCount
325333
}
326334

327335
private fun computeBitmapSize() {

0 commit comments

Comments
 (0)