app/src/main/java/com/yahala/ImageLoader/bitmap/BitmapUtil.java
/**
     * Creates a new bitmap from the given one in the specified size respecting the size ratio of the origin image.
     *
     * @param b        original image
     * @param width    preferred width of the new image
     * @param height   preferred height of the new image
     * @param upsample if true smaller images than the preferred size are increased, if false the origin bitmap is returned
     * @return new bitmap if size has changed, otherwise original bitmap.
     */
Creates a new bitmap from the given one in the specified size respecting the size ratio of the origin image.
Creates a new bitmap from the given one in the specified size respecting the size ratio of the origin image .
public Bitmap scaleBitmap(Bitmap b, int width, int height, boolean upsampling) {
        int imageHeight = b.getHeight();
        int imageWidth = b.getWidth();
        if (!upsampling && imageHeight <= height && imageWidth <= width) {
            return b;
        }
        int finalWidth = width;
        int finalHeight = height;
        if (imageHeight > imageWidth) {
            float factor = ((float) height) / ((float) imageHeight);
            finalHeight = new Float(imageHeight * factor).intValue();
            finalWidth = new Float(imageWidth * factor).intValue();
        } else {
            float factor = ((float) width) / ((float) imageWidth);
            finalHeight = new Float(imageHeight * factor).intValue();
            finalWidth = new Float(imageWidth * factor).intValue();
        }
        Bitmap scaled = null;
        try {
            scaled = Bitmap.createScaledBitmap(b, finalWidth, finalHeight, true);
        } catch (final Throwable e) {
        }
        if (scaled != b) {
            recycle(b);
        }
        return scaled;
    }
public Bitmap scaleBitmap ( Bitmap b , int width , int height , boolean upsampling ) { int imageHeight = b . getHeight ( ) ; int imageWidth = b . getWidth ( ) ; if ( ! upsampling && imageHeight <= height && imageWidth <= width ) { return b ; } int finalWidth = width ; int finalHeight = height ; if ( imageHeight > imageWidth ) { float factor = ( ( float ) height ) / ( ( float ) imageHeight ) ; finalHeight = new Float ( imageHeight * factor ) . intValue ( ) ; finalWidth = new Float ( imageWidth * factor ) . intValue ( ) ; } else { float factor = ( ( float ) width ) / ( ( float ) imageWidth ) ; finalHeight = new Float ( imageHeight * factor ) . intValue ( ) ; finalWidth = new Float ( imageWidth * factor ) . intValue ( ) ; } Bitmap scaled = null ; try { scaled = Bitmap . createScaledBitmap ( b , finalWidth , finalHeight , true ) ; } catch ( final Throwable e ) { } if ( scaled != b ) { recycle ( b ) ; } return scaled ; }
Creates a new bitmap from the given one in the specified size respecting the size ratio of the origin image.
Creates a new bitmap from the given one in the specified size respecting the size ratio of the origin image .
// calling gc does not help as is called anyway // http://code.google.com/p/android/issues/detail?id=8488#c80 // System.gc(); // recycle b only if createScaledBitmap returned a new instance.