calculate In Sample Size by Bitmap options - Android android.graphics

Android examples for android.graphics:Bitmap Size

Description

calculate In Sample Size by Bitmap options

Demo Code

import android.graphics.BitmapFactory;

public class Main {

  public static int calculateInSampleSize(BitmapFactory.Options options, int reqWidth, int reqHeight) {
    int inSampleSize = 1; 
    if (options.outHeight > reqHeight || options.outWidth > reqWidth) {
      final int halfHeight = options.outHeight / 2;
      final int halfWidth = options.outWidth / 2;
      while ((halfHeight / inSampleSize) > reqHeight && (halfWidth / inSampleSize) > reqWidth) {
        inSampleSize *= 2;//from  w  w  w. ja  v a  2 s  . c o m
      }
    }
    return inSampleSize;
  }
}

Related Tutorials