【问题标题】:Picasso displays image smaller than expected (half expected size)Picasso 显示的图像比预期的要小(预期大小的一半)
【发布时间】:2021-02-07 13:20:02
【问题描述】:

我正在使用 Picasso 从这样的 URL 加载图像并将它们显示在我的 Android 应用程序中:http://image.tmdb.org/t/p/w185/A7HtCxFe7Ms8H7e7o2zawppbuDT.jpg

但是,问题在于它们显示的是原始大小的一半。我不知道为什么。

直接在浏览器中加载上面的 URL,你会得到一个 185px x 278px 的图像

我将其中一张图像保存在本地,用作调试的虚拟图像。当我使用imageView.setImageResource(R.drawable.dummyimage); 时,图像显示为正确的大小。 (对不起,我试着附上截图,但我的声望不够高。)

但是,当我使用 Picasso.with(mContext).load(mMoviePosterURL).into(imageView); 加载图像时,它们会显示为该大小的一半。 (如果可以的话,我会附上截图。)

这是我的 ImageView:

<ImageView
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:id="@+id/grid_item_imageview"
    android:src="@drawable/dummyimage">
</ImageView>

还有我的布局:

<GridView
    android:id="@+id/grid_view"
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:verticalSpacing="0dp"
    android:horizontalSpacing="0dp"
    android:stretchMode="columnWidth"
    tools:context=".MainActivityFragment"
    android:numColumns="auto_fit">
</GridView>

我在 BaseAdapter 中的 getView 方法的完整代码:

public View getView(int position, View convertView, ViewGroup parent) {
            if (convertView == null) {
                imageView = new ImageView(mContext);
            } else {
                imageView = (ImageView) convertView;
            }

           //imageView.setImageResource(R.drawable.dummyimage);  for comparison

            if(mMoviePosterPaths!=null) {
                Picasso.with(mContext)
                   .load(mMoviePosterPaths[position])
                   .into(imageView);
            }
            return imageView;
        }

我找到了一些解决方法。一种是这样做:.resize(185*2,278*2),但如果可能的话,我想避免放大图像。

另一种方法是从提供图像的 API 中调用更大的图像,但我觉得这效率较低,因为它需要更多数据。

四处搜索我没有找到其他人遇到这个问题,所以也许我只是在做一些愚蠢的事情,但我很难过:(

我想这里主要是我试图理解为什么imageView.setImageResource()Picasso {...} 显示相同的图像不同。

谢谢

【问题讨论】:

  • 很不清楚你说displaying half their original size是什么意思。它看起来更小吗? @drawable/dummyimage 的大小是多少?
  • 只是为了确保:您知道在 Android 中我们不处理像素,而是处理 dp(与密度无关的像素)吗?
  • @Foxinsocks 我会附上截图,但我没有足够的声誉。基本上,当我在文件的本地副本上使用 setImageResource() 时,它会按预期显示。当我使用毕加索时,它看起来比预期的要小(预期大小的一半)
  • @Foxinsocks 我在这个线程上有一些截图,我也问过这个问题。谢谢discussions.udacity.com/t/…
  • @Foxinsocks @drawable/dummyimage 的大小为 185x278

标签: android picasso


【解决方案1】:

您将图像大小调整为原始大小的两倍。这就是为什么图像只显示一半。尝试删除

.resize(185*2,278*2)

或调整宽度和高度。

.resize(185,278)

然后在你的 ImageView 中添加

android:scaleType="fitXY"

希望这会有所帮助:)

【讨论】:

  • 谢谢。澄清一下,.resize(185*2,278*2) 是解决方法。当我删除它,或者当我使用.resize(185,278) 时,它显示为一半大小(小于预期)。我已经尝试过scaleType="fitXY",但没有成功,但我会再试一次。谢谢
  • 我曾经用过毕加索,但遇到了一些问题。我现在正在使用 UIL。也可以使用 UIL 进行测试 :)
  • 在您现有的毕加索代码中添加 .fit().centerInside() 并重试。
  • 尝试了您的建议。 scaleType="fitXY".fit().centerInside()。没用。不过谢谢。我可以做一些变通方法,比如将尺寸乘以 *2(这看起来很奇怪),或者我可以从 API 调用更大的图像(但这似乎效率较低,因为它会使用更多数据)
【解决方案2】:

我遇到了同样的问题,但是在AppCompatImageView 中使用android:scaleType="fitXY" 属性我得到的尺寸与原始尺寸完全相同。

XML:

<androidx.appcompat.widget.AppCompatImageView
        android:scaleType="fitXY"
        android:id="@+id/student_photo_preview"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:src="@drawable/student"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

JAVA代码:

getPicassoUnsafeCertificate(context).load(student.getPhoto())
                            .fit()
                            .networkPolicy(NetworkPolicy.NO_CACHE)
                            .memoryPolicy(MemoryPolicy.NO_CACHE)
                            .placeholder(R.drawable.no_image)
                            .error(R.drawable.no_image)
                            .into(previewImage);

我使用getPicassoUnsafeCertificate() 自定义方法来关闭 SSL 验证检查(我不关心这部分,但我添加了额外的好处)。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-03-30
    • 2015-08-01
    • 1970-01-01
    • 2014-03-10
    • 1970-01-01
    • 1970-01-01
    • 2016-02-11
    • 2019-11-23
    相关资源
    最近更新 更多