当前位置:网站首页>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
}
}
边栏推荐
- Interpreting the "four thoughts" of Wal Mart China President on the transformation and upgrading of physical retail
- EasyCVR程序以服务启动异常,进程启动却正常,是什么原因?
- 【Bug】C# IQueryable里的元素更改不了值
- What is medical treatment? AI medical concept analysis AI
- Startup process analysis of APP performance optimization
- Under the background of aging, the comprehensive energy efficiency management platform escorts hospitals
- Generate all possible binary search trees
- Android 3年外包工面试笔记,有机会还是要去大厂学习提升,作为一个Android程序员
- 组合总数II[每个元素只能用一次 + 去重复解集]
- Docker deploy redis
猜你喜欢

被同事坑到周末加班, 没见过把Redis用成这个鬼样子的。。。

How to achieve energy-saving and reasonable lighting control in order to achieve the "double carbon" goal

Startup process analysis of APP performance optimization

What is the use of AI technology in the medical field?
![[FreeRTOS] 07 binary semaphore and count semaphore](/img/9c/a3e4b9e02f754c5d3a54d94b7b4e35.png)
[FreeRTOS] 07 binary semaphore and count semaphore

Application of acrel-3000web power management system in Duba Expressway

High imitation Betta app

docker 部署redis
![The input parameter is object, but it was passed as [object object] because it needs to be converted to JSON format](/img/8c/b1535e03900d71b075f73f80030064.png)
The input parameter is object, but it was passed as [object object] because it needs to be converted to JSON format

The easycvr program started abnormally as a service, but the process started normally. What is the reason?
随机推荐
元宇宙中的社会秩序
How much business do you need to know to do data analysis
List<? Extensions T > and list <? Super T > difference
Save: software analysis, verification and test platform
NLP-D58-nlp比赛D27&刷题D14&读论文&mathtype
Salesforce Future method in salesforce – @future
Summary of common register bit operation modes in MCU
解决项目依赖报红问题
How to achieve energy-saving and reasonable lighting control in order to achieve the "double carbon" goal
Shuttle global levitation button
Jimureport building block report - table linkage chart topic
物联网卡设备接入EasyCVR,如何查看拉流IP以及拉流时间?
==What is the difference between and equals?
Dot and cross product
2018/gan:self attention generating adversarial networks
Generative countermeasure networks (Gans) and variants
【数字信号】基于matlab模拟窗函数频谱细化【含Matlab源码 1906期】
Chaos engineering, learn about it
Another short video app with high imitation and eye opening
[proteus simulation] example of T6963C driving pg12864 (with Chinese and English display)