从onTouch MotionEvent存储变量 - Android

时间:2016-11-21 11:16:42

标签: java android motionevent

我有一个imageView&我想知道触摸在哪个方向移动。在DOWN,它将X和Y坐标存储在变量initX和initY中。在移动时,它将当前坐标存储在finalX和finalY。

我在textViews中显示了这些变量,并且它们显示了我期待的值。

在UP上,我调用另一个方法,即WayToMove,并将其传递给initX,initY,finalX和finalY。但是,传递的值始终为零。他们不应该。知道为什么吗?

public void moveTile(){


    ImageView view1 = (ImageView) findViewById(R.id.view1);
    ImageView view2 = (ImageView) findViewById(R.id.view2);

    final TextView tv = (TextView) findViewById(R.id.textView);
    final TextView tv2 = (TextView) findViewById(R.id.textView2);
    final TextView tv3 = (TextView) findViewById(R.id.textView3);



    view1.setOnTouchListener(new View.OnTouchListener() {
        @Override
        public boolean onTouch(View v, MotionEvent event) {  // half-inched from https://developer.android.com/training/gestures/detector.html

            int initialY =0;
            int finalY = 0;
            int initialX = 0;
            int finalX = 0;

            int action = MotionEventCompat.getActionMasked(event);

            switch(action) {
                case (MotionEvent.ACTION_DOWN):

                    initialX = (int) event.getX();
                    initialY = (int) event.getY();
                    tv.setText(initialX + ", " + initialY);

                    tv3.setText( initialX + ", " + initialY );
                    return true;
                case (MotionEvent.ACTION_MOVE):
                    tv.setText("Action was MOVE");
                    finalX = (int) event.getX();
                    finalY = (int) event.getY();

                    tv2.setText(finalX + ", " + finalY);


                    return true;
                case (MotionEvent.ACTION_UP):

                    whichWayToMove(initialX, finalX, initialY, finalY);


                    tv.setText("Action was UP");
                    return true;
                case (MotionEvent.ACTION_CANCEL):
                    tv.setText("Action was CANCEL");
                    return true;
                case (MotionEvent.ACTION_OUTSIDE):
                    tv.setText("Movement occurred outside bounds " +
                            "of current screen element");
                    return true;
                default:
                    tv.setText("wut?");

            }


                    return false;
        }
    });




}



public void whichWayToMove(int initX, int finalX, int initY, int finalY){

    TextView tv4 = (TextView) findViewById(R.id.textView4);
    TextView tv5 = (TextView) findViewById(R.id.textView5);

    tv5.setText("initX: " + initX + ". finalX: " + finalX + " initY: " + initY + " . finalY: " +
            "" + finalY +
            "" );

    String direction = "";

    int deltaX = initX - finalX;
    int deltaY = initY - finalY;



    if (Math.abs(deltaX)> Math.abs(deltaY)){ // then we're moving on the X axis

        if (deltaX >= 0){direction = "left";}
        else {direction = "right";}

    }

        else { // we're moving on the Y axis

        if (deltaY >= 0){direction = "up";}
        else {direction = "down";}

    }

   // tv4.setText("direction is: " + direction);
    tv4.setText("deltaX is: " + deltaX + ". deltaY is: " + deltaY);

}  

1 个答案:

答案 0 :(得分:0)

这是因为每次你的行动得到改变"听众再次被召唤。

因此,finalYfinalX为零。

如果您想获取ACTION_UP发生的当前坐标,请像其他event.getX();一样调用cases

更新

如果你想存储你的变量" persistent"你必须将它存储在全局变量中。

public class MyTouchListener implements View.OnTouchListener {

  private int mOnDownX;

  private int mOnDownY;

  @Override
  public boolean onTouch(View v, MotionEvent event) {
    int action = MotionEventCompat.getActionMasked(event);

    switch(action) {
      case (MotionEvent.ACTION_DOWN):

        mOnDownX = (int) event.getX();
        mOnDownY = (int) event.getY();
        break;

     case (MotionEvent.ACTION_UP):

       int mOnUpX = (int) event.getX();
       int mOnUpY = (int) event.getY();

       Log.v("Finger down at X:", String.valueOf(mOnDownX));
       Log.v("Finger down at Y:", String.valueOf(mOnDownY));

       Log.v("Finger up at X:", String.valueOf(mOnUpX));
       Log.v("Finger up at Y:", String.valueOf(mOnUpY));
       break;
  }
}