【问题标题】:Coil ImageView doesn't fit exactly线圈 ImageView 不完全适合
【发布时间】:2019-08-19 08:52:56
【问题描述】:

根据Coils 文档,我不必对fit() 的图像进行任何配置。问题是,ImageView 没有正确加载:

这是我对ImageViewPicasso 的配置:

picasso.load(unsplashResult?.photoUrls?.thumbnailUrl)
                        .fit()
                        .error(R.drawable.img_placeholder)
                        .placeholder(R.drawable.img_placeholder)
                        .into(currentImageView)

这是带有Coil的代码:

currentImageView.load(unsplashResult?.photoUrls?.thumbnailUrl) {
                        placeholder(R.drawable.img_placeholder)
                        error(R.drawable.img_placeholder)
                    }

虽然我的ImageView 上的结果不一样。

这是Coil 加载它们的方式:

这就是Picasso 加载它们的方式:

问题是,如何使用Coil 达到相同的结果?

编辑

这是我的ImageView

<ImageView
            android:adjustViewBounds="true"
            android:id="@+id/ivUnsplashImage"
            android:src="@drawable/some_unsplash_img"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"/>

【问题讨论】:

  • 嗯。实际上它说不需要进行任何合适的配置。 coil-kt.github.io/coil/migrating
  • 关于 github 中的 Coils README
  • 你能解决这个问题吗?

标签: android coil


【解决方案1】:

基于本文档

您可以根据您的要求将 scaleType 设置为 centerCrop 或 scale(Scale.FILL) 来解决此问题。

https://coil-kt.github.io/coil/api/coil-base/coil.request/-request-builder/scale/

https://coil-kt.github.io/coil/api/coil-base/coil.size/-scale/

你设置Scale这样输入

view.load(chatMessageBean.thumb) {
       crossfade(750)
       placeholder(errorPlaceHolder)
       transformations(CircleCropTransformation())
       error(errorPlaceHolder)
       scale(Scale.FILL)
}

【讨论】:

  • 只有一件事,这种行为确实解决了,但在第二次加载时它仍然存在
  • @coroutineDispatcher 能否详细说明一下?
  • 这段代码确实有效,但是如果我再次单击相同的片段,照片将恢复到以前的状态(不正确的,问题中的图片1)
【解决方案2】:

所以,根据迁移指南的答案应该是这样的;

imageView.scaleType = ImageView.ScaleType.FIT_CENTER

或者应该在 XML 中定义

android:scaleType="fitCenter"

【讨论】:

  • 不,同样的结果
  • 你检查了吗? github.com/coil-kt/coil/issues/20 我认为缩放有问题
  • @coroutineDispatcher 看起来这与这个问题有关(github.com/coil-kt/coil/issues/20)。我正在努力在几天内为 0.7.0 版本修复它。
  • 是的,但在我的情况下,即使将scaletype 设置为centercrop,它仍然没有给出预期的结果
【解决方案3】:

只需删除布局 XML 文件中的 android:adjustViewBounds="true" 行即可。

【讨论】:

    【解决方案4】:

    我从文档中了解到,您应该使用 ImageView.ScaleType

    imageView.scaleType = ImageView.ScaleType.FIT_XY
    

    或者你可以在你的 ImageView 的 xml 标签中使用它

    android:scaleType="fitXY"
    

    除了FIT_XY,你也可以根据需要使用ImageView.ScaleType.CENTER_CROP

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2021-02-21
      • 1970-01-01
      • 1970-01-01
      • 2013-03-19
      相关资源
      最近更新 更多