1

I coded a threaded surfaceview. It works but does not seem to refresh well. What I mean by not refresh well is EX: If I move a bitmap position it continues to be drawn at is last location + is a new one.

Here is my code:

public GameView(Context context) 
{               
    super(context);
    holder=getHolder(); 
    currentScreen=new TestScreen();
    currentScreen.Load(this.getResources());
}

protected void Resume()
{
    isRunning=true;
    renderThread=new Thread(this);
    renderThread.start();   
}


public void run() 
{
    while(isRunning)
    {
        gametime=lasttime-System.nanoTime();
        lasttime=System.nanoTime();
        if(!holder.getSurface().isValid())
                continue;

        if(!(currentScreen==null))
        {
            currentScreen.Update(gametime);
            Canvas cv=holder.lockCanvas();
            currentScreen.Draw(cv, gametime);        
            holder.unlockCanvasAndPost(cv);                         
        }       
    }
}

public void pause()
{
    isRunning=false;
    while(true)
    {
        try
        {
            renderThread.join();
        }
        catch (InterruptedException e)
        {

        }
        break;
    }
    renderThread=null;
}

The Screen class code:

public void Load(Resources resources) {
    square=BitmapFactory.decodeResource(resources, R.drawable.square);
    x=y=0;
    super.Load(resources);
}

@Override
public void Update(long gametime) {
    x++;
    super.Update(gametime);
}

@Override
public void Draw(Canvas cv, long gametime) {
    cv.drawBitmap(square, x, y, null);
    super.Draw(cv, gametime);
}

I tried without the Screen class method but it does the same thing. Am I missing some lines to clear the screen?

iknow
  • 8,358
  • 12
  • 41
  • 68

1 Answers1

1

Since you aren't providing a dirty rectangle to lockCanvas(), the system will offer no guarantee of what will happen to the Canvas between unlockCanvasAndPost(Canvas) and the next time lockCanvas() is called.

For this reason, you must redraw the entire content of the Canvas, not just the part that has changed. It looks like you are only drawing the square that is moving, without drawing a background to fill the rest of the Canvas.

Note: If you used lockCanvas(Rect dirty), the situation would be different.

Rob K
  • 294
  • 2
  • 10