如何使用视图(微光)作为图像视图(Glide)的占位符

Meh*_*tei 13 android android-glide shimmer

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

Glide.with(image.context).load(url)
        .error(context.getDrawable(R.drawable.placeholder))
        .into(image)
Run Code Online (Sandbox Code Playgroud)

我看到 Glide 库还有一个“占位符”函数,它可以在图像仍在加载时加载要显示的 Drawable。

另一方面,对于整个 recyclerView,我使用Facebook Shimmer库来显示正在加载 recyclerview。

看看我的应用程序,一切正常。但是,在关闭 Shimmer(获取数据)和显示图像之间仍然存在间隔时间。这正是需要占位符的时候。我想知道,有没有办法将 Shimmer 用作 imageView 的占位符?Glide 中的 Placeholder 特性只支持 Drawable,Shimmer 是一个 View。

无论如何将Shimmer转换为Drawable?还是GIF?或者有什么其他建议?

Meh*_*tei 19

感谢上面 Mike 的评论:有一个 ShimmerDrawable 类,您可以在其中构建一个 shimmer 视图作为可在 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)
Run Code Online (Sandbox Code Playgroud)

  • 如果我想让可绘制的圆角怎么办?我看不到任何方法。我应该制作一个自定义绘图吗? (3认同)
  • 不为我工作 (3认同)

Mar*_*osP 5

就我而言,我想要一个带有圆角的可绘制对象作为占位符,但该库不支持该占位符,我发现您可以使用ShapeableImageViewGoogle Material Designs 来实现此目的。

1.将 Google Material Designs Library 添加到 'com.google.android.material:material:1.2.1'版本 1.2.1 及更高版本的依赖项中。

2.在列表项视图中将 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"/>
Run Code Online (Sandbox Code Playgroud)

3.在RecyclerView.ViewHolder类中将上述ShapeableImageView的ShapeAppearanceModel设置为CornerFamily.ROUNDED您想要的圆角半径(以像素为单位),并将ShimmerDrawable加载为Glide中的占位符,如下所示:

//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)
Run Code Online (Sandbox Code Playgroud)

使用辅助类将半径从 dp 转换为像素

fun dpToPx(context: Context, dp: Int): Int {
  return (dp * context.resources.displayMetrics.density).toInt()
}
Run Code Online (Sandbox Code Playgroud)

上面的结果将是:

微光圆润