1
votes

I know that by using fit() and resize(), the image gets downsized before downloading. Yet for times when listView is used and the images' ratios are varying, I needa set the imageView's layout_width and layout_height as wrap_content, and then use transform() to determine the image's ratio before putting it into the imageView.

eg.

        Picasso.with(mContext).load(region.getImage_url()).transform(new Transformation() {
            @Override
            public Bitmap transform(Bitmap source) {
                int targetWidth = getContext().getSharedPreferences(Constants.PREFERENCES, Context.MODE_PRIVATE).getInt(Constants.DEVICE_WIDTH, 0);
                double aspectRatio = (double) source.getHeight() / (double) source.getWidth();
                int targetHeight = (int) (holder.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;
            }

            @Override
            public String key() {
                return "transformation" + " desiredWidth";
            }
        }).into(holder.imageView);

So my problem is will Picasso's transform method also deal with the downsizing of the images before downloading? If not, what is the best approach towards it, while not setting fixed ratio to the listview's imageViews?

2
Do you mean you want to reduce the size of an image before you download it?Harry
yes, that's what I want. Or did I get any mistake? I primary goal is to make the loading of images efficient.Derekyy
No, I got your point. I've been working a lot in creating an efficient bandwidth usage. You can see my answer if that could help you.Harry
the image gets downsized before downloading how do you expect to do that exactly?njzk2

2 Answers

3
votes

I also work with Picasso, and my answer is "no". Images are always being downloaded first, then resized/transformed, then displayed.

The only way to download a reduced size (to be displayed in ListView) is to provide a smaller size or thumbnail version of the image. It is easy to achieve if you host the images in your own server.

2
votes

You can use Thumbor service to crop and resize images before downloading. Square has a nice Java client for it - Pollexor. And special Picasso RequestTransformer for Pollexor. That said, you'll get all the preprocessing extracted to the backend.