Android 毕加索 "fit().centerCrop()" 没有加载图像

Android Picasso "fit().centerCrop()" doesn't load image

我想在我的图像视图中显示图像 (URL) 作为最大宽度和可变高度根据宽度大小,当我使用此代码部分时:

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 什么都不显示。我不明白为什么。也许是我的列表设计和图像视图的问题。这是我的 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>

我尝试设置 wrap_contentmatch_parent ImageView 的宽度和高度,但没有成功也工作了。我哪里不见了?提前致谢。

首先,您没有在使用 fit() 之前添加 .。 用这个替换你的代码: Picasso.with(context).load(product.getImage()).fit().centerCrop().into(imageView)

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

然后将您的图像放入图像视图:Picasso.with(context).load(product. getImage).fit().into(imageView);

adjustViewBounds 成功了:

android:layout_width="match_parent"
android:layout_height="match_parent"
android:adjustViewBounds="true"
android:scaleType="centerCrop"

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

public 生成器调整大小(int targetWidth,int targetHeight)

Resize the image to the specified size in pixels. Use 0 as desired dimension to resize keeping aspect ratio.

使用这个。

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

    return displayMetrics.widthPixels
}


.resize(getDeviceWidth(), 0)