How to scale a bitmap quickly

I'm 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 ,that is, it would not be feasible to have the proper dimensions already because it would lose much quality. The problem I'm 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 drastically to de 10 to 20 FPS. My question is how to make that when scaling the bitmap the FPS did not fall so much?

OBS 1: I'm using the native method Bitmap.createScaledBitmap.

Note 2: if necessary ,there is no request for further information or code snippets.

Author: Luiz Vieira, 2018-06-21

1 answers

The process of scaling an image is quite costly, especially when you want to scale the image. To reduce, scaling algorithms can simply apply an average (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 you can't escape an interpolation (something like: the pixels of the scaled image are calculated from the rate of change of the region in the original image, considering the dimension). Therefore, does not scale in images within the main loop of a game, as this even knocks down the FPS.

What is most common is that you have previously produced versions at different scales of the image, and use the appropriate version depending on the location/device / etc. If you have a lot of images or you can't produce this beforehand, use images at a higher scale and scale down outside the mainloop of the game (before you start, for example). The use of higher-scale images also help maintain a better resolution(the resolution of the interpolation is not the best).

 3
Author: Luiz Vieira, 2018-07-26 01:01:14