1

I have a bitmap with the dimensions of 537 * 233. This is displayed in my fragment.

I am calculating the height of this bitmap through code as well.

I came to know that simply using,

image.getHeight() will always return 0.

Then I found that putting the same in overridden method onGlobalLayout() will give the actual height.

I did that. See my SO post for link.

Now the height I am getting is 155.

I also tried getting the height with,

BitmapDrawable bd = (BitmapDrawable) getResources().getDrawable(R.drawable.chart);

imgHeight = bd.getBitmap().getHeight();

Again the height I am getting is 155.

According to the above dimensions, the height should be 233.

I am seeing the same height in emulator too.

Why is the difference and/or what I consider to be its actual height ?

UPDATE:

ok, my chart was in drawable-hdpi and the density of my device is 160. So when I put the chart image in drawable folder, I got the correct height. But then if the chart height is fixed (233), why in some devices I am getting the chart height big enough to overlap my bottom timeline. Although I know a bit that this may be because of approximate values and not accurate values (density, resolution) that is causing the in-differences. But then, Any ideas how to fix that ?

Community
  • 1
  • 1
sjain
  • 23,126
  • 28
  • 107
  • 185
  • In which drawable folder you have the 'chart' image (hdpi, mdpi..) and what is the density of you device ? – JafarKhQ Sep 03 '13 at 09:56
  • ok, my chart was in `drawable-hdpi` and the density of my device is `160`. So when I put the chart image in `drawable` folder, I got the correct height. But then if the chart height is fixed, why in some devices I am getting the chart height big enough to overlap my bottom timeline. Any ideas ? – sjain Sep 03 '13 at 10:03

1 Answers1

1

First you know mdpi = 1, hdpi = 1.5 and xhdpi = 2.
Lets say you have an image in mdpi folder with width = 100px.
On mdpi device the image width will be 100x1 = 100px,
On hdpi device 100x1.5 = 150px,
On xhdpi device 100x2 = 200px.
if you dont have the image in hdpi or xhdpi folders the android system will scale them.

So when you have an Image in hdpi folder and you run the app on medium density device (mdpi), the android will scale down the image by 1.5. 233 / 1.5 = 155.
The same will happend if run the app to hdpi device you will get an image with ~310 width.

So, to avoid the scaling i suggest to put the image in drawable-nodpi folder (the images in this folder will not scaled by android system).

PS: if you put the image in drawable folder and run in mdpi device the image will not scaled because drawable folder = drawable-mdpi

JafarKhQ
  • 8,676
  • 3
  • 35
  • 45
  • Nice information. So is this the reason that in some devices I am getting overlapping of timeline with the chart image ? My two devices have same density, width and height. But one is showing overlap i.e. height of chart is overlapping the bottom timeline. – sjain Sep 03 '13 at 10:24
  • yes, to make sure put your image in mdpi folder and run the app on mdpi device, if the image getting overlapping you need a smaller image, if its good, run the app on hdpi and xhdpi device it should fit exactly (but pixelated). so you need 1.5x and 2x images for hdpi and xhdpi devices, create them using photo editor (photoshop, gimp) – JafarKhQ Sep 03 '13 at 10:32
  • Both of my devices have density 160 which falls in mdpi device category. In one, the chart is overlapping and on other device it is showing no overlap. So why these differences if both are mdpi devices and how to resolve ? Why isn't it fitted exactly ? – sjain Sep 03 '13 at 10:36
  • what is the screen resolution of those devices? – JafarKhQ Sep 03 '13 at 10:40
  • The devices and information is in this link - http://stackoverflow.com/questions/18531532/bitmap-size-difference-for-devices-with-same-density-inches-and-resolution. The problem is, I need a mechanism to distinguish between the two. Any ideas welcome. – sjain Sep 03 '13 at 10:45
  • The Asus Memo Pad 172V is mdpi (170), but MID 7510 is ldpi (130). your problem with MID 7510 i dont have a workaround for this (from code also width and height are fake). for me i will not support these kind of devices. – JafarKhQ Sep 03 '13 at 10:57