我有一个要放入我的应用程序中的gif文件。我知道如何插入图像资源,但是当我尝试添加gif时,它将变成静态图像。
DrawImage(image = +imageResource(R.drawable.gif))
是否有人试图在Jetpack Compose中添加gif,以致无法在线查找文档?
答案 0 :(得分:1)
答案 1 :(得分:0)
使用以下代码(取自https://github.com/luca992/coil-composable/blob/master/coil-composable/src/androidMain/kotlin/com/luca992/compose/image/CoilImage.kt并进行了修改),我能够在撰写cur.execute("UPDATE table SET Token = (?) WHERE DiscordID = (?);", (Discordid, token,))
中显示动画GIF:
0.1.0-dev16
这取决于线圈:
import android.graphics.drawable.Animatable
import android.graphics.drawable.Drawable
import android.os.Build.VERSION.SDK_INT
import androidx.annotation.Px
import androidx.compose.foundation.Image
import androidx.compose.runtime.*
import androidx.compose.ui.Modifier
import androidx.compose.ui.WithConstraints
import androidx.compose.ui.geometry.Size.Companion.Zero
import androidx.compose.ui.graphics.ImageAsset
import androidx.compose.ui.graphics.asImageAsset
import androidx.compose.ui.platform.ContextAmbient
import androidx.compose.ui.unit.Constraints.Companion.Infinity
import androidx.core.graphics.drawable.toBitmap
import androidx.ui.tooling.preview.Preview
import coil.ImageLoader
import coil.decode.GifDecoder
import coil.decode.ImageDecoderDecoder
import coil.request.CachePolicy
import coil.request.LoadRequest
import coil.request.LoadRequestBuilder
import coil.size.Scale
import coil.target.Target
import kotlinx.coroutines.*
@Composable
fun CoilImage(
model: Any,
modifier : Modifier = Modifier,
customize: LoadRequestBuilder.() -> Unit = {}
) {
WithConstraints(modifier) {
var width =
if (constraints.maxWidth > Zero.width && constraints.maxWidth < Infinity) {
constraints.maxWidth
} else {
-1
}
var height =
if (constraints.maxHeight > Zero.height && constraints.maxHeight < Infinity) {
constraints.maxHeight
} else {
-1
}
//if height xor width not able to be determined, make image a square of the determined dimension
if (width == -1) width = height
if (height == -1) height = width
val image = state<ImageAsset> { ImageAsset(width,height) }
val context = ContextAmbient.current
var animationJob : Job? = remember { null }
onCommit(model) {
val target = object : Target {
override fun onStart(placeholder: Drawable?) {
placeholder?.apply {
animationJob?.cancel()
if(height != -1 && width != -1) {
animationJob = image.update(this, width, height)
} else if (height == -1) {
val scaledHeight = intrinsicHeight * (width / intrinsicWidth )
animationJob = image.update(this, width, scaledHeight)
} else if (width == -1) {
val scaledWidth = intrinsicWidth * (height / intrinsicHeight)
animationJob = image.update(this, scaledWidth, height)
}
}
}
override fun onSuccess(result: Drawable) {
animationJob?.cancel()
animationJob = image.update(result)
}
override fun onError(error: Drawable?) {
error?.run {
animationJob?.cancel()
animationJob = image.update(error)
}
}
}
val loader = ImageLoader.Builder(context)
.componentRegistry {
if (SDK_INT >= 28) {
add(ImageDecoderDecoder())
} else {
add(GifDecoder())
}
}.build()
val request = LoadRequest.Builder(context)
.data(model)
.size(width, height)
.scale(Scale.FILL)
.diskCachePolicy(CachePolicy.ENABLED)
.apply{customize(this)}
.target(target)
val requestDisposable = loader.execute(request.build())
onDispose {
image.value = ImageAsset(width,height)
requestDisposable.dispose()
animationJob?.cancel()
}
}
Image(modifier = modifier, asset = image.value)
}
}
internal fun MutableState<ImageAsset>.update(drawable: Drawable, @Px width: Int? = null, @Px height: Int? = null) : Job? {
if (drawable is Animatable) {
(drawable as Animatable).start()
return GlobalScope.launch(Dispatchers.Default) {
while (true) {
val asset = drawable.toBitmap(
width = width ?: drawable.intrinsicWidth,
height = height ?: drawable.intrinsicHeight)
.asImageAsset()
withContext(Dispatchers.Main) {
value = asset
}
delay(16)
//1000 ms / 60 fps = 16.666 ms/fps
//TODO: figure out most efficient way to dispaly a gif
}
}
} else {
value = drawable.toBitmap(
width = width ?: drawable.intrinsicWidth,
height = height ?: drawable.intrinsicHeight)
.asImageAsset()
return null
}
}
用法如下:
implementation 'io.coil-kt:coil:0.11.0'
implementation 'io.coil-kt:coil-gif:0.11.0'