Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

like image 253
abeikverdi Avatar asked Jan 22 '16 06:01

abeikverdi


1 Answers

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);
like image 156
Knossos Avatar answered Sep 23 '22 02:09

Knossos