Android Picasso的"fit().centerCrop()"无法加载图片。

3
我希望将图片(URL)显示在我的ImageView中,使其宽度最大,高度根据宽度大小可变。当我使用以下代码时:
imageView = (ImageView) convertView.findViewById(R.id.imageView);
Picasso.with(context).load(product.getImage()).into(imageView);

我可以将图片显示在ImageView中。我还尝试使用resize().centerCrop()方法,它也成功了。
但是,当我使用以下代码时:

Picasso.with(context).load(product.getImage()).fit().centerCrop().into(imageView);

或者

Picasso.with(context).load(product.getImage()).fit().centerInside().into(imageView);

我的ImageView没有显示出任何内容。我不明白为什么会这样,可能是我的列表设计和ImageView存在问题。这是我的list_content_item.xml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:orientation="vertical"
    android:layout_width="match_parent"
    android:layout_height="match_parent"

    android:weightSum="1">

    <ImageView 
        android:id="@+id/imageView"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:layout_weight="0.80"
        app:srcCompat="@mipmap/ic_launcher" />

    <LinearLayout

        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:weightSum="1"
        android:layout_weight="0.2"
        android:orientation="horizontal">
        <TextView
            android:id="@+id/tv_title"
            android:layout_width="match_parent"
            android:layout_weight="0.3"
            android:layout_height="match_parent" />
        <TextView
            android:id="@+id/tv_stats"
            android:layout_weight="0.7"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:gravity="center"/>

    </LinearLayout>

</LinearLayout>

我尝试将ImageView的宽度和高度都设置为wrap_contentmatch_parent,但仍然没有起作用。我错在哪里?谢谢。


使用日志记录 Picasso 的跟踪信息,可以通过 Picasso.with(context).setLoggingEnabled(true); 实现。您可以将此行代码放入您的 Application 中以记录整个应用程序,或者只需将其放入您的 Activity/Fragment 中即可。 - Cătălin Florescu
请勿使用fit()方法:Picasso无法加载图像。 - harun karaca
因为您同时使用了fit和center crop,所以必须只使用其中之一。示例图像可以适应或居中裁剪,但不能同时进行。 - tohid noori
3个回答

7
adjustViewBounds可以解决这个问题:
android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"

我发现使用XML属性而不是Picasso方法更容易配置这种行为。


1
谢谢,它起作用了。我像这样更新了我的ImageView并更改了我的代码:Picasso.with(context).load(product.getImage()).into(imageView); - pseudocode

0

首先,在使用fit()之前,您需要添加一个.。 请使用以下代码替换您的代码: Picasso.with(context).load(product.getImage()).fit().centerCrop().into(imageView)

作为替代方案,您也可以在xml文件中使用android:scaleType="fitXY"定义imageView的比例类型。

然后将图片加载到imageView中:Picasso.with(context).load(product.getImage()).fit().into(imageView);


1
我尝试了,但是没有成功。ImageView 再次没有显示任何内容。 - pseudocode

0

public Builder resize(int targetWidth, int targetHeight)

将图像调整为指定的像素大小。 使用0作为所需的维度以保持纵横比。

使用此功能。

fun Fragment.getDeviceWidth(): Int {
    val displayMetrics = DisplayMetrics()
    requireActivity().windowManager.defaultDisplay.getRealMetrics(displayMetrics)

    return displayMetrics.widthPixels
}


.resize(getDeviceWidth(), 0)

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接