static public Bitmap resizeBitmap(Bitmap bmSrc,Rect rectDest,ScaleMode scaleMode,boolean fitBitmapResult,boolean scaleConditionally,boolean recycleBmpSource){
  Matrix mat=new Matrix();
  Bitmap bmResult;
  int wOrig=bmSrc.getWidth();
  int hOrig=bmSrc.getHeight();
  int wScaleTo=rectDest.width();
  int hScaleTo=rectDest.height();
  if (wScaleTo == 0 && hScaleTo == 0)   throw new Error("resizeBitmap: need at least $scaleTo.width or $scaleTo.height");
  float arW=(wScaleTo != 0) ? (float)wScaleTo / (float)wOrig : Float.NaN;
  float arH=(hScaleTo != 0) ? (float)hScaleTo / (float)hOrig : Float.NaN;
  arW=Float.isNaN(arW) ? arH : arW;
  arH=Float.isNaN(arH) ? arW : arH;
  float arMin=Math.min(arW,arH);
  float arMax=Math.max(arW,arH);
  wScaleTo=(wScaleTo != 0) ? wScaleTo : (int)(((float)wOrig) * arW);
  hScaleTo=(hScaleTo != 0) ? hScaleTo : (int)(((float)hOrig) * arH);
  if (scaleConditionally) {
    boolean isScaleConditionally=(wScaleTo >= bmSrc.getWidth()) && (hScaleTo >= bmSrc.getHeight());
    if (isScaleConditionally)     return Bitmap.createBitmap(bmSrc);
  }
  bmResult=Bitmap.createBitmap(wScaleTo,hScaleTo,Config.ARGB_8888);
  Canvas canvas=new Canvas(bmResult);
switch (scaleMode) {
case STRETCH:
{
      mat.reset();
      mat.postScale(arW,arH);
      canvas.drawBitmap(bmSrc,mat,null);
      break;
    }
case LETTERBOX:
{
    if (fitBitmapResult) {
      if (bmResult != null)       bmResult.recycle();
      wScaleTo=(int)((float)wOrig * arMin);
      hScaleTo=(int)((float)hOrig * arMin);
      bmResult=Bitmap.createBitmap(wScaleTo,hScaleTo,Config.ARGB_8888);
      canvas.setBitmap(bmResult);
    }
    mat.reset();
    mat.postScale(arMin,arMin);
    canvas.drawBitmap(bmSrc,mat,null);
    break;
  }
case ZOOM:
{
  mat.reset();
  mat.postScale(arMax,arMax);
  canvas.drawBitmap(bmSrc,mat,null);
  break;
}
case NONE:
{
mat.reset();
canvas.drawBitmap(bmSrc,mat,null);
break;
}
}
if (recycleBmpSource) bmSrc.recycle();
return bmResult;
}

static public Bitmap resizeBitmap ( Bitmap bmSrc , Rect rectDest , ScaleMode scaleMode , boolean fitBitmapResult , boolean scaleConditionally , boolean recycleBmpSource ) { Matrix mat = new Matrix ( ) ; Bitmap bmResult ; int wOrig = bmSrc . getWidth ( ) ; int hOrig = bmSrc . getHeight ( ) ; int wScaleTo = rectDest . width ( ) ; int hScaleTo = rectDest . height ( ) ; if ( wScaleTo == 0 && hScaleTo == 0 ) throw new Error ( "resizeBitmap: need at least $scaleTo.width or $scaleTo.height" ) ; float arW = ( wScaleTo != 0 ) ? ( float ) wScaleTo / ( float ) wOrig : Float . NaN ; float arH = ( hScaleTo != 0 ) ? ( float ) hScaleTo / ( float ) hOrig : Float . NaN ; arW = Float . isNaN ( arW ) ? arH : arW ; arH = Float . isNaN ( arH ) ? arW : arH ; float arMin = Math . min ( arW , arH ) ; float arMax = Math . max ( arW , arH ) ; wScaleTo = ( wScaleTo != 0 ) ? wScaleTo : ( int ) ( ( ( float ) wOrig ) * arW ) ; hScaleTo = ( hScaleTo != 0 ) ? hScaleTo : ( int ) ( ( ( float ) hOrig ) * arH ) ; if ( scaleConditionally ) { boolean isScaleConditionally = ( wScaleTo >= bmSrc . getWidth ( ) ) && ( hScaleTo >= bmSrc . getHeight ( ) ) ; if ( isScaleConditionally ) return Bitmap . createBitmap ( bmSrc ) ; } bmResult = Bitmap . createBitmap ( wScaleTo , hScaleTo , Config . ARGB_8888 ) ; Canvas canvas = new Canvas ( bmResult ) ; switch ( scaleMode ) { case STRETCH : { mat . reset ( ) ; mat . postScale ( arW , arH ) ; canvas . drawBitmap ( bmSrc , mat , null ) ; break ; } case LETTERBOX : { if ( fitBitmapResult ) { if ( bmResult != null ) bmResult . recycle ( ) ; wScaleTo = ( int ) ( ( float ) wOrig * arMin ) ; hScaleTo = ( int ) ( ( float ) hOrig * arMin ) ; bmResult = Bitmap . createBitmap ( wScaleTo , hScaleTo , Config . ARGB_8888 ) ; canvas . setBitmap ( bmResult ) ; } mat . reset ( ) ; mat . postScale ( arMin , arMin ) ; canvas . drawBitmap ( bmSrc , mat , null ) ; break ; } case ZOOM : { mat . reset ( ) ; mat . postScale ( arMax , arMax ) ; canvas . drawBitmap ( bmSrc , mat , null ) ; break ; } case NONE : { mat . reset ( ) ; canvas . drawBitmap ( bmSrc , mat , null ) ; break ; } } if ( recycleBmpSource ) bmSrc . recycle ( ) ; return bmResult ; }
not the regular bitmap resizer . much smarter with resizing and cropping options and resizing with proportions .
