recyclerview中使用glide加载图片第一次加载时图片被放大或缩小

出现这个问题首先就去百度找答案了,结果找到了很多关于glide加载的问题解决方案

        Glide.with(context).load(fruit.getImageId()).into(holder.fruitImg);//这是我原本写的
        Glide.with(context).load(fruit.getImageId()).asBitmap().centerCrop().placeholder(R.drawable.my_ic_launcher).into(holder.fruitImg);//这是百度的第一种解决方案写法
        Glide.with(context).load(fruit.getImageId()).placeholder(R.drawable.my_ic_launcher).dontAnimate().into(holder.fruitImg);//这是百度的第二种解决方案写法

第一种就是改成bitmap再进行设置图片,第二种就是取消glide的加载动画dontAnimate

;但是,这两种都试了试,对我的情况没效果。

然后就去找其他方案,后来发现还有一个影响因素,就是imageview宽高设置。

下面是我的布局

<?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>

recyclerview是使用gridelayout 一行两个排的,然后问题就在于这个imageview的宽设置,设置wrap_content时,刚开始的界面都是被放大扁扁的图片,滑动一下重新滑回来就正常了,这肯定是不行的。


解决方法就是把imageview的宽该成match_parent;重新运行就ok了。应为设置为固定值的话也不好适配排版,只能选择match_parent。


猜你喜欢

转载自blog.csdn.net/AnotherPig/article/details/80060141