如何使用视图(微光)作为 imageView(Glide)的占位符
How to use a view (shimmer) as a placeholder for an imageView (Glide)
我正在使用 Glide 将图像加载到我的 imageView(在 recyclerview 中):
Glide.with(image.context).load(url)
.error(context.getDrawable(R.drawable.placeholder))
.into(image)
我看到 Glide 库还有一个“占位符”功能,它可以加载要在图像仍在加载时显示的 Drawable。
另一方面,对于整个 recyclerView,我使用 Facebook Shimmer 库来显示正在加载 recyclerview。
查看我的应用程序,一切正常。但是,在关闭 Shimmer(获取数据)和显示图像之间仍然存在间隔时间。这正是需要占位符的时候。我想知道,有没有办法将微光用作 imageView 的占位符? Glide 中的 Placeholder 特性只支持 Drawable 而 Shimmer 是一个 View
有什么办法可以把Shimmer转成Drawable吗?还是动图?或者有其他建议吗?
感谢 Mike 在上面的评论:
有一个 ShimmerDrawable class,您可以在其中构建一个可以在 Glide 中使用的 drawbale 的 shimmer 视图:
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)
我开发了一个库来轻松添加 shimmer/skeleton 加载效果。
https://github.com/AgnaldoNP/AGSkeletonLoading
README.md 上有关于如何使用它的说明。
你不需要添加一堆布局来模拟骨架,它是自动计算的。用于显示骨架的布局与显示内容相同。
如果您在布局文件上使用 SkeletonImageView,您只需调用 startLoading() 和 stopLoading() 来控制动画。
希望对你有所帮助
在我的例子中,我想要一个带圆角的可绘制对象作为占位符,但库不支持它,我发现您可以使用 Google Material 中的 ShapeableImageView
来实现此目的] 设计。
1.Add Google Material Designs Library 到你的依赖中
'com.google.android.material:material:1.2.1'
1.2.1 及以上版本。
2.In 您的列表项视图将您的 ImageView 定义为如下所示的 ShapeableImageView:
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/shapeImageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@android:color/holo_red_dark"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
3.And in RecyclerView.ViewHolder class 将上述 ShapeableImageView 的 ShapeAppearanceModel 设置为 CornerFamily.ROUNDED
并以像素为单位将 ShimmerDrawable 作为占位符加载像下面这样滑动:
//initialize shimmer
val shimmer = Shimmer.ColorHighlightBuilder()
.setBaseColor(ContextCompat.getColor(itemView.context, R.color.teal_200))
.setBaseAlpha(0.7f)
.setHighlightAlpha(0.7f)
.setHighlightColor(ContextCompat.getColor(itemView.context, R.color.purple_700))
.setDuration(1800)
.setDirection(Shimmer.Direction.LEFT_TO_RIGHT)
.setAutoStart(true)
.build()
//create ShimmerDrawable()
val shimmerDrawable = ShimmerDrawable()
shimmerDrawable.setShimmer(shimmer)
//set the ShapeAppearanceModel to CornerFamily.ROUNDED and the radius in pixels
val radius: Float = dpToPx(itemView.context, 15).toFloat();
shapeImageView.setShapeAppearanceModel(shapeImageView.getShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED, radius)
.build())
//load url from Glide and add shimmerDrawable as placeholder
Glide.with(itemView.context).load(item.url)
.placeholder(shimmerDrawable)
.into(shapeImageView)
使用助手 class 将半径从 dp 转换为像素
fun dpToPx(context: Context, dp: Int): Int {
return (dp * context.resources.displayMetrics.density).toInt()
}
上面的结果将是:
我正在使用 Glide 将图像加载到我的 imageView(在 recyclerview 中):
Glide.with(image.context).load(url)
.error(context.getDrawable(R.drawable.placeholder))
.into(image)
我看到 Glide 库还有一个“占位符”功能,它可以加载要在图像仍在加载时显示的 Drawable。
另一方面,对于整个 recyclerView,我使用 Facebook Shimmer 库来显示正在加载 recyclerview。
查看我的应用程序,一切正常。但是,在关闭 Shimmer(获取数据)和显示图像之间仍然存在间隔时间。这正是需要占位符的时候。我想知道,有没有办法将微光用作 imageView 的占位符? Glide 中的 Placeholder 特性只支持 Drawable 而 Shimmer 是一个 View
有什么办法可以把Shimmer转成Drawable吗?还是动图?或者有其他建议吗?
感谢 Mike 在上面的评论: 有一个 ShimmerDrawable class,您可以在其中构建一个可以在 Glide 中使用的 drawbale 的 shimmer 视图:
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)
我开发了一个库来轻松添加 shimmer/skeleton 加载效果。 https://github.com/AgnaldoNP/AGSkeletonLoading
README.md 上有关于如何使用它的说明。 你不需要添加一堆布局来模拟骨架,它是自动计算的。用于显示骨架的布局与显示内容相同。
如果您在布局文件上使用 SkeletonImageView,您只需调用 startLoading() 和 stopLoading() 来控制动画。 希望对你有所帮助
在我的例子中,我想要一个带圆角的可绘制对象作为占位符,但库不支持它,我发现您可以使用 Google Material 中的 ShapeableImageView
来实现此目的] 设计。
1.Add Google Material Designs Library 到你的依赖中
'com.google.android.material:material:1.2.1'
1.2.1 及以上版本。
2.In 您的列表项视图将您的 ImageView 定义为如下所示的 ShapeableImageView:
<com.google.android.material.imageview.ShapeableImageView
android:id="@+id/shapeImageView"
android:layout_width="70dp"
android:layout_height="70dp"
android:background="@android:color/holo_red_dark"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent"/>
3.And in RecyclerView.ViewHolder class 将上述 ShapeableImageView 的 ShapeAppearanceModel 设置为 CornerFamily.ROUNDED
并以像素为单位将 ShimmerDrawable 作为占位符加载像下面这样滑动:
//initialize shimmer
val shimmer = Shimmer.ColorHighlightBuilder()
.setBaseColor(ContextCompat.getColor(itemView.context, R.color.teal_200))
.setBaseAlpha(0.7f)
.setHighlightAlpha(0.7f)
.setHighlightColor(ContextCompat.getColor(itemView.context, R.color.purple_700))
.setDuration(1800)
.setDirection(Shimmer.Direction.LEFT_TO_RIGHT)
.setAutoStart(true)
.build()
//create ShimmerDrawable()
val shimmerDrawable = ShimmerDrawable()
shimmerDrawable.setShimmer(shimmer)
//set the ShapeAppearanceModel to CornerFamily.ROUNDED and the radius in pixels
val radius: Float = dpToPx(itemView.context, 15).toFloat();
shapeImageView.setShapeAppearanceModel(shapeImageView.getShapeAppearanceModel()
.toBuilder()
.setAllCorners(CornerFamily.ROUNDED, radius)
.build())
//load url from Glide and add shimmerDrawable as placeholder
Glide.with(itemView.context).load(item.url)
.placeholder(shimmerDrawable)
.into(shapeImageView)
使用助手 class 将半径从 dp 转换为像素
fun dpToPx(context: Context, dp: Int): Int {
return (dp * context.resources.displayMetrics.density).toInt()
}
上面的结果将是: