Here you can find the source of scaleBitmap(String path, int newWidth, int newHeight)
public static Bitmap scaleBitmap(String path, int newWidth, int newHeight)
//package com.java2s; import android.graphics.Bitmap; import android.graphics.Bitmap.Config; import android.graphics.BitmapFactory; import android.graphics.Canvas; import android.graphics.Matrix; import android.util.Log; public class Main { public static Bitmap scaleBitmap(Bitmap bitmap, int width, int height) { long start = System.currentTimeMillis(); if (bitmap == null) { return null; }// w w w . ja v a 2s. c o m int newWidth = width; int newHeight = height; if (newHeight == 0) { newHeight = (int) (newWidth / (float) bitmap.getWidth() * bitmap .getHeight()); } Bitmap result = Bitmap.createBitmap(newWidth, newHeight, Config.ARGB_8888); Canvas canvas = new Canvas(result); Matrix matrix = new Matrix(); float scaleX = 1; float scaleY = 1; scaleX = newWidth / (float) bitmap.getWidth(); if (height != 0) { scaleY = newHeight / (float) bitmap.getHeight(); } else { scaleY = scaleX; } matrix.postScale(scaleX, scaleY); canvas.drawBitmap(bitmap, matrix, null); bitmap.recycle(); Log.i("map", "map cost=" + (System.currentTimeMillis() - start)); return result; } public static Bitmap scaleBitmap(String path, int newWidth, int newHeight) { Bitmap bm = BitmapFactory.decodeFile(path); int width = bm.getWidth(); int height = bm.getHeight(); float scaleWidth = ((float) newWidth) / width; float scaleHeight = ((float) newHeight) / height; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); Bitmap newbm = Bitmap.createBitmap(bm, 0, 0, width, height, matrix, true); bm.recycle(); return newbm; } }