开发者

onTouch Direction

开发者 https://www.devze.com 2023-03-13 18:18 出处:网络
I have a GirdView and i have a onTouchListener, i want to get the direction of when someone swipes their finger across the screen, i can get right and left easily, but i also want to get up and down.

I have a GirdView and i have a onTouchListener, i want to get the direction of when someone swipes their finger across the screen, i can get right and left easily, but i also want to get up and down.

But my problem is this..

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

As i said it gets left and right without a problem, but it never detects up or down because it fires the left/right before it gets to the up/down statements, unless you move your finger exactly up and down it doesn't work...

Is there a way of getting wherever a user swipes up or down?

Any help is much appreciated, Thank you.


I believe the Gesture Listener can help you with that, it got introduced in 1.6

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

cheers tobias

0

精彩评论

暂无评论...
验证码 换一张
取 消