Glide 在第一次加载时缩小图像

Glide shrinks image on first load

当我使用 Glide 将图像加载到 ImageView 时,它会缩小图像。当我旋转或重新打开相同的 activity 时,它的大小正确。我已经试过了 。我已经将修复大小传递给 xml 内的 ImageView 并且我也尝试覆盖该大小。 centerCrop()fitCenter() 对收缩没有影响。 我还尝试调试图像大小。无论是否缩小,它 returns 相同的宽度和高度。

查看:

<ImageView
      android:id="@+id/thumbnail"
      android:layout_width="@dimen/episode_detail_img_width"
      android:layout_height="@dimen/episode_detail_img_height"
      android:layout_marginTop="@dimen/episode_detail_img_margin_top" />

滑行

Glide.with(getActivity())
            .load(url)
            .placeholder(R.drawable.thumb_placeholder)
            .centerCrop()
            .diskCacheStrategy(DiskCacheStrategy.ALL)
            .into(thumbnail);

与 Picasso 相比,Glide 在内存方面效率更高。 Glide 自动将它在缓存和内存中保存的图像的大小限制为 ImageView 尺寸

清理您的项目并尝试此代码:

Glide
  .with(context)
  .load(url)
  .override(400, 200) // resizes the image to these dimensions (in pixel).does not respect aspect ratio
  .into(thumbnail);

很高兴你得到了答案。只是为了更多地描述你的问题是:

Glide 在开始加载图像之前计算了您的 ImageView 高度和宽度,但是当图像加载时它会替换您的占位符可绘制对象。因此,如果您的占位符可绘制高度或重量很小,Glide 将使用占位符的尺寸渲染下载的位图。

因此,为了在不同的屏幕尺寸和布局中不出现延迟,我建议在占位符中使用高尺寸可绘制对象。

还有一个技巧是在占位符中使用颜色,因为颜色没有高度或宽度,所以要在整个视图区域中渲染。

您已定义 placeHolder() 方法,该方法将在初始时间加载,当 glide 根据给定 url 加载图像时,然后 glide 替换加载的图像与占位符图像。这将出现在 imageView 中。 这个加载的图像根据这些指定的方法设置到 imageView 中。

喜欢: cropCenter()、fitCenter() 等

不要使用 cropCenter() 它会从中心裁剪图像并将其放入您的 imageView 中,因此图像质量会下降。

使用 fitCenter() 方法。

<ImageView
            android:id="@+id/conversation_team_photo"
            android:layout_width="56dp"
            android:layout_height="56dp"
            android:scaleType="fitCenter"
             />

我在这里定义了使用 glide 的 bast 方法

可以通过以下方式修复:

1) 添加.dontAnimate()

2) 找到与图片比例匹配的占位符

说明: https://github.com/bumptech/glide/issues/1295

The first time the image loads the default .crossFade() kicks in and your placeholder aspect ratio is not matching the loaded images' ratio. The second time the image is loaded from memory cache and the animations are skipped in that case, resulting in just loading the image properly. You can disable animations via .dontAnimate(), or force a fade-in animation .animate(R.anim.fade_in), or find a placeholder that matches the image's ratio (i.e. square in your case, because of 512x512).