Sophie
Sophie

Reputation: 2634

Parsing online images into ImageView

I am trying to parse online images into ImageView using JSON and for that i am using Picasso library

But i am not getting online image into ImageView due to big size of image, Width: 4608 pixels and Height: 2592 pixels

    Picasso.with(context)
    .load(imageURL)
    .noFade()
    .placeholder(R.drawable.ic_launcher)
    .error(R.drawable.ic_launcher)
    .into(viewHolder.imageView);

Note:- I am getting small size image into ImageView successfully

Upvotes: 0

Views: 212

Answers (2)

Huzefa Gadi
Huzefa Gadi

Reputation: 1149

use this

Picasso.with(context)
    .load(imageURL)
    .noFade()
    .fit()
    .centerCrop()
    .placeholder(R.drawable.ic_launcher)
    .error(R.drawable.ic_launcher)
    .into(viewHolder.imageView);

Upvotes: 0

Raghunandan
Raghunandan

Reputation: 133560

You can apply custom transforms.

I used the below to scale the image keeping the aspect ratio

Transformation transformation = new Transformation() {
@Override 
public Bitmap transform(Bitmap source) {

            int targetWidth = width;
            double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
            int targetHeight = (int) (targetWidth  * aspectRatio);

            Bitmap result = Bitmap.createScaledBitmap(source, targetWidth, targetHeight, false);

            if (result != source) {
                // Same bitmap is returned if sizes are the same
                source.recycle();
            }

            return result;

        }

Then

Picasso.with(context).
          load("your url").transform(transformation)
          .into(holder.iv)

Look at image transformations @

http://square.github.io/picasso/

for adding custom transforms based on your requirement

You can also have a look@

https://futurestud.io/blog/picasso-image-resizing-scaling-and-fit/

Upvotes: 3

Related Questions