android - 使用 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>
解决方案
用这个版本的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)
它也允许我们在完全加载时下载缩略图
推荐阅读
- ios - 当我打包 use_frameworks 时,包会变大。为什么?
- node.js - Node Sequelize json 字段顺序
- c - C定义方法
- java - 从命令行运行 jar 时覆盖 pom.xml 中指定的主类
- haskell - 为什么尾递归模数可以优化?
- python - 用于捕获一组单词的正则表达式查询
- python - 在 ubuntu 上运行 python 程序时出现 PermissionError
- css - 使用线性渐变/径向渐变的 css/scss 中的背景图案
- python - Python 如何读取这个 json simil 字符串
- bash - 在 bash 脚本中不在子 shell 中运行哈希