I am writing a custom class. There is a bitmap that needs to be calculated for some pre-processing and sizing before it actually gets drawn. The bitmap itself is a pre-processed 9-patch image. In the constructor, there is this code:
BitmapFactory.Options bmpOptions = new BitmapFactory.Options();
bmpOptions.inJustDecodeBounds = true;
BitmapFactory.decodeResource(getResources(), BITMAP_ID, bmpOptions);
Bitmap bmp = BitmapFactory.decodeResource(getResources(), BITMAP_ID);
Log.d(getClass().getSimpleName(), "width: " + bmp.getWidth() + " " + bmpOptions.outWidth + "; height: " + bmp.getHeight() + " " + bmpOptions.outHeight);
Output on the 7" Samsung Galaxy Tab 7 running Android 3.2.2:
width: 556 556; height: 890 890
Output on the 10" Motorola Xoom running Android 4.1:
width: 556 556; height: 890 890
Output on the 7" Nexus 7 running Android 4.2.2:
width: 740 834; height; 1185 1335
The actual dimensions of the bitmap are:
mdpi: 558 x 892
hdpi: 836 x 1337
The bitmap is a pre-processed 9-patch which is why the sizes are off by 2 pixels. I can not understand why the hdpi assets on the Nexus 7 would make this much of a difference.
I have tried these configurations as well:
bmpOptions.inScaled = false;
and
bmpOptions.inTargetDensity = getResources().getDisplayMetrics().densityDpi;
and
bmpOptions.inTargetDensity = getResources().getDisplayMetrics().densityDpi;
bmpOptions.inDensity = getResources().getDisplayMetrics().densityDpi;
and
bmpOptions.inTargetDensity = 0;
bmpOptions.inDensity = 0;
I have also tried the opposite approach and create a second BitmapFactory.Options for the decoded bitmap and tell it to not scale at all.
All of these provide the same exact results.
The first 3 lines don't affect the 4th line since the 4th line don't use any of the variables above it. also the "bmpOptions" doesn't get used or filled at all. maybe you've renamed the variables while posting this question?
also, is it possible that the bitmap image file is located on folder that has a density qualifier (or the default one which is like mdpi) ?
only if you put the file into the drawable-nodpi, you will always get the same number of pixels for width&height.