To clarify, I'm interested in doing a simple animation that changes from one bitmap to another after a few seconds, and then repeats. For example, if I drew a frowny face, how could I set it to be removed after a few seconds, and replaced it with a smiley face?
Here's some example code
public class MySurface extends SurfaceView implements Runnable {
SurfaceHolder ourHolder;
Thread ourThread = null;
boolean isRunning = true;
Bitmap frowny;
Bitmap smiley;
public MySurface(Context context) {
super(context);
init(context);
}
public MySurface(Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
public MySurface(Context context, AttributeSet attrs, int defStyle) {
super(context, attrs, defStyle);
init(context);
}
private void init(Context context) {
// do stuff that was in your original constructor...
ourHolder = getHolder();
ourThread = new Thread(this);
ourThread.start();
frowny = BitmapFactory.decodeResource(getResources(),
R.drawable.frowny);
smiley = BitmapFactory
.decodeResource(getResources(), R.drawable.smiley);
}
public void pause() {
isRunning = false;
while (true) {
try {
ourThread.join();
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourThread = null;
}
public void resume() {
isRunning = true;
}
@SuppressLint("WrongCall")
public void run() {
// TODO Auto-generated method stub
while (isRunning) {
if (!ourHolder.getSurface().isValid()) {
continue;
}
Canvas c = ourHolder.lockCanvas();
onDraw(c);
ourHolder.unlockCanvasAndPost(c);
}
}
@Override
protected void onDraw(Canvas canvas) {
// TODO Auto-generated method stub
super.onDraw(canvas);
canvas.drawBitmap(frowny, (getWidth/2), (getHeight/2), null);
//wait a period/remove frowny
canvas.drawBitmap(smiley, (getWidth/2), (getHeight/2), null);
postInvalidate();
}
}