0

I'm making an Android game.

I loaded a 128x240 PNG image with my sprites into a Bitmap object and tried to retrieve a 16x16 tile from it via createBitmap(bitmap, x, y, w, h), but got a 1/4 smaller section of the picture instead.

The width/height of the picture according to getWidth/getHeight is 512x960.

What am I doing wrong?

Jorge Bucaran
  • 5,588
  • 2
  • 30
  • 48

1 Answers1

0

Probably you have the picture in your drawable folder and testing it on a device with high resolution? Android automatically scales up pictures according to http://developer.android.com/guide/practices/screens_support.html

I think the quick solution is to put the image to a drawable-xhdpi (or according other drawable folder).

EDIT: based on a comment at I don't want Android to resize my bitmap Automatically, you can add a folder "drawable-nodpi" to prevent the resizing

Community
  • 1
  • 1
jpm
  • 3,300
  • 1
  • 19
  • 29
  • See my "EDIT". If you put the image in a folder "drawable-nodpi", it should not be resized at all and you can assume the original size independent of the device/resolution – jpm Oct 21 '13 at 16:39
  • I see. I think the resizing is useful in my situation so what I would like is a way to get that magic number programmatically so that I don't have to worry about what drawable folder I'm using. – Jorge Bucaran Oct 21 '13 at 16:52
  • ah I see. I'm not sure how to do that, but take a look at http://developer.android.com/reference/android/graphics/Bitmap.html#getScaledHeight(int) Either you can find out the factor with the help of that method or it might lead you to an easy way to get it from the system (I'm pretty sure that should be possible...) – jpm Oct 21 '13 at 16:56