Android 的SurfaceView的SurfaceHolder.CallBack的基本架构
public class MenuView extends SurfaceView implements SurfaceHolder.CallBack{
pubilc MenuView(){} //构造器
public MenuView(BasketballActivity activity) {
super(activity);
this.activity=activity;
this.getHolder().addCallback(this);//设置生命周期回调接口的实现者
}
public void onDraw(Canvas canvas){ }
@Override
public boolean onTouchEvent(MotionEvent e)
{
float x=e.getX();
float y=e.getY();
switch(e.getAction())
{
case MotionEvent.ACTION_DOWN:
if(x>=0&&x<=105&&y>=300&&y<=335)
{ }
else if(x>=0&&x<=105&&y>=340&&y<=375)
{ }
else if(x>=0&&x<=105&&y>=380&&y<=415)
{ }
else if(x>=0&&x<=105&&y>=420&&y<=455)
{ }
new MenuThread(this).start();
break;
case MotionEvent.ACTION_UP:
if(x>=0&&x<=105&&y>=300&&y<=335)
{
activity.hd.sendEmptyMessage(GAME_LOAD);
}
else if(x>=0&&x<=105&&y>=340&&y<=375)
{
activity.hd.sendEmptyMessage(GAME_HELP);
}
else if(x>=0&&x<=105&&y>=380&&y<=415)
{
activity.hd.sendEmptyMessage(GAME_ABOUT);
}
else if(x>=0&&x<=105&&y>=420&&y<=455)
{
System.exit(0);
}
}
return true;
}
@Override
public void surfaceChanged(SurfaceHolder holder, int format, int width,
int height) {
}
@Override
public void surfaceCreated(SurfaceHolder holder) {
Canvas canvas=holder.lockCanvas();
try
{
synchronized(holder)
{
onDraw(canvas);//绘制
}
}catch(Exception e)
{
e.printStackTrace();
}
finally
{
if(canvas!=null)
{
holder.unlockCanvasAndPost(canvas);
}
}
}
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
}
}