@@ -31,17 +31,19 @@ package com.github.awxkee.avifcoil.decoder.animation
3131import android.graphics.Bitmap
3232import android.graphics.drawable.BitmapDrawable
3333import android.os.Build
34+ import coil3.Extras
3435import coil3.Image
3536import coil3.ImageLoader
3637import coil3.asImage
3738import coil3.decode.DecodeResult
3839import coil3.decode.Decoder
3940import coil3.fetch.SourceFetchResult
41+ import coil3.getExtra
42+ import coil3.request.ImageRequest
4043import coil3.request.Options
4144import coil3.request.allowHardware
4245import coil3.request.allowRgb565
4346import coil3.request.bitmapConfig
44- import coil3.size.Scale
4547import coil3.size.Size
4648import coil3.size.pxOrElse
4749import com.radzivon.bartoshyk.avif.coder.AvifAnimatedDecoder
@@ -94,21 +96,23 @@ public class AnimatedAvifDecoder(
9496 )
9597 }
9698
97- val dstWidth = options.size.width.pxOrElse { 0 }
98- val dstHeight = options.size.height.pxOrElse { 0 }
99- val scaleMode = when (options.scale) {
100- Scale .FILL -> ScaleMode .FILL
101- Scale .FIT -> ScaleMode .FIT
102- }
10399
104100 val originalImage = AvifAnimatedDecoder (sourceData)
105101
102+ val originalSize = originalImage.getImageSize()
103+ val (dstWidth, dstHeight) = (originalSize.width to originalSize.height).flexibleResize(
104+ maxOf(
105+ options.size.width.pxOrElse { 0 },
106+ options.size.height.pxOrElse { 0 }
107+ )
108+ )
109+
106110 DecodeResult (
107111 image = originalImage.toCoilImage(
108112 dstWidth = dstWidth,
109113 dstHeight = dstHeight,
110114 colorConfig = mPreferredColorConfig,
111- scaleMode = scaleMode
115+ scaleMode = ScaleMode . FIT
112116 ),
113117 isSampled = true
114118 )
@@ -123,7 +127,7 @@ public class AnimatedAvifDecoder(
123127 dstHeight : Int = 0,
124128 colorConfig : PreferredColorConfig ,
125129 scaleMode : ScaleMode
126- ): Image = if (getFramesCount() > 1 ) {
130+ ): Image = if (getFramesCount() > 1 && options.enableAvifAnimation ) {
127131 AnimatedDrawable (
128132 frameStore = AvifAnimatedStore (
129133 avifAnimatedDecoder = this ,
@@ -157,6 +161,7 @@ public class AnimatedAvifDecoder(
157161 )
158162 }.asImage()
159163
164+ /* * Note: If you want to use this decoder in order to convert image into other format, then pass [enableAvifAnimation] with false to [ImageRequest] */
160165 public class Factory (
161166 private val preheatFrames : Int = 6 ,
162167 private val exceptionLogger : ((Exception ) -> Unit )? = null ,
@@ -188,4 +193,41 @@ public class AnimatedAvifDecoder(
188193 }
189194 }
190195
191- }
196+ }
197+
198+ private fun Pair <Int , Int >.flexibleResize (
199+ max : Int
200+ ): Pair <Int , Int > {
201+ val (width, height) = this
202+
203+ if (max <= 0 ) return this
204+
205+ return if (height >= width) {
206+ val aspectRatio = width.toDouble() / height.toDouble()
207+ val targetWidth = (max * aspectRatio).toInt()
208+ targetWidth to max
209+ } else {
210+ val aspectRatio = height.toDouble() / width.toDouble()
211+ val targetHeight = (max * aspectRatio).toInt()
212+ max to targetHeight
213+ }
214+ }
215+
216+ /* * Note: Only works if you use [AnimatedAvifDecoder] */
217+ fun ImageRequest.Builder.enableAvifAnimation (enableAvifAnimation : Boolean ) = apply {
218+ extras[enableAvifAnimationKey] = enableAvifAnimation
219+ }
220+
221+ /* * Note: Only works if you use [AnimatedAvifDecoder] */
222+ val ImageRequest .enableAvifAnimation: Boolean
223+ get() = getExtra(enableAvifAnimationKey)
224+
225+ /* * Note: Only works if you use [AnimatedAvifDecoder] */
226+ val Options .enableAvifAnimation: Boolean
227+ get() = getExtra(enableAvifAnimationKey)
228+
229+ /* * Note: Only works if you use [AnimatedAvifDecoder] */
230+ val Extras .Key .Companion .enableAvifAnimation: Extras .Key <Boolean >
231+ get() = enableAvifAnimationKey
232+
233+ private val enableAvifAnimationKey = Extras .Key (default = true )
0 commit comments