public boolean onTouchEvent(MotionEvent event) {
float npx = event.getX();
float npy = event.getY();
switch(event.getAction())
{
case MotionEvent.ACTION_DOWN:
Rect r = new Rect((int) (x-1), (int) (y-1)
, (int) (x+strWidth+2), (int) (y+ 20));
if(r.contains((int)npx, (int)npy))
{
//人为控制物体
isControlByFigner = true;
offFignerX = npx - x + 1;
offFignerY = npy - y + 1;
}
break;
case MotionEvent.ACTION_MOVE:
if(isControlByFigner)
{
//跟随手指
x = npx - offFignerX +1;
y = npx - offFignerY +1;
//计算时差
timeOffset = System.currentTimeMillis() - timeRecord;
timeRecord = System.currentTimeMillis();
}
break;
case MotionEvent.ACTION_UP:
if(isControlByFigner)
{
//计算释放时的速度
Vx = (npx - opx)/timeOffset;
Vx = (npy - opy)/timeOffset;
//释放手指
isControlByFigner = false;
}
break;
}
opx = npy;
opy = npy;
this.invalidate();
return super.onTouchEvent(event);
}