tags:

views:

257

answers:

2

I have tested my game on HTC Desire with Android 2.2. Game is 2D with custom defined sprites with multiple bitmap images (frames). Frames are generated from one larger image using method Bitmap.createBitmap():

bitmapFrames[currentFrame][0] = Bitmap.createBitmap(image, startX, startY, width, height, matrix, true);

It works ok on Android 1.5 and 1.6 devices. Also it works ok on all emulators (1.5, 1.6, 2.1 and 2.2) but on real HTC Desire device all sprite frames are drawn. It looks like above mentioned method ignore parameters startX, startY, width, height when creating bitmap frame.
Any clue about this issue?

A: 

@Damir - did you find a solution? I have a similar problem where I use Bitmap.createBitmap(...) successfully in the emulator, but the actual 2.2 device shows the wrong part of the bitmap image. I can only test on the emulator and one froyo device, so I don't know about 1.5 and 1.6 devices.

Specifically, the 'width' and 'height' variables seem to work, and the 'matrix' seems to work, but the wrong graphic ends up in the resulting bitmap, so I think the 'startX' and 'startY' variables are read incorrectly (by the function Bitmap.createBitmap). But what is a workaround?

In my application, also a game, a set of tiles is saved in a single png image. each of the tiles is 8 x 8, and the whole collection stores images that go together to make pillars and platforms and ladders. The program takes a number in a map and replaces this number with a tile from the tile sheet. This new arrangement of tiles represents the background in the game. (this is all similar to game systems like the nintendo DS). The android code seems to be taking 8 x 8 squares, so I suspect that the 'height' and 'width' are working, but the backgrounds show incorrect info inside the squares. Has anybody seen anything like this? Is there something about 2.2 that leads to this kind of behavior?

radiodee1
I found a better solution. create drawable folders for the various screen densities. If you have a folder called 'drawable-hdpi' and the android device has a high screen density, the operating system will look in this folder for the drawable resource. If it finds a resource for your screen density, it won't try to resize a resource from another folder. In other words, keep several 'drawable' folders with identical data (if you don't want the android system to resize your bitmaps automatically). These are my folders:`res/drawable``res/drawable-hdpi``res/drawable-mdpi``res/drawable-ldpi`
radiodee1
+1  A: 

OK, I found solution for this problem. Let me explain where issue arise: I noticed that my tiles do not have same problem as sprites even they are created on same way. This is because for tile images I didn't use transparency. Conclusion is:
If you are using solid Bitmap image
bmpImage.getConfig() == Config.RGB_565
then above mentioned method works fine. But, if you are using transparency
bmpImage.getConfig() == Config.ARGB_8888
then method
Bitmap.createBitmap(image, startX, startY, width, height, matrix, true);
always return whole bitmap image instead just one part. This issue only exist on Android 2.2 real device.

Solution:
I had manualy to copy Color values from source image and create frames using parts of int[] color values using below methods:

  • Bitmap.getPixels()

  • System.arraycopy()

  • Bitmap.createBitmap(int[] colors, int width, int height, Bitmap.Config config)

Damir