'公共类 MainActivity 扩展 Activity 实现 OnTouchListener {
MySurface msurface;
float x,y,sX,sY,fX,fY;
Bitmap bt,plus;
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
//setContentView(R.layout.activity_main);
msurface=new MySurface(this);
msurface.setOnTouchListener(this);
x=0;
y=0;
sX=0;
sY=0;
fX=0;
fY=0;
bt=BitmapFactory.decodeResource(getResources(), R.drawable.hndle);
plus=BitmapFactory.decodeResource(getResources(), R.drawable.gbut);
setContentView(msurface);
}
protected void onPause()
{
// TODO Auto-generated method stub
super.onPause();
msurface.pause();
}
protected void onResume()
{
// TODO Auto-generated method stub
super.onResume();
msurface.resume();
}
public boolean onTouch(View v, MotionEvent event)
{
x=event.getX();
y=event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
sX=event.getX();
sY=event.getY();
break;
case MotionEvent.ACTION_UP:
fX=event.getX();
fY=event.getY();
break;
}
return true;
}
公共类 MySurface 扩展 SurfaceView 实现 Runnable {
SurfaceHolder 我们的Holder;
Thread ourTh=null;
boolean isRunning=false;
public MySurface(Context context)
{
super(context);
ourHolder=getHolder();
ourTh=new Thread(this);
ourTh.start();
}
public void pause() //bringin android games
{
isRunning=false;
while(true)
{
try
{
ourTh.join();
}
catch (InterruptedException e)
{
// TODO Auto-generated catch block
e.printStackTrace();
}
break;
}
ourTh=null;
}
public void resume()
{
isRunning=true;
}
@Override
public void run()
{
while(isRunning)
{
if(!ourHolder.getSurface().isValid())
continue;
Canvas canvas=ourHolder.lockCanvas();
canvas.drawRGB(02, 02, 150); //backgroung color hai
if(x!=0 && y!=0)
{
canvas.drawBitmap(bt, x-(bt.getWidth()/2), y-(bt.getHeight()/2), null);
}
if(sX==256 && y==27)
{
canvas.drawBitmap(bt, x-(bt.getWidth()/2), y-(bt.getHeight()/2), null);
Intent a=new Intent(MainActivity.this,dusari.class);
}
/*if(fX!=0 && fY!=0)
{
canvas.drawBitmap(bt, x-(bt.getWidth()/2), y-(bt.getHeight()/2), null);
}*/
ourHolder.unlockCanvasAndPost(canvas);
}
}