How to scale a bitmap quickly

Asked

Viewed 38 times

1

I’m making a game using Surfaceview and I need a bitmap to be , 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 climb it , even already prescaled , the FPS drops dramatically to 10 to 20 FPS. My doubt 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.

OBS 2: If necessary ,there is no request for more information or excerpts of the code.

1 answer

3


The process of scaling an image is quite costly, especially when you want to enlarge the image. To reduce, scaling algorithms can simply apply an average (something like: the pixel of the scaled-down image is the average value of the pixels of that region in the original image), but to zoom in, there is no escaping an interpolation (something like: the pixels of the enlarged image are calculated from the variation rate of the region in the original image, considering the new dimension). That’s why, is not done scale into images within the main loop of a game, as this knocks down even the FPS.

What is more common is that you have previously produced versions on different image scales, and use the appropriate version depending on the location/device/etc. If you have too many images or you can’t produce this previously, use images at a higher scale and scale it down out of the game mainloop (before starting, for example). The use of higher-scale images also helps maintain a better resolution (interpolation resolution is not the best).

Browser other questions tagged

You are not signed in. Login or sign up in order to post.