Java tutorial
//package com.java2s; //License from project: Open Source License import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Matrix; import android.graphics.Bitmap.Config; public class Main { public static Bitmap getDrawable(String path, int zoom, int mItemwidth, int mItemHerght) { BitmapFactory.Options options = new BitmapFactory.Options(); options.inJustDecodeBounds = true; BitmapFactory.decodeFile(path, options); int mWidth = options.outWidth; int mHeight = options.outHeight; int s = 1; while ((mWidth / s > mItemwidth * 2 * zoom) || (mHeight / s > mItemHerght * 2 * zoom)) { s *= 2; } options = new BitmapFactory.Options(); options.inPreferredConfig = Config.ARGB_8888; options.inSampleSize = s; Bitmap bm = BitmapFactory.decodeFile(path, options); if (bm != null) { int h = bm.getHeight(); int w = bm.getWidth(); float ft = (float) ((float) w / (float) h); float fs = (float) ((float) mItemwidth / (float) mItemHerght); int neww = ft >= fs ? mItemwidth * zoom : (int) (mItemHerght * zoom * ft); int newh = ft >= fs ? (int) (mItemwidth * zoom / ft) : mItemHerght * zoom; float scaleWidth = ((float) neww) / w; float scaleHeight = ((float) newh) / h; Matrix matrix = new Matrix(); matrix.postScale(scaleWidth, scaleHeight); bm = Bitmap.createBitmap(bm, 0, 0, w, h, matrix, true); return bm; } return null; } }