/
GifMovieDrawableDecoder.kt
153 lines (138 loc) · 5.79 KB
/
GifMovieDrawableDecoder.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
/*
* Copyright (C) 2022 panpf <panpfpanpf@outlook.com>
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.github.panpf.sketch.decode
import android.graphics.Bitmap.Config.ARGB_8888
import android.graphics.Bitmap.Config.RGB_565
import android.graphics.Movie
import android.os.Build
import androidx.annotation.RequiresApi
import androidx.annotation.WorkerThread
import androidx.exifinterface.media.ExifInterface
import com.github.panpf.sketch.ComponentRegistry
import com.github.panpf.sketch.Sketch
import com.github.panpf.sketch.datasource.BasedStreamDataSource
import com.github.panpf.sketch.decode.internal.ImageFormat
import com.github.panpf.sketch.decode.internal.isGif
import com.github.panpf.sketch.drawable.MovieDrawable
import com.github.panpf.sketch.drawable.SketchAnimatableDrawable
import com.github.panpf.sketch.fetch.FetchResult
import com.github.panpf.sketch.request.ANIMATION_REPEAT_INFINITE
import com.github.panpf.sketch.request.animatable2CompatCallbackOf
import com.github.panpf.sketch.request.animatedTransformation
import com.github.panpf.sketch.request.animationEndCallback
import com.github.panpf.sketch.request.animationStartCallback
import com.github.panpf.sketch.request.internal.RequestContext
import com.github.panpf.sketch.request.repeatCount
import kotlinx.coroutines.Dispatchers
import kotlinx.coroutines.withContext
/**
* Adds gif support by Movie
*/
@RequiresApi(Build.VERSION_CODES.KITKAT)
fun ComponentRegistry.Builder.supportMovieGif(): ComponentRegistry.Builder = apply {
addDrawableDecoder(GifMovieDrawableDecoder.Factory())
}
/**
* A [DrawableDecoder] that uses [Movie] to decode GIFs.
*
* Only the following attributes are supported:
*
* * repeatCount
* * animatedTransformation
* * onAnimationStart
* * onAnimationEnd
* * disallowReuseBitmap
*/
@RequiresApi(Build.VERSION_CODES.KITKAT)
class GifMovieDrawableDecoder(
private val requestContext: RequestContext,
private val dataSource: BasedStreamDataSource,
) : DrawableDecoder {
@WorkerThread
override suspend fun decode(): Result<DrawableDecodeResult> = kotlin.runCatching {
val request = requestContext.request
val movie: Movie? = dataSource.newInputStream().buffered().use { Movie.decodeStream(it) }
val width = movie?.width() ?: 0
val height = movie?.height() ?: 0
check(movie != null && width > 0 && height > 0) { "Failed to decode GIF." }
val config = if (movie.isOpaque && request.bitmapConfig?.isLowQuality == true)
RGB_565 else ARGB_8888
val movieDrawable = MovieDrawable(movie, config).apply {
setRepeatCount(request.repeatCount ?: ANIMATION_REPEAT_INFINITE)
// Set the animated transformation to be applied on each frame.
setAnimatedTransformation(request.animatedTransformation)
}
val imageInfo =
ImageInfo(width, height, ImageFormat.GIF.mimeType, ExifInterface.ORIENTATION_UNDEFINED)
val animatableDrawable = SketchAnimatableDrawable(
animatableDrawable = movieDrawable,
imageUri = request.uriString,
requestKey = requestContext.key,
requestCacheKey = requestContext.cacheKey,
imageInfo = imageInfo,
dataFrom = dataSource.dataFrom,
transformedList = null,
extras = null,
).apply {
// Set the start and end animation callbacks if any one is supplied through the request.
val onStart = request.animationStartCallback
val onEnd = request.animationEndCallback
if (onStart != null || onEnd != null) {
withContext(Dispatchers.Main) {
registerAnimationCallback(animatable2CompatCallbackOf(onStart, onEnd))
}
}
}
DrawableDecodeResult(
drawable = animatableDrawable,
imageInfo = animatableDrawable.imageInfo,
dataFrom = animatableDrawable.dataFrom,
transformedList = animatableDrawable.transformedList,
extras = animatableDrawable.extras,
)
}
@RequiresApi(Build.VERSION_CODES.KITKAT)
class Factory : DrawableDecoder.Factory {
override fun create(
sketch: Sketch,
requestContext: RequestContext,
fetchResult: FetchResult
): GifMovieDrawableDecoder? {
val dataSource = fetchResult.dataSource
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT
&& !requestContext.request.disallowAnimatedImage
&& dataSource is BasedStreamDataSource
) {
val imageFormat = ImageFormat.parseMimeType(fetchResult.mimeType)
val isGif =
if (imageFormat == null) fetchResult.headerBytes.isGif() else imageFormat == ImageFormat.GIF
if (isGif) {
return GifMovieDrawableDecoder(requestContext, dataSource)
}
}
return null
}
override fun toString(): String = "GifMovieDrawableDecoder"
override fun equals(other: Any?): Boolean {
if (this === other) return true
if (javaClass != other?.javaClass) return false
return true
}
override fun hashCode(): Int {
return javaClass.hashCode()
}
}
}