滑动 - 添加过渡淡入淡出会导致占位符调整大小

5
使用Glide时,如果使用了占位符并使用交叉淡入淡出转换,它会导致占位符产生不必要的缩放效果。占位符的大小应该在layerlist drawable中设置为50dp。使用crossFade()

https://www.youtube.com/watch?v=7FlCJDSwoAI

没有 crossFade():

https://www.youtube.com/watch?v=vqZKZb-BKqE

Glide.with(context)
      .load(itemList.get(i))
      .apply(RequestOptions.fitCenterTransform())
      .placeholder(R.drawable.ic_altered_placeholder)
      .transition(DrawableTransitionOptions.withCrossFade())
      .into(holder.imageView);

视图持有者:
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/vh_iv_album_single_picture"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:adjustViewBounds="true"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

可绘制占位符:
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
    <!-- needs the extra spacing otherwise the drawable will be too big -->
    <item android:drawable="@drawable/ic_black_album_placeholder"
        android:left="51dp" android:right="51dp" android:top="51dp" android:bottom="51dp" />
</layer-list>

这个问题有解决方法吗?
这是复制代码的方法:
MainActivity:https://pastebin.com/3G7BMct3 RecyclerAdapter:https://pastebin.com/eX3T4w9s Viewholder:https://pastebin.com/Yvri5XFf 占位符:https://pastebin.com/pKputgmG

长话短说:https://dev59.com/rVwY5IYBdhLWcg3wwqL1。https://github.com/bumptech/glide/issues/363。我还没有找到解决这个问题的方法。此外,他写道他的PR修复了这个问题,但实际上并没有 https://github.com/bumptech/glide/pull/3913。 - Beloo
你有什么巧妙的解决方案吗?我正在尝试让它像imgur相册一样加载。 - DIRTY DAVE
1
很遗憾,我没有。否则我就会发布它了。我所看到的唯一方法是使用TransitionFactory实现自己的转换,但这需要时间。另外,您可以尝试联系PR的作者,他可能知道一些有用的信息来帮助您。 - Beloo
2个回答

0

如果你不想用Glide加载图片时重置ImageView大小,可以考虑以下方法。

ViewHolder的XML布局文件中,将ImageView的高度设置为固定值。

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    xmlns:app="http://schemas.android.com/apk/res-auto">

    <ImageView
        android:id="@+id/vh_iv_album_single_picture"
        android:layout_width="match_parent"
        android:layout_height="50dp"
        android:adjustViewBounds="true"
        app:layout_constraintTop_toTopOf="parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

此外,我建议将 placeholder 设置为 RequestOptions 的一部分。
RequestOptions requestOptions = new RequestOptions();
requestOptions.placeholder(R.drawable.ic_altered_placeholder);
requestOptions.error(R.drawable.error);
requestOptions.fitCenter();

然后在过渡之后应用选项。

Glide.with(this)
  .load(itemList.get(i))
  .transition(DrawableTransitionOptions.withCrossFade())
  .apply(requestOptions)
  .into(holder.imageView);

嘿,谢谢你的回复,但我需要调整我的图片大小,这就是为什么我将我的图像的 layout_height 设置为 wrap_content 的原因,因为如果它比固定高度小,就会有空白区域。而添加固定高度并不能解决过渡占位符调整大小的问题。 - DIRTY DAVE

0
不要使用占位符。在最新版本的Glide中,这个问题仍然存在。相反,如果实际的URL为空,我只需将占位符绘制到load中。
Glide.with(context)
      .load(itemList.get(i) ?: R.drawable.ic_altered_placeholder)
      .apply(RequestOptions.fitCenterTransform())
      .transition(DrawableTransitionOptions.withCrossFade())
      .into(holder.imageView);

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接