Java tutorial
//package com.java2s; import android.graphics.BitmapFactory; public class Main { /** * Calculate an inSampleSize for use in a * {@link BitmapFactory.Options} object when decoding * bitmaps using the decode* methods from * {@link BitmapFactory}. This implementation calculates * the closest inSampleSize that will result in the final decoded bitmap * having a width and height equal to or larger than the requested width and * height. This implementation does not ensure a power of 2 is returned for * inSampleSize which can be faster when decoding but results in a larger * bitmap which isn't as useful for caching purposes. * * @param options An options object with out* params already populated (run * through a decode* method with inJustDecodeBounds==true * @param reqWidth The requested width of the resulting bitmap * @param reqHeight The requested height of the resulting bitmap * @return The value to be used for inSampleSize */ public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) { // Raw height and width of image final int height = options.outHeight; final int width = options.outWidth; int inSampleSize = 1; if (height > reqHeight || width > reqWidth) { int widthSampleSize = 0; int heightSampleSize = 0; if (reqWidth < width) { widthSampleSize = Math.round((float) width / (float) reqWidth); } if (reqHeight < height) { heightSampleSize = Math.round((float) height / (float) reqHeight); } inSampleSize = Math.max(widthSampleSize, heightSampleSize); } return inSampleSize; } }