Overriding for rectangular images can cause the image to not display correctly when the height is sometimes larger and the width sometimes.
Here's what I did, in case someone wants to explore. This fixed the Glide bug of reducing images when scrolling the RecyclerView up and down.
For your information: I use androidx instead of support libraries, but it should work with ImageView and AppCompatImageView widgets as well.
Here is a snippet from the layout of the RecyclerView element:
<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 onBindViewHolder adapter to 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
Prior to Glide binding, ImageView was set to a layout of 0, 0, 0, 0. This ensures that Glide interprets it as new layout inflation and does not use a caching strategy.
minWidth set to 400dp , and layout_width set to match_parent and layout_height set to wrap_content .
When binding, centerCrop() will be used at runtime, so it does not matter which scaleType you set at design time in the xml layout.
Note: this example uses the image download from the external external storage directory of the local device. use other implementations to download from a network or URL
Credits: Layout customization for all zeros was mentioned and recommended by TWiStErRob on the Glide Github community forum.
@ https://github.com/TWiStErRob
For a question No. 1591
https://github.com/bumptech/glide/issues/1591
layout_height , if you set the ImageView to xml layout with absolute layout_height and layout_width - say 400dp and 300dp respectively, Glide works fine. Only when the sizes are different for each image can you see the problem.
Ram iyer
source share