public Bitmap decodeBestRegion(int requiredWidth,int requiredHeight,int gravity){
  boolean upscaling=false;
  int realWidth=getWidth();
  int realHeight=getHeight();
  BitmapFactory.Options options=new BitmapFactory.Options();
  if (!(requiredWidth <= realWidth && requiredHeight <= realHeight)) {
    float widthRatio=(float)requiredWidth / realWidth;
    float heightRatio=(float)requiredHeight / realHeight;
    float destinationRatio=Math.max(widthRatio,heightRatio);
    requiredWidth=Math.round(((float)requiredWidth / destinationRatio));
    requiredHeight=Math.round(((float)requiredHeight / destinationRatio));
    upscaling=true;
  }
  boolean limitReached=false;
  int basePow=0;
  int actualScale;
  while (!limitReached) {
    actualScale=(int)Math.pow(2,basePow + 1);
    if (requiredWidth <= realWidth / actualScale && requiredHeight <= realHeight / actualScale) {
      basePow++;
    }
 else {
      limitReached=true;
    }
  }
  options.inSampleSize=(int)Math.pow(2,basePow);
  int fromTop=0;
  int fromLeft=0;
  if (gravity == Gravity.CENTER) {
    fromTop=(realHeight - requiredHeight * options.inSampleSize) / 2;
    fromLeft=(realWidth - requiredWidth * options.inSampleSize) / 2;
  }
 else {
    int horizontalGravity=gravity & Gravity.HORIZONTAL_GRAVITY_MASK;
    int verticalGravity=gravity & Gravity.VERTICAL_GRAVITY_MASK;
    if (horizontalGravity == Gravity.LEFT)     fromLeft=0;
 else     if (horizontalGravity == Gravity.RIGHT)     fromLeft=realWidth - requiredWidth * options.inSampleSize;
 else     if (horizontalGravity == Gravity.CENTER_HORIZONTAL)     fromLeft=(realWidth - requiredWidth * options.inSampleSize) / 2;
    if (verticalGravity == Gravity.TOP)     fromTop=0;
 else     if (verticalGravity == Gravity.BOTTOM)     fromTop=realHeight - requiredHeight * options.inSampleSize;
 else     if (verticalGravity == Gravity.CENTER_VERTICAL)     fromTop=(realHeight - requiredHeight * options.inSampleSize) / 2;
  }
  Rect area=new Rect(fromLeft,fromTop,fromLeft + requiredWidth * options.inSampleSize,fromTop + requiredHeight * options.inSampleSize);
  return impl.decodeRegion(area,options);
}

public Bitmap decodeBestRegion ( int requiredWidth , int requiredHeight , int gravity ) { boolean upscaling = false ; int realWidth = getWidth ( ) ; int realHeight = getHeight ( ) ; BitmapFactory . Options options = new BitmapFactory . Options ( ) ; if ( ! ( requiredWidth <= realWidth && requiredHeight <= realHeight ) ) { float widthRatio = ( float ) requiredWidth / realWidth ; float heightRatio = ( float ) requiredHeight / realHeight ; float destinationRatio = Math . max ( widthRatio , heightRatio ) ; requiredWidth = Math . round ( ( ( float ) requiredWidth / destinationRatio ) ) ; requiredHeight = Math . round ( ( ( float ) requiredHeight / destinationRatio ) ) ; upscaling = true ; } boolean limitReached = false ; int basePow = 0 ; int actualScale ; while ( ! limitReached ) { actualScale = ( int ) Math . pow ( 2 , basePow + 1 ) ; if ( requiredWidth <= realWidth / actualScale && requiredHeight <= realHeight / actualScale ) { basePow ++ ; } else { limitReached = true ; } } options . inSampleSize = ( int ) Math . pow ( 2 , basePow ) ; int fromTop = 0 ; int fromLeft = 0 ; if ( gravity == Gravity . CENTER ) { fromTop = ( realHeight - requiredHeight * options . inSampleSize ) / 2 ; fromLeft = ( realWidth - requiredWidth * options . inSampleSize ) / 2 ; } else { int horizontalGravity = gravity & Gravity . HORIZONTAL_GRAVITY_MASK ; int verticalGravity = gravity & Gravity . VERTICAL_GRAVITY_MASK ; if ( horizontalGravity == Gravity . LEFT ) fromLeft = 0 ; else if ( horizontalGravity == Gravity . RIGHT ) fromLeft = realWidth - requiredWidth * options . inSampleSize ; else if ( horizontalGravity == Gravity . CENTER_HORIZONTAL ) fromLeft = ( realWidth - requiredWidth * options . inSampleSize ) / 2 ; if ( verticalGravity == Gravity . TOP ) fromTop = 0 ; else if ( verticalGravity == Gravity . BOTTOM ) fromTop = realHeight - requiredHeight * options . inSampleSize ; else if ( verticalGravity == Gravity . CENTER_VERTICAL ) fromTop = ( realHeight - requiredHeight * options . inSampleSize ) / 2 ; } Rect area = new Rect ( fromLeft , fromTop , fromLeft + requiredWidth * options . inSampleSize , fromTop + requiredHeight * options . inSampleSize ) ; return impl . decodeRegion ( area , options ) ; }
extracts the " best " rectangle region based on the specified dimensions . the " best " region means the biggest region of the original ( downsampled ) image , given the required gravity and output sizes / ratio . you should use this method when you have a big source image that have to be fit / be partially displayed .
