Reputation: 11
I've been running into a weird problem trying to use SurfaceView. Like this one, It works, but it keeps flickering between 2 frames*. Also, unlockCanvasAndPost Takes around 30-50Ms, Is this normal?
Redrawing everything each time is NOT a good idea in this case, I don't think redrawing 350 dots every frame is a good idea.
*Flickering between two frames: Lets say i draw a dot every frame at (X,X), X being the frame number. I will see dots 1,1 3,3 5,5 7,7 9,9 showing up every impair frame and dots 2,2 4,4 6,6 8,8 showing up every pair frame.
Here is the code that draws:
Canvas C;
while(running)
{
synchronized (mSurfaceHolder)
{
DoCalc(); //Doesn't do anything special. changes vars X1 and Y1
C=mSurfaceHolder.lockCanvas();
aDraw(C); // Only contains canvas.drawRect(X1-1, Y1-1, X1+1,Y1+1, P);
mSurfaceHolder.unlockCanvasAndPost(C); //Why does it take so long?
}
try {Thread.sleep(500);} //Temporary delay.
catch (InterruptedException e) {}
}
Upvotes: 1
Views: 1692
Reputation: 21639
SurfaceView has two buffers, which are swapped every frame which means your code works exactly as it should.
if you don't want to redraw all dots you need to draw into a bimap using its canvas first, or to draw two dots per frame prevoius and current into surfaceview.
Upvotes: 1