Glide使用placeholder占位图,最后加载出来的图片尺寸变成了占位图尺寸

Glide使用placeholder占位图,最后加载出来的图片尺寸变成了占位图尺寸。网上搜了不少方法都解决不了,其实我最后也没能研究出解决这个问题的方法,猜测可能是Glide或者是RequestOptions

的placeholder对内容大小作了什么处理吧。

下面的是一个取巧的解决方案:

Glide不设置placeholder,在ImageView控件的位置再加一个占位图 ImageView(id是iv_temp,外部局是RelativeLayout),直接在xml布局里默认让它加载占位图显示,然后给Glide添加加载监听

成功或者失败 如果都不要显示占位图了就可以把占位图设置.visibility = View.GONE,其实也算是一样的效果吧,不管黑猫白猫,能抓老鼠的应该就算是好猫了吧。

下面的代码是kotlin的

    val requestOptions = RequestOptions()
//            .placeholder(R.mipmap.ic_book_def)//不设置placeholder了
            .error(R.mipmap.ic_book_def)
            .diskCacheStrategy(DiskCacheStrategy.NONE)
            .centerInside()
列表adapter里面

Glide.with(this.context)
                        .load(item)
                        .apply(requestOptions)
                        .listener(object : RequestListener<Drawable> {
                            //这里的listener只监听了第一次加载的情况,第二第三次的如果要监听就要在它们apply后面加上listener
                            override fun onLoadFailed(e: GlideException?, model: Any?, target: Target<Drawable>?, isFirstResource: Boolean): Boolean {
                                Log_i("image","000000----onResourceReady---图片加载失败---position="+position)
                                return false
                            }

                            override fun onResourceReady(resource: Drawable?, model: Any?, target: Target<Drawable>?, dataSource: DataSource?, isFirstResource: Boolean): Boolean {
                               Log_i("image","33333333333----onResourceReady---图片加载成功---position="+position)
                                (holder?.getImageView(R.id.iv_temp) as ImageView).visibility = View.GONE
                                return false
                            }
                        })
                        .into(holder?.getImageView(R.id.image_item_image) as ImageView)

 

列表的布局item设置

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="match_parent"
    android:layout_margin="3px"
    android:focusable="true"><!-- android:background="@drawable/selector_image_item"-->

    <ImageView
        android:id="@+id/image_item_image"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerInParent="true"
        android:adjustViewBounds="true"
        android:focusable="false"
        android:scaleType="centerInside" />

    <ImageView
        android:id="@+id/iv_temp"
        android:layout_width="@dimen/dimens_200"
        android:layout_height="@dimen/dimens_200"
        android:layout_centerInParent="true"
        android:src="@mipmap/ic_book_def" />
</RelativeLayout>

上面的方案是取巧了,后面研究解决了一下RecyclerView使用glide加载图片列表刷新第二次后大小会缩小的问题,所以这个占位图应该也可以用类似的办法解决,先获取图片bitmap的大小,再去设置大小。

如果设置之后出现二次刷新图片缩小的,可以看一下这个:RecyclerView使用glide加载图片列表刷新第二次后大小会缩小 https://blog.csdn.net/k9526310/article/details/114400513


版权声明:本文为k9526310原创文章,遵循CC 4.0 BY-SA版权协议,转载请附上原文出处链接和本声明。