gpt4 book ai didi

android - 如何使用 View (微光)作为 ImageView 的占位符( Glide )

转载 作者:行者123 更新时间:2023-12-03 13:26:19 24 4
gpt4 key购买 nike

我正在使用 Glide 将图像加载到我的 imageView(位于 recyclerview 中):

Glide.with(image.context).load(url)
.error(context.getDrawable(R.drawable.placeholder))
.into(image)
我看到 Glide 库还有一个“占位符”功能,它可以加载一个 Drawable,以便在图像仍在加载时显示。
另一方面,对于整个 recyclerView,我使用的是 Facebook Shimmer库以显示正在加载 recyclerview。
查看我的应用程序,一切正常。但是,在关闭 Shimmer(获取数据)和显示图像之间仍然存在间隔时间。这正是需要占位符的时候。我想知道,有没有什么方法可以使用 Shimmer 作为 imageView 的占位符? Glide 中的 Placeholder 功能只支持 Drawable,而 Shimmer 是一个 View。
有什么方法可以将 Shimmer 转换为 Drawable?还是GIF?或者有什么其他建议?

最佳答案

感谢 Mike 在上面的评论:
有一个 ShimmerDrawable 类,您可以在其中构建一个微光 View 作为可在 Glide 中使用的 drawbale:

private val shimmer = Shimmer.AlphaHighlightBuilder()// The attributes for a ShimmerDrawable is set by this builder
.setDuration(1800) // how long the shimmering animation takes to do one full sweep
.setBaseAlpha(0.7f) //the alpha of the underlying children
.setHighlightAlpha(0.6f) // the shimmer alpha amount
.setDirection(Shimmer.Direction.LEFT_TO_RIGHT)
.setAutoStart(true)
.build()

// This is the placeholder for the imageView
val shimmerDrawable = ShimmerDrawable().apply {
setShimmer(shimmer)
}


Glide.with(image.context).load(url)
.placeholder(shimmerDrawable)
.error(context.getDrawable(R.drawable.placeholder))
.into(image)

关于android - 如何使用 View (微光)作为 ImageView 的占位符( Glide ),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61076174/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com