How to quickly scale a bitmap

1

I am making a game using SurfaceView and I need a bitmap to have, for example, a size of 20x20, but this bitmap is reused in other parts of the code in different sizes, ie it would not be viable to have it already the proper dimensions because it would lose much quality. The problem I am facing is that the bitmap in its normal size the game runs between 45 and 50 FPS, already when I scale it, even already pre-scaled, the FPS drops dramatically to 10 to 20 FPS. My question is how to make when scaling the bitmap the FPS did not fall so much?

NOTE 1: I am using the native method Bitmap.createScaledBitmap .

NOTE 2: If necessary, you do not need to request more information or snippets of code.

    
asked by anonymous 21.06.2018 / 17:25

1 answer

3

The process of scaling an image is very costly, especially when you want to enlarge the image. To reduce, scaling algorithms can simply apply a mean (something like: the pixel of the scaled image is the average value of the pixels of that region in the original image), but to increase it can not escape a tween (something like: the pixels of the enlarged image are calculated from the rate of variation of the region in the original image, considering the new dimension). So does not scale images within the main loop of a game, as this even knocks the FPS down.

What is more common is you have previously produced versions on different scales of the image, and use the appropriate version depending on the location / device / etc. If you have too many images or can not produce them previously, use images on a higher scale and reduce the scale outside the game mainloop (before you start, for example). Using higher-scale images also helps to maintain a better resolution (the interpolation resolution is not the best).

    
26.07.2018 / 03:01