Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

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.

like image 387
pseudocode Avatar asked Mar 10 '26 18:03

pseudocode


1 Answers

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.

like image 103
Federik Avatar answered Mar 12 '26 07:03

Federik



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!