0

I am quite new to Android programming. What I need to do is calculate the degrees of an arrow after it has stopped moving. I am using the following code:

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;

            }

Need to be able to use xValue and 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;
        }
    }

Insert xValue and yValue into 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();*/
            }
        }

And call getAngle() from else inside the run() method. The problem is that the xValue and yValue only get calculated during motion events. The image keeps on spinning after the motion event is over. So I need to get the newest values from the MotionEvent when the image velocity reaches 0. Any ideas would be appreciated.

jprofitt
  • 10,874
  • 4
  • 36
  • 46
Lunchbox
  • 1,538
  • 2
  • 16
  • 42
  • Check this http://stackoverflow.com/questions/11287737/rotating-an-image-and-displaying-angle-of-rotation – JiTHiN Mar 20 '13 at 13:22
  • So you need the values when velocity is 0 and you don't know when the velocity is 0,is this what your are asking? – Abx Mar 20 '13 at 13:26
  • rIHaN JiTHiN, I think we had the same problem, I will try it out and let you know, thank you. Abhilash, I know when the velocity is 0, I just need to access the motion events while there is no motion – Lunchbox Mar 20 '13 at 13:44
  • I just checked and I need to calculate the degrees when the arrow has stopped moving. The arrow stops at a position that is not pre defined. I need to get two variables from the onTouch method to calculate the angle. Thanx anyways – Lunchbox Mar 20 '13 at 13:57

0 Answers0