我正在开发像Facebook的Chat Head这样的应用程序
到目前为止,我实现了拖拽和轻弹球,以反转球的位置即。屏幕上的球离开50%,向右轻弹然后向右边移动,从右边轻弹然后移动到左边缘
我目前的代码如下所示:
Point initialPos = new Point();
PointF initialTouch = new PointF();
Point delta = new Point();
@Override
public boolean onTouch(View v, MotionEvent event) {
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
initialPos.set(mWindowParams.x, mWindowParams.y);
initialTouch.set(event.getRawX(), event.getRawY());
return true;
case MotionEvent.ACTION_UP:
int releaseX = (int)event.getRawX();
int releaseY = (int)event.getRawY();
travel[0] = mWindowParams.x;
travel[1] = mWindowParams.y;
if (Math.abs(delta.x) < (Dimension.screenX * 0.15) && Math.abs(delta.y) > 100) {
travel[2] = 0;
travel[3] = Math.min(Math.max(0, releaseY), bottomEdge);
} else {
/*double rad = Math.atan2(initialTouch.y - releaseY, initialTouch.x - releaseY);
double degree = (rad * 180 / Math.PI + 180) % 360;*/
double slope = (releaseY - initialTouch.y) / (releaseX - initialTouch.x);
double straightLineEquation = initialPos.y - initialPos.x * slope;
if (initialPos.x < Dimension.screenX / 2) {
// flick right
int destY = (int) (Dimension.screenX * slope + straightLineEquation);
travel[2] = Dimension.screenX - mBall.getWidth();
travel[3] = Math.min(Math.max(0, destY), bottomEdge);
} else {
// flick left
travel[2] = 0;
travel[3] = Math.min(Math.max(0, (int) straightLineEquation), bottomEdge);
}
}
moveBallToPos(travel);
return true;
case MotionEvent.ACTION_MOVE:
delta.set((int) (event.getRawX() - initialTouch.x), (int) (event.getRawY() - initialTouch.y));
mWindowParams.x = initialPos.x + delta.x;
mWindowParams.y = initialPos.y + delta.y;
mWindowManager.updateViewLayout(mBall, mWindowParams);
return true;
}
return false;
}
我使用斜率方程和直线方程,但不处理所有情况..
请告诉我如何做到这一点,我的数学很糟糕,非常感谢!