Android Bitmap Scale scaleBitmapKeepRatio(Bitmap bitmap, int dstWidth, int dstHeight)

Here you can find the source of scaleBitmapKeepRatio(Bitmap bitmap, int dstWidth, int dstHeight)

Description

scale Bitmap Keep Ratio

License

Open Source License

Declaration

public static Bitmap scaleBitmapKeepRatio(Bitmap bitmap, int dstWidth,
            int dstHeight) 

Method Source Code

//package com.java2s;
//License from project: Open Source License 

import android.graphics.Bitmap;

import android.graphics.Matrix;

public class Main {
    public static Bitmap scaleBitmapKeepRatio(Bitmap bitmap, int dstWidth,
            int dstHeight) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();
        float ratio = ((float) w) / h;
        if (dstWidth > dstHeight) {
            dstWidth = (int) (dstHeight * ratio);
        } else {/*from ww  w  .j  a  v  a  2  s. co m*/
            dstHeight = (int) (dstWidth / ratio);
        }
        return scaleBitmap(bitmap, dstWidth, dstHeight);
    }

    public static Bitmap scaleBitmap(Bitmap bitmap, int dstWidth,
            int dstHeight) {
        int w = bitmap.getWidth();
        int h = bitmap.getHeight();

        Matrix matrix = new Matrix();
        float scaleWidth = ((float) dstWidth / w);
        float scaleHeight = ((float) dstHeight / h);
        matrix.postScale(scaleWidth, scaleHeight);

        return Bitmap.createBitmap(bitmap, 0, 0, w, h, matrix, true);
    }
}

Related

  1. getScaledBitmap(Bitmap bitmap, int width, int height)
  2. getScaledBitmapByHeight(Bitmap bm, int newHeight)
  3. scaleBitmap(Bitmap bitmap, int dstWidth, int dstHeight)
  4. scaleBitmap(Bitmap bitmap, int targetwidth)
  5. scaleBitmapIfNeededToSize(Bitmap bitmap, long size)
  6. scaleToTextSize(Bitmap bmp, float textSize)
  7. scaleToScreen(Activity activity, Bitmap bitmap)
  8. scaleImage(Bitmap image, float scale)
  9. scaleImg(Bitmap bitmap, float scale)