abeikverdi
abeikverdi

Reputation: 1226

Picasso's Target and centerCrop() and fit()

I have an image on my server and I want to display it using Picasso on my Android client. I want to add a default image when the image is loading on Picasso so I am using Target as follows:

Picasso.with(UserActivity.this).load(imageUri.toString()).transform(new RoundedTransformation(500, 1)).into(
new Target() {
    @Override
    public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from) {
        userPic.setImageBitmap(bitmap);
    }

    @Override
    public void onBitmapFailed(Drawable drawable) {
        userPic.setImageBitmap(defaultDrawable);
    }

    @Override
    public void onPrepareLoad(Drawable drawable) {
        userPic.setImageBitmap(defaultDrawable);
    }
});

I want to centerCrop() and fit() this image but it gives me an error and it tells me that I cant use them with Target. Is there anyway to use these features on Picasso? Why don't they allow these two functions with Target?

Upvotes: 5

Views: 10799

Answers (3)

Mahesh Haldar
Mahesh Haldar

Reputation: 591

We can also resize the image as required by the imageview which will save memory usage if the image too large.

Callback method can be used to hide the progress bar and show some text within the image View on image load fail.

Picasso.with(context)
.load(url)
.placeholder(R.drawable.placeholder_img)
.error(R.drawable.error_img)
.resize(450, 420)
.centerCrop()
.fit()
.into(imageView, new Callback() {
        @Override
        public void onSuccess() {
            progressBar.setVisibility(View.GONE);
        }

        @Override
        public void onError() {
            progressBar.setVisibility(View.GONE);
            image_failed_text.setVisibility(View.VISIBLE);
        }
    });

Upvotes: 2

kishorepatel
kishorepatel

Reputation: 139

Try this

Picasso.with(context)
  .load(url)
  .resize(50, 50)
  .centerCrop()
  .fit()
  .placeholder(defaultImageLink)
  .error(R.drawable.user_placeholder_error)
  .transform(new RoundedTransformation(500, 1))
  .into(imageView)

Upvotes: 3

Knossos
Knossos

Reputation: 16068

You don't need to use Target to accomplish your goal.

Side note, I am not certain that you can actually use both fit() and centerCrop() together.

See this example:

Picasso.with(context)
    .load(url) // Equivalent of what ends up in onBitmapLoaded
    .placeholder(R.drawable.user_placeholder) // Equivalent of what ends up in onPrepareLoad
    .error(R.drawable.user_placeholder_error) // Equivalent of what ends up in onBitmapFailed
    .centerCrop()
    .fit()
    .into(imageView);

Upvotes: 12

Related Questions