Here you can find the source of scaleBitmapKeepRatio(Bitmap bitmap, int dstWidth, int dstHeight)
public static Bitmap scaleBitmapKeepRatio(Bitmap bitmap, int dstWidth, int dstHeight)
//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); } }