user3011902
user3011902

Reputation:

Picasso Android Only Load Images above a Certain Scale

I am using the Picasso Library to load Images into a Listview form several different online sources. Some Images however are too small and end up looking blurred when applying them to the View. Is there a way to Check what size the Images are when loading them in using Picasso? Here is My Code:

 Picasso.with(MainActivity.context)
                    .load(imgUrl)
                    .transform(new BitmapTransform(MAX_WIDTH, MAX_HEIGHT))
                    .skipMemoryCache()
                    .resize(size, size)
                    .centerInside()
                    .into(target);

And the Transform:

class BitmapTransform implements Transformation {

int maxWidth;
int maxHeight;

public BitmapTransform(int maxWidth, int maxHeight) {
    this.maxWidth = maxWidth;
    this.maxHeight = maxHeight;
}

@Override
public Bitmap transform(Bitmap source) {
    int targetWidth, targetHeight;
    double aspectRatio;
    int maxWidthTmp = maxWidth;
    int maxHeightTmp = maxHeight;

    if (source.getWidth() > source.getHeight()) {
        targetWidth = maxWidthTmp;
        aspectRatio = (double) source.getHeight() / (double) source.getWidth();
        targetHeight = (int) (targetWidth * aspectRatio);
    } else {
        targetHeight = maxHeightTmp;
        aspectRatio = (double) source.getWidth() / (double) source.getHeight();
        targetWidth = (int) (targetHeight * aspectRatio);
    }

    Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);
    if (result != source) {
        source.recycle();
    }
    return result;
}

@Override
public String key() {
    return maxWidth + "x" + maxHeight;
}

};

I have tried to check the scale of the Source Paramater that goes into the Transform method, but the width is always: 666. Any suggestions on how to do this?

Thank you

Upvotes: 0

Views: 146

Answers (1)

Fahim
Fahim

Reputation: 12358

Try this

private Target target = new Target() {
            @Override
            public void onBitmapLoaded(Bitmap bitmap, Picasso.LoadedFrom from)      {  
                int width =bitmap.getWidth();
                int height =bitmap.getHeight();
            }
            @Override
            public void onBitmapFailed() {
            }
      }

private void someMethod() {
   Picasso.with(this).load("url").into(target);
}

Hope this will help

Upvotes: 0

Related Questions