Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Resizing Bitmap after Loading it with Universal Image Loader

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?

like image 383
shadowarcher Avatar asked Jun 24 '14 20:06

shadowarcher


2 Answers

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

like image 156
Peter Zhao Avatar answered Nov 14 '22 22:11

Peter Zhao


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
    }
});
like image 26
nitesh goel Avatar answered Nov 14 '22 22:11

nitesh goel