0

I want to draw a bit my according to dimensions that I will set later on. But the only method Im familliar with is the following one:

canvas.drawBitmap(test, canvas.getWidth()/2 - test.getWidth()/2, canvas.getHeight()/2  - test.getHeight()/2, null);

which only draw the bitmap accordign to the image dimension, so my question is, is there another method to draw a bitmap with different dimension or just a way to change it?

Thanks!

Golan Kiviti
  • 3,895
  • 7
  • 38
  • 63

1 Answers1

0

Use below code to resize bitmap with your choice dimensions:

public static Bitmap decodeSampledBitmapFromPath(String path, int reqWidth,
            int reqHeight) {

   final BitmapFactory.Options options = new BitmapFactory.Options();
   options.inJustDecodeBounds = true;
   BitmapFactory.decodeFile(path, options);

   options.inSampleSize = calculateInSampleSize(options, reqWidth,
            reqHeight);

    // Decode bitmap with inSampleSize set
    options.inJustDecodeBounds = false;
    Bitmap bmp = BitmapFactory.decodeFile(path, options);
    return bmp;
}

public static int calculateInSampleSize(BitmapFactory.Options options,
         int reqWidth, int reqHeight) {

    final int height = options.outHeight;
    final int width = options.outWidth;
    int inSampleSize = 1;

    if (height > reqHeight || width > reqWidth) {
        if (width > height) {
                inSampleSize = Math.round((float) height / (float) reqHeight);
        } else {
                inSampleSize = Math.round((float) width / (float) reqWidth);
        }
    }
 return inSampleSize;
 }
Anil Jadhav
  • 2,128
  • 1
  • 17
  • 31