Overriding for rectangular images could lead the image not correctly displayed when height is sometimes greater and width some time else.
Here is what I did, just in case someone wants to explore. This fixed the Glide bug of shrinking the images when the RecyclerView is scrolled up and down.
FYI: I am using androidx instead of the support libraries, but it should work with ImageView and AppCompatImageView widgets as well.
Here is the snippet from the RecyclerView's Item Layout:
<RelativeLayout...
<androidx.appcompat.widget.AppCompatImageView
android:id="@+id/attachment_image"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:adjustViewBounds="true"
android:background="@drawable/camera_image_preview_default"
android:minWidth="400dp"
android:scaleType="centerCrop" />
</RelativeLayout>
And Here is the Code in my adapter onBindViewHolder override:
@Override
public void onBindViewHolder(@NonNull RecyclerView.ViewHolder holder, int position) {
....
....
attachmentImage.layout(0, 0, 0, 0);
Glide.with(context)
.load(Uri.fromFile(new File(AppGlobals.IMAGES_THUMBNAIL_STORE_PATH + packet.getImageFileName())))
.placeholder(ResourcesCompat.getDrawable(context.getResources(), R.drawable.image_loading_placeholder, null))
.centerCrop()
.error(ResourcesCompat.getDrawable(context.getResources(), R.drawable.missing_thumbnail, null))
.into(attachmentImage);
....
....
}
If you notice,
Before the Glide binding, the ImageView has been set to layout 0, 0, 0, 0. This will ensure Glide interprets it as a new layout inflation and not use Cache Strategy.
The minWidth
has been set to 400dp
, while the layout_width
has been set to match_parent
and layout_height
has been set to wrap_content
. You can manipulate the minWidth
to your choice of limit.
The binding will use centerCrop()
at runtime so it doesn't really matter what scaleType
you set at design time in the xml layout.
FYI: This example uses loading an image from the local device's public external storage directory. use other implementations to load from network or URL
Credits: The setting of layout to all zeros was mentioned and recommended by TWiStErRob in the Glide Github Community Forum.
@ https://github.com/TWiStErRob
For the issue #1591
https://github.com/bumptech/glide/issues/1591
On a side note, if you set the ImageView in the xml layout with absolute layout_height
and layout_width
- say 400dp and 300dp respectively, Glide works perfectly. Its only when the sizes are different for every image, one sees the problem.