重寫Activity的onTouchEvent方法:ide
float x_temp01 = 0.0f;
float y_temp01 = 0.0f;
float x_temp02 = 0.0f;
float y_temp02 = 0.0f;
@Override
public boolean onTouchEvent(MotionEvent event)
{
//得到當前座標
float x = event.getX();
float y = event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
{
x_temp01 = x;
y_temp01 = y;
}
break;
case MotionEvent.ACTION_UP:
{
x_temp02 = x;
y_temp02 = y;
if(x_temp01!=0 && y_temp01!=0)//
{
// 比較x_temp01和x_temp02
// x_temp01>x_temp02 //向左
// x_temp01==x_temp02 //豎直方向或者沒動
// x_temp01<x_temp02 //向右
if(x_temp01>x_temp02)//向左
{
//移動了x_temp01-x_temp02
}
if(x_temp01<x_temp02)//向右
{
//移動了x_temp02-x_temp01
}
}
}
break;
case MotionEvent.ACTION_MOVE:
{
}
break;
}
return super.onTouchEvent(event);
}spa