preface

In the use of RecycleView + Glide loading network pictures will appear when the refresh of the same picture will be big or small;

why

The reason is that when we load the network image, because the size and proportion of the image is not fixed, so the ImageView does not set the fixed size, but set the “WRAP_content” adaptive size, and Glide’s image loading logic is:

1. If the View layout parameter size > 0 and > padding, use that layout parameter; 2. If View size > 0 and > padding, use the actual size; 3. If the View layout parameter is “wrap_content” and a layout has occurred at least once, print a warning log. It is recommended to use target.size_original or specify other fixed sizes by override(). And use the screen size for the requested size; 4. In other cases (the layout parameters are match_parent, 0, or WRAP_content and no layout has occurred), wait for the layout to complete and then go back to Step 1.

So there will be a big and small problem

The solution

Set the ImageView to a fixed size:

       <ImageView
                android:id="@+id/iv_photo"
                android:layout_width="260dp"
                android:layout_height="260dp"
                 android:scaleType="fitStart"
                />
Copy the code

In this way, you can make the network picture with the same network address every time the refresh will be bigger or smaller problem; However, if the size of the network picture itself is relatively small, there will be a big blank at the bottom of the problem; So that’s where we can do our second approach.

Call Glide override to set the image size, and the ImageView is set to “wrAP_content” :

    
        <ImageView
                android:id="@+id/iv_photo"
                android:layout_width="wrap_content"
                android:layout_height="wrap_content"
                app:layout_constrainedWidth="true"
                app:layout_constrainedHeight="true"
                app:layout_constraintWidth_max="260dp"
                app:layout_constraintHeight_max="260dp"
                app:layout_constraintHeight_min="100dp"
                android:scaleType="fitStart"/>
Copy the code

Glide loads the code below

Glideapp.with (context).load(url) // Set the fixed size in pixels. Override (displayutils.dp2px (context,260f)) .placeholder(R.mipmap.message_icon_error_image) .error(R.mipmap.message_icon_error_image) .into(imageView)Copy the code

Using this method When the network picture when it is less than the width of setting itself pixels high Displays network image width is high, and when the network picture is greater than the set wide high will Glide ratio compression, thus achieved the effect of we need to perfect, but also solved the same network image refresh every time is not the same as the size of the problem.