【问题标题】:onTouch DirectiononTouch 方向
【发布时间】:2011-06-18 21:00:28
【问题描述】:

我有一个GirdView,我有一个onTouchListener,我想知道有人在屏幕上滑动手指时的方向,我可以轻松左右,但我也想上下.

但我的问题是这个..

float currentX;
        float currentY;

        @Override
        public boolean onTouch(View v, MotionEvent event) {
                float x = event.getX();
                float y = event.getY();

                switch(event.getAction()) {
                case MotionEvent.ACTION_DOWN:
                    Log.d("Showpatch", "Down Triggered " + x + ", " + y);
                    currentX = x;
                    currentY = y;
                    break;

                case MotionEvent.ACTION_UP:
                    Log.d("Showpatch", "Up Triggered" + x + ", " + y);

                    Log.d("Showpatch", "Testing Left... " + x + ", " + y);
                    if (currentX > x) {
                        Log.d("Showpatch", "Moved Left");
                        return false;
                    }

                    Log.d("Showpatch", "Testing Right... " + x + ", " + y);
                    if (currentX < x) {
                        Log.d("Showpatch", "Moved Right");
                        return false;
                    }

                    Log.d("Showpatch", "Testing Down... " + x + ", " + y);
                    if (currentY < y) {
                        Log.d("Showpatch", "Moved Down");
                        return false;
                    }

                    Log.d("Showpatch", "Testing Up... " + x + ", " + y);
                    if (currentY < y) {
                        Log.d("Showpatch", "Moved Up");
                        return false;
                    }

                    break;
                }


            return false;
        }

正如我所说,它可以毫无问题地向左和向右移动,但它永远不会检测到向上或向下,因为它会在到达向上/向下语句之前触发向左/向右,除非你准确地向上和向下移动手指它不会不行……

有没有办法获得用户向上或向下滑动的位置?

非常感谢任何帮助,谢谢。

【问题讨论】:

    标签: java android gridview touch


    【解决方案1】:

    我相信 Gesture Listener 可以帮到你,它是在 1.6 中引入的

    http://developer.android.com/resources/articles/gestures.html

    干杯 托比亚斯

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2016-11-28
      • 2015-01-09
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多