java旋转角度_java – 如何确定旋转图像的最终位置或角度

我有一个在Fling上旋转的Wheel的

ImageView.

旋转完成后,如何检测车轮的最终位置?

基本上,类似于命运轮,结果取决于车轮停在哪里

有没有办法检测吊装/旋转何时完成,然后获得最终角度?

我想将这个角度与圆圈中的4个象限中的一个相关联,并从中设置结果.谢谢,我的一些代码如下

//手势检测/

private class MyWheelOnTouchListener implements OnTouchListener {

private double startAngle;

@Override

public boolean onTouch(View v, MotionEvent event) {

switch (event.getAction()) {

case MotionEvent.ACTION_DOWN:

rotateAnim(); // test

// 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;

}

// set the touched quadrant to true

quadrantTouched[getQuadrant(event.getX() - (wheelWidth / 2), wheelHeight - event.getY() - (wheelHeight / 2))] = true;

wheeldetector.onTouchEvent(event);

return true;

}

}

/**

* Simple implementation of a {@link SimpleOnGestureListener} for detecting a fling event.

*/

private class MyWheelGestureDetector extends SimpleOnGestureListener {

private double endAngle;

@Override

public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {

// get the quadrant of the start and the end of the fling

int q1 = getQuadrant(e1.getX() - (wheelWidth / 2), wheelHeight - e1.getY() - (wheelHeight / 2));

int q2 = getQuadrant(e2.getX() - (wheelWidth / 2), wheelHeight - e2.getY() - (wheelHeight / 2));

// the inversed rotations

if ((q1 == 2 && q2 == 2 && Math.abs(velocityX) < Math.abs(velocityY))

|| (q1 == 3 && q2 == 3)

|| (q1 == 1 && q2 == 3)

|| (q1 == 4 && q2 == 4 && Math.abs(velocityX) > Math.abs(velocityY))

|| ((q1 == 2 && q2 == 3) || (q1 == 3 && q2 == 2))

|| ((q1 == 3 && q2 == 4) || (q1 == 4 && q2 == 3))

|| (q1 == 2 && q2 == 4 && quadrantTouched[3])

|| (q1 == 4 && q2 == 2 && quadrantTouched[3])) {

wheel.post(new FlingWheelRunnable(-1 * (velocityX + velocityY)));

} else {

// the normal rotation

wheel.post(new FlingWheelRunnable(velocityX + velocityY));

}

endAngle = getAngle(e1.getX(), e2.getY());

return true;

}

}

/**

* A {@link Runnable} for animating the the dialer's fling.

*/

private class FlingWheelRunnable implements Runnable {

private float velocity;

public FlingWheelRunnable(float velocity) {

this.velocity = velocity;

}

@Override

public void run() {

if (Math.abs(velocity) > 5) { // original = 5

rotateDialer(velocity / 100); // original = 75

velocity /= 1.0666F; // original = 1.0666F

wheel.getRotation());

// post this instance again

wheel.post(this);

}

}

}

/**

* @return The angle of the unit circle with the image view's center

*/

private double getAngle(double xTouch, double yTouch) {

double x = xTouch - (wheelWidth / 2d);

double y = wheelHeight - yTouch - (wheelHeight / 2d);

switch (getQuadrant(x, y)) {

case 1:

return Math.atan(y / x) * 180 / Math.PI;

case 2:

return 180 - Math.atan(y / x) * 180 / Math.PI;

case 3:

return 180 + (-1 * Math.atan(y / (x)) * 180 / Math.PI);

case 4:

return 360 + Math.atan(y / (x)) * 180 / Math.PI;

default:

return 0;

}

}

/**

* @return The selected quadrant.

*/

private static int getQuadrant(double x, double y) {

if (x >= 0) {

return y >= 0 ? 1 : 4;

} else {

return y >= 0 ? 2 : 3;

}

}

/**

* Rotate the wheel.

*

* @param degrees The degrees, the dialer should get rotated.

*/

private void rotateDialer(float degrees) {

matrix.postRotate(degrees, wheelWidth / 2, wheelHeight / 2);

wheel.setImageMatrix(matrix);

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值