Something is wrong with drawBitmap - with source and dest rectangles

I have a game in which at one level the sheep can explode with mines. Explosion animation is controlled using png, which contains an array of 4x4 images in a 512x515 PNG file ... see below.

enter image description here

Then I animate the explosion using the following code:

exp_bitmap_number_to_draw = (int)(time_since_death / 100); if (exp_bitmap_number_to_draw < 16) { explosion_dst_rect.left = b2sx(sheep_x[i]) - sheep_radius * 5; explosion_dst_rect.right = b2sx(sheep_x[i]) + sheep_radius * 5; explosion_dst_rect.bottom = b2sy(sheep_y[i]) + sheep_radius * 5; explosion_dst_rect.top = b2sy(sheep_y[i]) - sheep_radius * 5; explosion_src_rect.left = 128 * (exp_bitmap_number_to_draw % 4); explosion_src_rect.top = 128 * (exp_bitmap_number_to_draw / 4); explosion_src_rect.right = explosion_src_rect.left + 128; explosion_src_rect.bottom = explosion_src_rect.top + 128; canvas.drawBitmap(explosion_bitmap, explosion_src_rect, explosion_dst_rect, null); } 

Where explosion_src_rect is Rect and extension_ext is RectF. b2sx () and b2sy () are functions that are converted from the absolute coordinates of the sheep in the "playing field" to the coordinates on the screen - it's just adding an offset.

The code works fine on several phones I've tried, including S and Galaxy S II communications. But now a friend tried the code on the samsung galaxy tab 8.9 and found that the explosions appeared goofey. He sent me this partial screen capture:

enter image description here

Any idea what could be causing this?

+1
source share
3 answers

If resources are not available in the correct density, the system loads the default resources and scales them up or down as necessary to match the current screen density. There are some situations in which you may not want Android to scale the resource beforehand. The easiest way to avoid pre-scaling is to put the resource in the resource directory using the nodpi configuration nodpi . For example: res/drawable-nodpi/icon.png . Therefore, you should put your bitmaps in the `res/drawable-nodpi/ . Take a look at: http://developer.android.com/guide/practices/screens_support.html

+4
source

the width of your destination rectangle, and not the size of one of your shot frames, as you said, is set to canvas width / 4. Phones are usually the same size, but a larger tablet has a much larger screen size - As the documentation says:

Draw the specified bitmap by scaling / translating automatically to fill the destination rectangle [...] This function ignores the density associated with the bitmap. This is due to the fact that the coordinate spaces of the source rectangle and the destination are in their respective densities, so the corresponding scaling factor should already be applied.

So, your bitmap, not scalable to canvas, gets a picture starting with src Rect, but extends beyond src because it was told to fill in the DST, ignoring the densities of the two locations.

As for the solution, I'm not sure for sure - it would probably be better to just have images of all sizes that might appear in your /res . Depending on how you load the bitmap, you can get the bitmap to scale, but then your fixed width translation over the bitmap should become more dynamic.

Hope this helps somehow :)

0
source

I had the same problem that I asked here, and the answer for me was to load my bitmaps like this, and not in the usual way:

GetResources (). OpenRawResource (ImageID)

0
source

Source: https://habr.com/ru/post/1335260/


All Articles