Pause and resume thread drawing to SurfaceView
I am developing a chess game for Android (http://androidchess.appspot.com), using SurfaceView
for the chessboar开发者_开发问答d. I have a drawing Thread
, that draws the chessboard in a loop. The problem is that when there are no active animations (this is more that 90% of time), it makes no sense to waste CPU and battery for drawing. How should I solve this? Maybe somehow pausing and resuming the drawing Thread
?
Try something like this (you can test it by trying this code):
final Object monitor = new Object();
class Drawing implements Runnable
{
@override
void run()
{
while(true)
{
synchronized(monitor)
{
monitor.wait();
}
// draw
}
}
}
public static void main(String[] args) {
// start your thread
while(true)
{
if( needRefresh )
{
synchronized(monitor)
{
monitor.notify();
}
}
}
}
I would recommend using a Semaphore if you want to signal once and then keep drawing (i.e. do not block) until the signal is turned off.
You pretty much answered the question yourself. You should pause your drawing thread when nothing is rendered. When using the OpenGL SurfaceView you can also use an immediate mode (which draws continuously) or manual (draw by request).
精彩评论