Resizing Bitmap after Loading it with Universal Image Loader
Asked Answered
E

4

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?

Elidaelidad answered 24/6, 2014 at 20:21 Comment(3)
#4838215Pythagoreanism
I would suggest you use the Picasso library. It's allows very easy image loading and manipulation, while still being flexible.Maryn
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.Sites
I
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

Indiana answered 3/7, 2014 at 13:24 Comment(0)
O
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
    }
});
Ornate answered 1/7, 2014 at 6:9 Comment(0)
G
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.

Gar answered 3/7, 2014 at 10:26 Comment(0)
G
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);
Galligan answered 4/7, 2014 at 10:52 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.