Using glide to load images in recyclerview is zoomed in or out when the image is first loaded

When this problem occurred, I first went to Baidu to find the answer, and found a lot of solutions to glide loading problems.

        Glide.with(context).load(fruit.getImageId()).into(holder.fruitImg);//This is what I originally wrote
        Glide.with(context).load(fruit.getImageId()).asBitmap().centerCrop().placeholder(R.drawable.my_ic_launcher).into(holder.fruitImg);//This is Baidu's first solution Scheme writing
        Glide.with(context).load(fruit.getImageId()).placeholder(R.drawable.my_ic_launcher).dontAnimate().into(holder.fruitImg);//This is Baidu's second solution

The first is to change to bitmap and then set the picture, the second is to cancel the glide loading animation dontAnimate

; however, tried both and it didn't work in my case.

Then I went to find other solutions, and later found that there is another influencing factor, that is, the imageview width and height settings.

Below is my layout

<?xml version="1.0" encoding="utf-8"?>
<android.support.v7.widget.CardView
    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"
    android:layout_margin="5dp"
    app:cardCornerRadius="4dp"
    >
    <LinearLayout
        android:orientation="vertical"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">
        <ImageView
            android:contentDescription="aaa"
            android:id="@+id/fruit_image"
            android:scaleType="centerCrop"
            android:layout_width="wrap_content"
            android:layout_height="100dp"/>
        <TextView
            android:id="@+id/fruit_name"
            android:layout_gravity="center_horizontal"
            android:layout_margin="5dp"
            android:textSize="16sp"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>
    </LinearLayout>

</android.support.v7.widget.CardView>

The recyclerview uses gridlayout for one row and two rows. Then the problem lies in the width setting of the imageview. When wrap_content is set, the interface at the beginning is an enlarged and flat picture. It is normal to slide it and slide it back again. This is definitely not possible. of.


The solution is to change the width of the imageview to match_parent; re-run it and it will be ok. If it should be set to a fixed value, it is not easy to adapt to the typesetting, and only match_parent can be selected.


Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324849627&siteId=291194637