我试图创建一个允许用户按时设置模拟时钟的课程。他们必须移动分针,而不是时针,顺时针或逆时针,以设置当前时间。时针根据分针进度自动移动,但我无法正确移动时针。每次经过十二点和六点时都没有平稳的运动,那里有角度临界点。
直到这一刻,这是我的锻炼。在十二点钟,角度等于0度,当然最小角度,在六点钟,角度是180度,最大角度。因此,从12到6(顺时针),我们有正角度(0,180),从6到12(顺时针),我们有负角度(-180,0)。没关系,但是如果我想根据分针进度来计算小时针的正确位置,我必须将这些角度转换为0-360度范围。
这是我处理手势的地方:
@Override
public boolean onTouch(View v, MotionEvent event) {
// Clock is the clock sphere and the minutes hand.
final float xc = clock.getTranslationX() + (clock.getWidth() / 2);
final float yc = clock.getTranslationY() + (clock.getHeight() / 2);
final float x = event.getX();
final float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
clock.clearAnimation();
mMinutesCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));
break;
case MotionEvent.ACTION_MOVE:
/**
* Translate angles from [-179,179] to [0,360] to be able to move
* hours hand properly.
*/
// Start Angle
mMinutesCurrAngle = set360Angle(mMinutesCurrAngle);
mMinutesPrevAngle = mMinutesCurrAngle;
// Finish angle
mMinutesCurrAngle = Math.toDegrees(Math.atan2(event.getX() - xc, yc - event.getY()));
mMinutesCurrAngle = set360Angle(mMinutesCurrAngle);
if ((mMinutesCurrAngle > mMinutesPrevAngle)) {
// Clockwise between 12 and 6
mHoursCurrAngle = mLastSpinHoursAngle + (mMinutesCurrAngle / 12);
} else if ((mMinutesCurrAngle < mMinutesPrevAngle)) {
// counter-Clockwise between 6 and 12
mHoursCurrAngle = mLastSpinHoursAngle + (mMinutesCurrAngle / 12);
} else if ((mMinutesCurrAngle > mMinutesPrevAngle) && (mMinutesCurrAngle < 0)) {
// Clockwise between 6 and 12
mHoursCurrAngle = mLastSpinHoursAngle + (- mMinutesCurrAngle / 12);
} else if ((mMinutesCurrAngle < mMinutesPrevAngle) && (mMinutesCurrAngle < 0)) {
// counter-Clockwise between 6 and 12
mHoursCurrAngle = mLastSpinHoursAngle + (mMinutesCurrAngle / 12);
}
newSpin();
// Transelate angles to the original format to represent them properly.
mMinutesPrevAngle = translate360Angle(mMinutesPrevAngle);
mMinutesCurrAngle = translate360Angle(mMinutesCurrAngle);
animate(clock, mMinutesPrevAngle, mMinutesCurrAngle, 0);
animate(hour, mHoursPrevAngle, mHoursCurrAngle, 0);
mHoursPrevAngle = mHoursCurrAngle;
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
这是我翻译角度的地方:
/**
* Translate angles from [-179,179] to [0,360] to be able to move
* hours hand properly.
* @param minutesAngle
* @return
*/
private double set360Angle(double angle) {
if (angle < 0) return (360 + angle);
else return angle;
}
/**
* Transelate angles to the original format to represent them properly.
* @param angle
* @return
*/
private double translate360Angle(double angle) {
if (angle > 180) return (-360 + angle);
else return angle;
}
这就是我知道如果分针开始新的旋转:
private void newSpin() {
if (translate360Angle(mMinutesPrevAngle) < 0 && translate360Angle(mMinutesCurrAngle) > 0) {
// New Spin clockwise
// I must remember hour hand angle
mLastSpinHoursAngle = mHoursPrevAngle;
} else if (translate360Angle(mMinutesPrevAngle) > 0 && translate360Angle(mMinutesCurrAngle) < 0) {
// New Spin counter-clockwise
// I must remember hour hand angle
mLastSpinHoursAngle = mHoursPrevAngle;
}
}
任何人都能帮助我吗?如果有人能帮助我,我保证把你的名字写给我的第一个未出生的女儿......开个玩笑。
答案 0 :(得分:0)
我发现问题出在哪里......
问题是newSpin()方法中的“if conditions”。在检查条件之前,我将角度转换为原始格式(从0度到180度,12点到6点,顺时针,从-180度到0度,6点到12点) '时钟,顺时针也是)。因此,请检查用户是否用分针开始新的旋转,每当用户经过6点而不是12点时,它会添加/减去新的旋转。
所以,我确实修复了它修改这些条件,并检查360度格式的分针角度,前一个和当前角度。现在,如果前一个角度大于355度且当前角度小于5度,我向mSpinNumber添加一个新的旋转。同样,如果前一个角度小于5度且当前角度大于355度,我将一个旋转减去mSpinNumber。我也将方法的名称从newSpin()改为calculateHourHandAngle()。
private void calculateHourHandAngle() {
if ((mMinutesPrevAngle > 355) && (mMinutesCurrAngle < 5)) {
// New Spin clockwise
mSpinNumber++;
} else if ((mMinutesPrevAngle < 5) && (mMinutesCurrAngle > 355)) {
// New Spin counter-clockwise
mSpinNumber--;
}
mHoursCurrAngle = (mSpinNumber * (360/12)) + (mMinutesCurrAngle / 12);
}
我还在onTouch()方法中删除了不必要的代码:
@Override
public boolean onTouch(View v, MotionEvent event) {
// Clock is the clock sphere and the minutes hand.
final float xc = clock.getTranslationX() + (clock.getWidth() / 2);
final float yc = clock.getTranslationY() + (clock.getHeight() / 2);
final float x = event.getX();
final float y = event.getY();
switch (event.getAction()) {
case MotionEvent.ACTION_DOWN:
clock.clearAnimation();
hour.clearAnimation();
mMinutesCurrAngle = Math.toDegrees(Math.atan2(x - xc, yc - y));
break;
case MotionEvent.ACTION_MOVE:
/**
* Translate angles from [-179,179] to [0,360] to be able to move
* hours hand properly.
*/
// Start Angle
mMinutesCurrAngle = set360Angle(mMinutesCurrAngle);
mMinutesPrevAngle = mMinutesCurrAngle;
// Finish angle
mMinutesCurrAngle = Math.toDegrees(Math.atan2(event.getX() - xc, yc - event.getY()));
mMinutesCurrAngle = set360Angle(mMinutesCurrAngle);
calculateHourHandAngle();
animate(clock, translate360Angle(mMinutesPrevAngle), translate360Angle(mMinutesCurrAngle), 0);
animate(hour, mHoursPrevAngle, mHoursCurrAngle, 0);
mHoursPrevAngle = mHoursCurrAngle;
break;
case MotionEvent.ACTION_UP:
break;
}
return true;
}
现在我可以知道用户设置了几个小时,因为我知道手钟的初始位置,旋转次数和分针角度。