0

When I create a Canvas from a bitmap that is generated with the BitmapFactory.decodeResources from a drawable resource that is a png image file of 400 x 400 pixels, the Canvas height and width are not 400 but 600! In the code below, the drawable resource wind_scale is a png file that I generated with PHP as a 400 x 400 image.

    Bitmap workingBitmap=BitmapFactory.decodeResource(getResources(), R.drawable.wind_scale, options);
    Bitmap mutableBitmap = workingBitmap.copy(Bitmap.Config.ARGB_8888, true);
    Canvas mCanvas = new Canvas(mutableBitmap);
    int mHeight = mCanvas.getHeight(); int mWidth = mCanvas.getWidth(); both 600!!!!

Is this behavior expected? If so, why is there a 50% increase?

alan.raceQs
  • 411
  • 4
  • 11

2 Answers2

0

I am not 100% sure of the problem here but it's similar to another SO question (Getting weird font size after setting it programmatically). The problem there was also a 50% difference in values, and it was due to a comparison of sp units to pixels on the OP's device with a screen density ratio of 1.5.

You might check into BitmapFactory.Options; there are several scale and density flags that may help. My best guess is that the image is being scaled for the device and this is causing a measurement mismatch.

Community
  • 1
  • 1
sighrobot
  • 447
  • 3
  • 16
0

Canvas it's not related to the bitmap size but to the device available screen size. If you try that code on different devices (or virtual devices) you will find different results.

Canvas and Drawables

Giuseppe Bertone
  • 2,062
  • 15
  • 15