我对 Android 编程很陌生。我需要做的是计算箭头停止移动后的度数。我正在使用以下代码:
public boolean onTouch(View v, MotionEvent event) {
//double xValue, yValue;
xValue = event.getX();
yValue = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
// reset the touched quadrants
for (int i = 0; i < quadrantTouched.length; i++) {
quadrantTouched[i] = false;
}
allowRotating = false;
startAngle = getAngle(event.getX(), event.getY());
break;
case MotionEvent.ACTION_MOVE:
double currentAngle = getAngle(event.getX(), event.getY());
rotateDialer((float) (startAngle - currentAngle));
startAngle = currentAngle;
break;
case MotionEvent.ACTION_UP:
allowRotating = true;
break;
}
需要能够使用xValue
和yValue
private double getAngle(double xTouch, double yTouch) {
double x = xTouch - (dialerWidth / 2d);
double y = dialerHeight - yTouch - (dialerHeight / 2d);
switch (getQuadrant(x, y)) {
case 1:
return Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI;
case 2:
case 3:
return 180 - (Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI);
case 4:
return 360 + Math.asin(y / Math.hypot(x, y)) * 180 / Math.PI;
default:
// ignore, does not happen
return 0;
}
}
插入xValue
并yValue
进入getAngle()
public void run() {
//double angleValue;
setCheck(velocity);
if (Math.abs(velocity) > 5 && allowRotating) {
rotateDialer(velocity / 75);
velocity /= 1.0666F;
// post this instance again
dialer.post(this);
}else{
/*Thread myThread = new Thread(new UpdateThread());
myThread.start();*/
}
}
并getAngle()
从方法内部的 else调用run()
。问题是xValue
和yValue
仅在运动事件期间计算。运动事件结束后,图像继续旋转。因此,我需要从MotionEvent
图像速度达到 0 时获取最新值。任何想法都将不胜感激。