dev90
dev90

Reputation: 7579

How to choose values for resize() value in Picasso

I have added an Image View, and set these values.

 <ImageView
            android:id="@ivNewsHeader
            android:layout_width="match_parent"
            android:layout_height="250dp"
            android:scaleType="centerCrop" />

I get images from server, and they are very high res images, if i add them as is, i end up getting OutOfMemoryError.

So i learnt that picasso offers a great way to tackle this. I used following, and this scale down any image which is higher than the mentioned dimensions.

Picasso.with(getApplicationContext()).load(imageURL).
                                    resize(180, 130).
                                    onlyScaleDown()
                                    .into(ivNews);

Now when i choose these resize(180, 130), the performance is very good and i don't get any OutOfMemoryError, but the image quality is very poor.

So my question is how to choose resize number, and what equation should is use to put correct width-height numbers in resize() method to get the perfect image without compromising the quality and performance.

My imageView height is 250dp and width is match_parent

Upvotes: 1

Views: 653

Answers (3)

dharmendra
dharmendra

Reputation: 7881

Try using fit() which is measuring the dimensions of the target ImageView and internally uses resize() to reduce the image size to the dimensions of the ImageView.

Quoting from this article

Upvotes: 1

Cătălin Florescu
Cătălin Florescu

Reputation: 5158

I think fit() is what you need, because will crop your image to ImageView size on screen. In this case, you don't need do to any more calculations.

Picasso.with(getApplicationContext())
       .load(imageURL)
       .fit()
       .centerInside()
       .into(imageView);

Upvotes: 1

MJM
MJM

Reputation: 5311

Specify scale value based on device size

Device width in pixel

 public static int getScreenWidth() {
        return Resources.getSystem().getDisplayMetrics().widthPixels;
    }

Convert DP to Pixel

public static int dpToPx(int dp)
{
    return (int) (dp * Resources.getSystem().getDisplayMetrics().density);
}

Now use these two function

Picasso.with(getApplicationContext()).load(imageURL).
                                    resize(getScreenWidth(), dpToPx(250)).
                                    onlyScaleDown()
                                    .into(ivNews);

Upvotes: 2

Related Questions