Glide - 添加过渡交叉淡入淡出会导致占位符调整大小

问题描述 投票:0回答:2

将 Glide 与占位符一起使用并使用过渡交叉淡入淡出时,会导致占位符产生不需要的调整大小效果。

占位符的大小应为图层列表可绘制内容内的 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>

这个问题有解决办法吗?


这是要复制的代码:

主要活动:https://pastebin.com/3G7BMct3

RecyclerAdapter:https://pastebin.com/eX3T4w9s

浏览者:https://pastebin.com/Yvri5XFf

占位符:https://pastebin.com/pKputgmG

android android-glide
2个回答
0
投票

如果您不希望根据您使用 Glide 加载的图像调整

ImageView
的大小,您可以考虑采用以下方法。

ImageView
的布局xml文件中设置
ViewHolder
的固定高度。

<?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);

0
投票

不要使用占位符。该问题在最新版本的 Glide 中仍然存在。相反,如果实际 url 为 null,我只需将占位符可绘制内容输入到

load
中:

Glide.with(context)
      .load(itemList.get(i) ?: R.drawable.ic_altered_placeholder)
      .apply(RequestOptions.fitCenterTransform())
      .transition(DrawableTransitionOptions.withCrossFade())
      .into(holder.imageView);
© www.soinside.com 2019 - 2024. All rights reserved.