Android Canvas Lagging

523 Views Asked by At

I'm creating a project for a class and I'm experiencing some lag issues. When the map is loaded either nothing will happen so you have to go back and reload it and it'll work for a while or after making some moves my phone/emulator will take a couple seconds until you can make your next move. This is how I have my classes set up:

I have my map class as the main controller. There are different maps but they all work the same. The map class sets the content view from the layouts like normal with an empty relative layout and it creates nodes, lines and has a game rules class to check different moves the player performs. The classes for the nodes, lines and game rules extend View so in the onDraw() I draw squares with defined coordinates and add them to the view in the map class. Depending on what the player does the lines and nodes will call postInvalidate() to update numbers or colors. Inside of the game rules class it constantly checks for a winner or loser and shows whose turn it is but I have invalidate() inside of the onDraw() so it's constantly being redrawn. There are 6+ nodes being drawn and 10+ lines and the game rules overlays everything. These are also displayed by adding the views to the relative layout.

I know using sleep() isn't ideal but ever since the game rules class was created and uses invalidate inside of onDraw() I've been experiencing this. I'm on the road but I can post code later. I am also stuck using canvas for this project since no other person working on this project has ever touched Android.

Thank you in advance!

TL;DR: How can I optimize using canvas better?

0

There are 0 best solutions below