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?