将 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
如果您不希望根据您使用 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);
不要使用占位符。该问题在最新版本的 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);