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?
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
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
}
});
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With