3
votes

I want to display image (URL) into my imageview as max width and variable height according to width size, when I use this code part:

imageView = (ImageView) convertView.findViewById(R.id.imageView);
Picasso.with(context).load(product.getImage()).into(imageView);

I can display my image into imageview. I also tried use resize().centerCrop() methods and it worked successfully.

But however, when I use

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

or

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

my imageview display nothing. I don't understand why. Maybe the problem my list design and imageview. Here is my 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>

I tried set wrap_content and match_parent both of ImageView's width and height and it didn't worked, too. Where am I missing? Thanks in advance.

3
Use logging to see trace of picasso using Picasso.with(context).setLoggingEnabled(true); You can put this line into your Application to log in entire app or just into your Activity/Fragment.Florescu Cătălin
because you use fit and center crop together you must use one of them .A sample image can fitted or center crop not together.tohid noori

3 Answers

8
votes

adjustViewBounds does the trick:

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

I find it easier to configure this behavior using XML attributes rather than Picasso methods.

0
votes

Firstly you are not adding a . before using fit(). Replace you code with this: Picasso.with(context).load(product.getImage()).fit().centerCrop().into(imageView)

As an Alternative you can also define the scaletype to your imageView usingandroid:scaleType="fitXY" In youn xml file.

Then get your image into imageview: Picasso.with(context).load(product. getImage).fit().into(imageView);

0
votes

public Builder resize(int targetWidth, int targetHeight)

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

use this.

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

    return displayMetrics.widthPixels
}


.resize(getDeviceWidth(), 0)