当前位置:网站首页>Chinese guide to accompanist component library - glide, hot
Chinese guide to accompanist component library - glide, hot
2022-06-24 00:07:00 【Ordinary netizens】

️ tip : If not necessary , It is recommended to use Coil. Coil Is based on Kotlin The realization of coprocessing , This means that it can better communicate with Jetpack Compose Integrate , Because the latter also uses a lot of CO processes .
rememberGlidePainter()
The main API rememberGlidePainter() The simplest use of is as follows :
import androidx.compose.foundation.Image
import com.google.accompanist.glide.rememberGlidePainter
Image(
painter = rememberGlidePainter(
request = “https://picsum.photos/300/300”,
previewPlaceholder = R.drawable.placeholder
),
contentDescription = stringResource(R.string.image_content_desc)
)
painter Use Glide Load incoming data , Then draw the resulting image .
Developers can also use requestBuilder Parameters are self-defined Glide Of RequestBuilder.
Fade in animation
The library supports fade in animation during image loading .

rememberGlidePainter The function parameter of fadeIn:Boolean The default is false, When fadeIn = true when , A default fade in animation will appear during the successful loading of the picture .
import androidx.compose.foundation.Image
import com.google.accompanist.glide.rememberGlidePainter
Image(
painter = rememberGlidePainter(
“https://picsum.photos/300/300”,
fadeIn = true
),
contentDescription = stringResource(R.string.image_content_desc),
)
Custom content
Sometimes a developer may want to display a placeholder image when the image is loaded or a failure prompt image when the image fails to load ,rememberGlidePainter() Back to painter It's a LoadPainter example ,ImageLoadState There are four states :Empty、Loading、Success and Error, Respectively corresponding to the initial state 、 Loading state 、 Load success and load failure . Developers can display different content as needed :
val painter = rememberGlidePainter(“https://picsum.photos/300/300”)
Box {
Image(
painter = painter,
contentDescription = stringResource(R.string.image_content_desc),
)
when (painter.loadState) {
is ImageLoadState.Loading -> {
// Display a circular progress indicator whilst loading
CircularProgressIndicator(Modifier.align(Alignment.Center))
}
is ImageLoadState.Error -> {
// If you wish to display s
《Android Summary of learning notes + Latest mobile architecture video + Big Android interview questions + Project actual combat source code handout 》
【docs.qq.com/doc/DSkNLaERkbnFoS0ZF】 Full content open source sharing
ome content if the request fails
}
}
}
preview
For support Android Studio Of Composable Previews function , Developers can use previewPlaceholder Parameter to pass in a picture resource ID , In order to Android Studio Preview the layout with pictures :
Image(
painter = rememberGlidePainter(
request = “https://picsum.photos/300/300”,
previewPlaceholder = R.drawable.placeholder,
),
contentDescription = stringResource(R.string.image_content_desc),
)
If you quote drawable Only used for previewPlaceholder, You can place it in debug Resources for building variants , for example :app/debug/res/drawable/, This operation will drawable Bundled in the debug version , It is also excluded from the release .
GIF
Accompanist Glide adopt Glide built-in GIF Support the implementation of GIF Image loading . The default support , No additional configuration is required .

Observe the change of loading state
Developers can use snapshotFlow() To observe painter.loadState The change of , In order to realize the monitoring of picture loading status , Then adjust the code logic as needed :
val painter = rememberGlidePainter(“https://image.url”)
LaunchedEffect(painter) {
snapshotFlow { painter.loadState }
.filter { it.isFinalState() }
.collect { result ->
// TODO do something with result
}
}
low { painter.loadState }
.filter { it.isFinalState() }
.collect { result ->
// TODO do something with result
}
}
边栏推荐
- Shutter control layout
- The easycvr program started abnormally as a service, but the process started normally. What is the reason?
- I was cheated by my colleagues to work overtime on weekends. I haven't seen redis used like this...
- ==What is the difference between and equals?
- [technical grass planting] the tail of the "double 11" event. Let's talk about how much discount the message push service package is!
- 1.< tag-动态规划和路径组合问题>lt.62. 不同路径 + lt.63. 不同路径 II
- 如何入门机器学习?
- Index principle and filling factor in database
- 人工智能技术岗位面试要注意什么?
- Detailed process from CPU fetching to sending control and microprogram control principle
猜你喜欢

Android 3年外包工面试笔记,有机会还是要去大厂学习提升,作为一个Android程序员

windows10安全模式进入循环蓝屏修复

Docker Deployment redis

生成所有可能的二叉搜索树

【数字信号】基于matlab模拟窗函数频谱细化【含Matlab源码 1906期】

元宇宙中的社会秩序

The easycvr program started abnormally as a service, but the process started normally. What is the reason?

Accompanist组件库中文指南 - Glide篇,劲爆

Detailed process from CPU fetching to sending control and microprogram control principle

SAVE: 软件分析验证和测试平台
随机推荐
DO280OpenShift访问控制--管理项目和账户
[interview experience package] summary of experience of being hanged during interview (I)
Easycvr implementation of adding preset point position function of Dahua equipment
fatal: The upstream branch of your current branch does not match the name of your current branch.
.NET 中的 Worker Service 介绍
Drag and drop report design - new features of jimureport 1.4.0
Interpreting the "four thoughts" of Wal Mart China President on the transformation and upgrading of physical retail
Three cool and coquettish bottom navigation
docker 部署redis
Synthetic big watermelon games wechat applet source code / wechat game applet source code
High imitation Betta app
Classical Chinese can be programmed???
What is medical treatment? AI medical concept analysis AI
SAVE: 软件分析验证和测试平台
windows10安全模式进入循环蓝屏修复
Index principle and filling factor in database
Another short video app with high imitation and eye opening
量化投资模型——高频交易做市模型相关(Avellaneda & Stoikov’s)研究解读&代码资源
Keywords such as extern and struct
Recommend 4 flutter heavy open source projects