public class MyHelloWorldView extends SurfaceView implements Callback, Runnable {
boolean isPause=false;
SurfaceHolder sfh = null;
public
MyHelloWorldView (Context context) {
super(context);
init(context);
}
public
MyHelloWorldView (Context context, AttributeSet attrs) {
super(context, attrs);
init(context);
}
void init(Context context)
{
sfh = this.getHolder();
sfh.addCallback(this);
}
@Override
public void run() {
if(!isPause)
{
try {
updateFrame();
render();
Thread.sleep(50);
} catch (InterruptedException e) {
e.printStackTrace();
}
}
}
void updateFrame() // 更新每帧的动画内容,处理数据
{}
void
render()
{
Canvas canvas = sfh.lockCanvas();
if(canvas != null)
{
canvas.save();
//canvas.drawxxx
canvas.restore();
sfh.unlockCanvasAndPost(canvas);
}
}
@Override
public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
}
@Override
public void surfaceCreated(SurfaceHolder arg0) {
Thread th = new Thread(this);
th.start();
}
@Override
public void surfaceDestroyed(SurfaceHolder arg0) {
}
}