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 ?

有帮助吗?

解决方案

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

许可以下: CC-BY-SA归因
不隶属于 StackOverflow
scroll top