Android 安卓毕加索“;安装().中心裁剪();不';加载图像

Android 安卓毕加索“;安装().中心裁剪();不';加载图像,android,android-imageview,picasso,Android,Android Imageview,Picasso,当我使用此代码部分时,我想在imageview中显示图像(URL),作为最大宽度和根据宽度大小的可变高度: imageView = (ImageView) convertView.findViewById(R.id.imageView); Picasso.with(context).load(product.getImage()).into(imageView); 我可以在imageview中显示我的图像。我还尝试了使用resize().centerCrop()方法,结果成功 但是,当我使用

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

imageView = (ImageView) convertView.findViewById(R.id.imageView);
Picasso.with(context).load(product.getImage()).into(imageView);
我可以在imageview中显示我的图像。我还尝试了使用resize().centerCrop()方法,结果成功

但是,当我使用

毕加索.with(context).load(product.getImage()).fit().centerCrop().into(imageView)

毕加索.with(context).load(product.getImage()).fit().centerInside().into(imageView)

我的imageview没有显示任何内容。我不明白为什么。也许问题出在我的列表设计和图像视图上。这是我的列表内容\u项。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\u contentmatch\u parent,但也不起作用。我在哪里失踪?提前感谢。

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

作为替代方案 您还可以使用
android:scaletype=“fitXY”
在YounXML文件中


然后将图像放入imageview:
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属性比使用毕加索方法更容易配置此行为。

公共生成器调整大小(int-targetWidth,int-targetLight)

将图像调整为指定的大小(以像素为单位)。 使用0作为所需尺寸,以调整大小,保持纵横比不变

用这个

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

    return displayMetrics.widthPixels
}


.resize(getDeviceWidth(), 0)

使用日志记录查看毕加索的踪迹,使用
picasso.with(context.setLoggingEnabled)(true)
您可以将此行放入您的
应用程序中
以登录整个应用程序,或仅登录到您的活动/片段中。不要使用fit()方法,因为您同时使用fit和center-crop,您必须使用其中一种方法。示例图像可以同时使用fit和center-crop。我尝试过了。但它不起作用。imageview再次没有显示任何内容。谢谢,它成功了。我像这样更新了我的imageview,并像这样更改了我的代码:Picasso.with(context.load(product.getImage()).to(imageview);