0

我试图在 Android 中获取视图的开始和结束坐标以创建动画。

我尝试了 OnTouch 事件,但我只能访问初始坐标。

我尝试使用 MotionEvent 但它没有用。

public boolean onTouch(View v, MotionEvent event)
        {
            float x = event.getX();
            float y = event.getY();
            //Log.d("S&R", "Y "+y);
            switch (event.getAction() & MotionEvent.ACTION_MASK)
            {
                case MotionEvent.ACTION_DOWN:
                    Log.d("S&R", "ACTION_DOWN");
                    break;
                case MotionEvent.ACTION_UP:
                    Log.d("S&R", "ACTION_UP");
                    break;
                case MotionEvent.ACTION_MOVE:
                    Log.d("S&R", "ACTION_MOVE");
                    break;
            }
            return false;
        }

我只想知道触摸是从上到下还是从下到上。

我怎么能做到?

谢谢你。

4

1 回答 1

0

我得到了它!

public boolean onTouch(View v, MotionEvent event) {


            boolean isReleased = event.getAction() == MotionEvent.ACTION_UP || event.getAction() == MotionEvent.ACTION_CANCEL;
            boolean isPressed = event.getAction() == MotionEvent.ACTION_DOWN;
            boolean isMoved = event.getAction() == MotionEvent.ACTION_MOVE;


            if(isPressed){
                Log.d("S&R","isPressed");
                lastY=event.getY();
                return true;
            }

            else if(isReleased){
                Log.d("S&R","isReleased");
                lastY=event.getY();
                return true;
            }

            else if(isMoved)
            {
               //Log.d("S&R","MOVE");
               Log.d("S&R","lastY=> "+lastY+" currentY=> "+event.getY());

               if(event.getY()<lastY)
               {
                   Log.d("S&R","up");

               }
                else if(event.getY()>lastY)
               {
                   Log.d("S&R","down");

               }
                lastY=event.getY();
                return true;
            }
            return false;
        }

谢谢大家。

于 2015-09-03T09:07:02.117 回答