I am making an app that mimic a classic game. Therefore I need to render some pixel art.
I have a 100×100 Bitmap as a buffer and I draw everything on the buffer without any difficulties.
When I try to render it on a 480×480 Canvas, here comes the problem.
Bitmap.createScaledBitmap will give me a blurred bitmap, so I use the following code for a clear cut between “blocks (scaled up pixels)”.
float scale = getWidth() / 100f;
for(int y = 0 ; y < getHeight() ; y++){
for(int x = 0 ; x < getWidth() ; x++){
buffer2.setPixel(x, y, buffer.getPixel((int)(x / scale), (int)(y / scale)));
}
}
The code works quite well and did exactly what I want. But the performance is pretty bad(takes 300+ ms). So I am writing to ask if there is a better way out. Thank you.
I would do a few things:
Break your
getHeight()andgetWidth()into local variables so that it isn’t called every iteration of the loop. (e.g.int height = getHeight(); for(int y = 0; y < height; y++);)Use an intermediate array to store the pixels, then when the loop has completed, use
setPixels()method instead ofsetPixel()to set the pixels all at once — it is significantly more efficient than setting the pixels one at a time.