6

I am loading an image with Universal Image Loader, and I'd like to scale it so that the width is the width of the screen and the height is scaled accordingly, meaning that before I load the image I know the width that I want, but not the height. So when I load the image I want to get the height and width of the image and use that to scale it according to the width of the screen. The code I use to do this is this:

try {
    display.getSize(size);
    scaledWidth = size.x;
} catch (java.lang.NoSuchMethodError ignore) {
    scaledWidth = display.getWidth();
}

String filePath = "file://" + getBaseContext().getFilesDir().getPath().toString() + "/" + imagePath + ".png";
Bitmap bitmap = imageLoader.loadImageSync(filePath);
int height = bitmap.getHeight();
int width = bitmap.getWidth();
scaledHeight =  (int) (((scaledWidth * 1.0) / width) * height);
//Code to resize Bitmap using scaledWidth and scaledHeight

What's the best way to resize the Bitmap using Universal Image Loader, or even better, is there a way that I can specify only the width and the Bitmap is scaled properly based on it's proportions?

shadowarcher
  • 2,845
  • 5
  • 17
  • 31
  • http://stackoverflow.com/questions/4837715/how-to-resize-a-bitmap-in-android – kupsef Jun 28 '14 at 07:01
  • I would suggest you use the [Picasso library](http://square.github.io/picasso/). It's allows very easy image loading and manipulation, while still being flexible. – michael_ Jul 02 '14 at 12:57
  • My solution is based on @nitesh-goel code and @zhaoyuanjie `DisplayImageOptions` but with `ImageScaleType.EXACTLY_STRETCHED`. Note that image will look blurry if you scale it up too much. – Leukipp Feb 27 '18 at 08:31

4 Answers4

9

You needn't do it yourself. Consider using ImageScaleType.EXACTLY

new DisplayImageOptions.Builder().imageScaleType(ImageScaleType.EXACTLY)

https://github.com/nostra13/Android-Universal-Image-Loader/blob/master/library/src/com/nostra13/universalimageloader/core/assist/ImageScaleType.java

Peter Zhao
  • 6,956
  • 3
  • 19
  • 22
2

use can use

// Load image, decode it to Bitmap and return Bitmap to callback
ImageSize targetSize = new ImageSize(120, 80); // result Bitmap will be fit to this size
imageLoader.loadImage(imageUri, targetSize, displayOptions, new SimpleImageLoadingListener() {
    @Override
    public void onLoadingComplete(String imageUri, View view, Bitmap loadedImage) {
        // Do whatever you want with Bitmap
    }
});
nitesh goel
  • 6,108
  • 2
  • 26
  • 37
0

I haven't read your code but the calculations for determining the height are pretty easy. What we want is to keep the aspect ratio of the image intact. So first calculate the aspect_ratio i.e

imageHeight/imageWidth = aspt_ratio;

then take this aspect_ratio and multiply it with the current screen width to get the height.

scaledHeight = aspt_ratio*screen_width;

since we know that the scaled width of the image will always be equal to the screen width,according to your requirement.

Parvaz Bhaskar
  • 1,355
  • 8
  • 28
0

This will work as you need

scaledWidth = size.x;

String filePath = "file://" + getBaseContext().getFilesDir().getPath().toString() + "/" + imagePath + ".png";

    android.graphics.BitmapFactory.Options options= new Options();
        options.inJustDecodeBounds=true;
 //Just gets image size without allocating memory
BitmapFactory.decodeFile(filePath, options);


int height = options.outHeight;
int width = bitmap.outWidth;
scaledHeight =  (int) (((scaledWidth * 1.0) / width) * height);


ImageSize targetSize = new ImageSize(scaledWidth, scaledHeight); 
Bitmap bmp = imageLoader.loadImageSync(imageUri, targetSize, displayOptions);
Abhimaan
  • 1,875
  • 2
  • 18
  • 26