I draw a background for android- games, consisting of cells. Now I draw every cell on the canvas in a loop. This method does not suit me.

Is it possible not to redraw the background every time the canvas is updated, but to redraw only the necessary objects? In Flash, I used layers for this, and then how to implement this?

  • My answer may not be the most correct for you, but there is such a thing as FPS - this is the number of frames per second, so he says how many times in a second the screen will be re-created, maybe you can’t think of a better situation, the only thing you do not need to create objects in such a cycle, there only draw - BORSHEVIK
  • fps - does not "redraw", but "redraws" at least. If the screen is updated in a loop and the code is simple, then fps will be large, if the code contains a bunch of complex calculations — fps drops noticeably. Those. fps is the effect, not the cause - BOPOH
  • "In your opinion is this a false statement?" - no, wrong. FPS shows how many frames were drawn, and not how many of them were drawn. You now have one ball on the screen downloads - one fps, in a second there are more than a million of them - fps sagged. I wrote about it - fps is only a consequence, it shows the result of the drawing, but does not specify it - BOPOH
  • in fact, he considered the option of redrawing only those cells that are changing, but this option is no longer necessary due to the increasing complexity of the calculations. Just imagine that there are about 50-100 moving objects on the map. It is necessary to check their coordinates in each frame and compare them with the cells. - Maxim Zheleznyakov

1 answer 1

Java started learning two or three days ago, but worked with Canvas in javascript.

You now have about the following algorithm:

цикл { нарисовать ячейку (клетку) } 

Why don't you create a class: CanvasCell , with methods
PS syntax problems may be

 public class CanvasCell { public int x; public int y; public int width; // может быть и вещественным public int height; public boolean isChange = false; // изменилась ли наша ячейка public void draw() { /* контекст Canvas можно передавать в параметрах в этой ф-ции, используя поля, рисуем клетку */ } /* конструктор */ public CanvasCell(int x, int y, int width, int height) { /* тут присваиваем значения */ } } А потом создаем наши экземпляры СanvasCell[] cells = []; // наш массив ячеек; 

Where is the optimization , you ask?
The optimization is that during the next rendering we will draw only those cells that have changed, check on the isChange field. The instance will store all the necessary information (coordinates, size, and so on) so it will not be difficult to redraw + this can be easily scaled later (add a new property).

Total : the idea is to have access to each object on the Canvas, then you can simulate layers, using the OOP is perfect here

PS Unfortunately, the implementation itself, I can not write, is not yet familiar with the whole java (