Resize Image : Image « 2D Graphics « Android






Resize Image

   
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Matrix;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;

class MyUtils {
  public MyUtils() {
    
  }
  
  public static Drawable resizeImage(Context ctx, int resId, int w, int h) {

      // load the origial Bitmap
      Bitmap BitmapOrg = BitmapFactory.decodeResource(ctx.getResources(),
                                                      resId);

      int width = BitmapOrg.getWidth();
      int height = BitmapOrg.getHeight();
      int newWidth = w;
      int newHeight = newWidth * height / width;

      // calculate the scale
      float scaleWidth = ((float) newWidth) / width;
      float scaleHeight = ((float) newHeight) / height;

      // create a matrix for the manipulation
      Matrix matrix = new Matrix();
      // resize the Bitmap
      matrix.postScale(scaleWidth, scaleHeight);
      // if you want to rotate the Bitmap
      // matrix.postRotate(45);

      // recreate the new Bitmap
      Bitmap resizedBitmap = Bitmap.createBitmap(BitmapOrg, 0, 0,
                                                 width, height, matrix, true);

      // make a Drawable from Bitmap to allow to set the Bitmap
      // to the ImageView, ImageButton or what ever
      return new BitmapDrawable(resizedBitmap);

    }
}

   
    
    
  








Related examples in the same category

1.Capture Image
2.extends BaseAdapter to create Image adapter
3.extends BaseAdapter to create adapter for images
4.Capture Image and display
5.Load up the image's dimensions
6.Lazy Loading Image
7.Fit Image No Margin
8.To Rotate Texture Image
9.Image Resize
10.Create Image and resize Image
11.image To Byte
12.Image Loader
13.Save Image and Text to SD card
14.Scale and rotate Image
15.create Image
16.Resize Photo