4.Glide使用之图片占位及过渡动画

本文介绍如何使用Glide加载图片并设置占位图、错误图及动画效果,提高用户体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

重点内容
为了更好的用户体验,我们在开发的时候一般会给图片放置一张加载过程的图片。一般加载图片大部分都是网络图片,加载速度取决于用户所在的环境,最好的方式是加载时显示一个占位图片,成功之后展示正确的图片。

Glide使用图片占位placeholder()(placeholder()的参数是drawable/mipmap资源的引用)

Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .placeholder(R.mipmap.ic_launcher) // can also be a drawable
    .into(imageViewPlaceholder);

当然,你不能设置一个网络图片的URL作为占位符,因为那也需要加载,应用的资源和drawable要保证是可用可访问的。当然,不能加载或加载失败也可以设置占位图

Glide使用错误图片占位error() (注:error()参数为drawable/mipmap资源)

Glide
    .with(context)
    .load("http://futurestud.io/non_existing_image.png")
    .placeholder(R.mipmap.ic_launcher) // can also be a drawable
    .error(R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded
    .into(imageViewError);

Glide加载图片使用渐入动画crossFade() 默认使用渐变动画

Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .placeholder(R.mipmap.ic_launcher) // can also be a drawable
    .error(R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded
    .crossFade()
    .into(imageViewFade);

crossFade()方法还有一个重载方法:crossFade(int duration),毫秒单位的淡入时间,Glide默认的渐入动画时间为300毫秒

Glide加载图片不使用过渡动画dontAnimate()

Glide
    .with(context)
    .load(UsageExampleListViewAdapter.eatFoodyImages[0])
    .placeholder(R.mipmap.ic_launcher) // can also be a drawable
    .error(R.mipmap.future_studio_launcher) // will be displayed if the image cannot be loaded
    .dontAnimate()
    .into(imageViewFade);

不建议这样做,除非你有充足的理由,以上方法调用没有依赖关系,你可以任意设置调用

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值