如何在停止移动(android)后计算图像的度数?

时间:2013-03-20 13:19:08

标签: android motionevent

我对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;

            }

需要能够使用xValueyValue

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

xValueyValue插入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()方法中的其他地方调用run()。问题是xValueyValue仅在运动事件期间计算。在运动事件结束后,图像继续旋转。因此,当图像速度达到0时,我需要从MotionEvent获取最新的值。任何想法都将受到赞赏。

0 个答案:

没有答案