@@ -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 ,
@@ -155,6 +159,7 @@ public class AnimatedAvifDecoder(
155159 )
156160 }.asImage()
157161
162+ /* * Note: If you want to use this decoder in order to convert image into other format, then pass [enableAvifAnimation] with false to [ImageRequest] */
158163 public class Factory (
159164 private val preheatFrames : Int = 6 ,
160165 private val exceptionLogger : ((Exception ) -> Unit )? = null ,
@@ -186,4 +191,41 @@ public class AnimatedAvifDecoder(
186191 }
187192 }
188193
189- }
194+ }
195+
196+ private fun Pair <Int , Int >.flexibleResize (
197+ max : Int
198+ ): Pair <Int , Int > {
199+ val (width, height) = this
200+
201+ if (max <= 0 ) return this
202+
203+ return if (height >= width) {
204+ val aspectRatio = width.toDouble() / height.toDouble()
205+ val targetWidth = (max * aspectRatio).toInt()
206+ targetWidth to max
207+ } else {
208+ val aspectRatio = height.toDouble() / width.toDouble()
209+ val targetHeight = (max * aspectRatio).toInt()
210+ max to targetHeight
211+ }
212+ }
213+
214+ /* * Note: Only works if you use [AnimatedAvifDecoder] */
215+ fun ImageRequest.Builder.enableAvifAnimation (enableAvifAnimation : Boolean ) = apply {
216+ extras[enableAvifAnimationKey] = enableAvifAnimation
217+ }
218+
219+ /* * Note: Only works if you use [AnimatedAvifDecoder] */
220+ val ImageRequest .enableAvifAnimation: Boolean
221+ get() = getExtra(enableAvifAnimationKey)
222+
223+ /* * Note: Only works if you use [AnimatedAvifDecoder] */
224+ val Options .enableAvifAnimation: Boolean
225+ get() = getExtra(enableAvifAnimationKey)
226+
227+ /* * Note: Only works if you use [AnimatedAvifDecoder] */
228+ val Extras .Key .Companion .enableAvifAnimation: Extras .Key <Boolean >
229+ get() = enableAvifAnimationKey
230+
231+ private val enableAvifAnimationKey = Extras .Key (default = true )
0 commit comments