首页 > 解决方案 > 使用 Glide 下载时,图像之间有巨大的空间分隔

问题描述

我正在尝试使用 Glide lib 从服务器下载图像后显示图像。下面是我的布局。问题是图像会自动调整它们之间的空间(垂直空间)。如果你看到我没有保留任何空间并且在 xml 预览中,我可以看到两个图像相互接触,一旦从服务器下载,我可以在 textview 和 imageviews 之间看到几乎一半的屏幕大小。如何管理这个空间。我读了很多博客和问题,但仍然不清楚。请建议。

<ScrollView xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical">

    <LinearLayout
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical"
        android:padding="@dimen/pad10">

        <TextView
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:layout_gravity="center_horizontal"
            android:gravity="center_horizontal"
            android:text="@string/welcome"
            android:textColor="@color/CadetBlue"
            android:textSize="@dimen/topm25"
            android:textStyle="bold" />

        <ImageView
            android:id="@+id/image1"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center_horizontal"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

        <ImageView
            android:id="@+id/image2"
            android:layout_width="200dp"
            android:layout_height="200dp"
            android:layout_gravity="center_horizontal"
            android:scaleType="fitCenter"
            android:src="@mipmap/ic_launcher" />

    </LinearLayout>
</ScrollView>

标签: androidimageview

解决方案


用这个版本的Glide,很不错

implementation 'com.github.bumptech.glide:glide:3.7.0'

简单的例子

Glide.with(context)
 .load("https://www.ejemplo.com/foto.png")
 .into(fotoImageView);

带有选项的示例

Glide.with(context)
 .load("https://www.ejemplo.com/foto.png")
 .crossFade()
 .centerCrop()
 .placeholder(R.drawable.ic_temp_image)
 .diskCacheStrategy(DiskCacheStrategy.ALL)
 .thumbnail(0.5f)
 .into(fotoImageView);

在您的情况下,您可以通过 centerCrop 更改 imageView 的 sacleType 并以相同的方式在 Glide 中使用 .centerCrop () ,如果要使用 fitCenter 也可以通过 fitCenter () 更改 Glide 这些属性可以在版本中找到滑翔,我把你放了,运气!

使用该CrossFade()方法我们可以使图像在加载完成时出现想象

CenterCrop()我们让图片占据了 ImageView 的所有可用空间

我们可以在加载图像时PlaceHolder(R.drawable.ic_temp_img)放置一个临时图像

我们可以配置diskCacheStrategy(DiskCacheStrategy.ALL)我们将要使用的cahce策略

即使图像非常大,thumbnail(0.5f)它也允许我们在完全加载时下载缩略图


推荐阅读