{"id":6,"date":"2011-06-07T12:34:00","date_gmt":"2011-06-07T02:34:00","guid":{"rendered":"http:\/\/www.lazydungeon.com\/blog\/android-fling-gesture-detection\/"},"modified":"2011-06-07T12:34:00","modified_gmt":"2011-06-07T02:34:00","slug":"android-fling-gesture-detection","status":"publish","type":"post","link":"https:\/\/www.lazydungeon.com\/blog\/android-fling-gesture-detection\/","title":{"rendered":"Android &#8220;Fling&#8221; Gesture Detection"},"content":{"rendered":"<p>Had some issues last night trying to get my Android game to detect the fling gesture, basically a click and drag in mouse world.<\/p>\n<p>I had the following code in my little View object.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\nprivate GestureDetector gestureDetector;\nprivate View.OnTouchListener gestureListener;\/\/ Gesture detection\n    gestureDetector = new GestureDetector(new MyGestureDetector());\n    gestureListener = new View.OnTouchListener() {\n        public boolean onTouch(View v, MotionEvent event) {\n            if (gestureDetector.onTouchEvent(event)) {\n                return true;\n            }\n            return false;\n        }\n    };\nthis.setOnTouchListener(gestureListener);\n\n  * Handle any fling action here\n  * @author dracox\n  *\n  *\/\n protected class MyGestureDetector extends SimpleOnGestureListener {\n  \/**\n   * We need to capture any user line drawn request here.\n   *\/\n  @Override\n  public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {\n   \/\/This should only be caring about flings after init\n   gameWalls.add(new FireWall(new Point((int)e1.getRawX(), (int)e1.getRawY()), new Point((int)e2.getRawX(), (int)e2.getRawY())));\n   return true;\n  }\n }\n<\/pre>\n<p>I fired up my emulator, but was not getting any fling action logged. However the onTouch events were been fired off.<\/p>\n<p>Initially I though maybe the emulator couldn&#8217;t handle fling event, but after much searching I found the root of the problem.<\/p>\n<p>Apparently, the onDown event was fired off first, which was consumed before it became a &#8220;fling&#8221;, so I had to override that too on my custom Gesture Detector class.<\/p>\n<p>I was a little puzzled about returning true for the onDown handler, as the API stated return true to consume the event and false to let it pass.<\/p>\n<pre class=\"brush: java; title: ; notranslate\" title=\"\">\n\/**\n * Need to let onDown pass through, otherwise it will block the onFling event\n *\/\n @Override\n public boolean onDown(MotionEvent event) {\n return true;\n }\n<\/pre>\n<p>Now we&#8217;re flinging!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>Had some issues last night trying to get my Android game to detect the fling gesture, basically a click and drag in mouse world. I had the following code in my little View object. private GestureDetector gestureDetector; private View.OnTouchListener gestureListener;\/\/ Gesture detection gestureDetector = new GestureDetector(new MyGestureDetector()); gestureListener = new View.OnTouchListener() { public boolean onTouch(View [&hellip;]<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[16],"tags":[20,26,21],"class_list":["post-6","post","type-post","status-publish","format-standard","hentry","category-story","tag-android-2","tag-debug","tag-java-2"],"_links":{"self":[{"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/posts\/6","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/comments?post=6"}],"version-history":[{"count":0,"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/posts\/6\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/media?parent=6"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/categories?post=6"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.lazydungeon.com\/blog\/wp-json\/wp\/v2\/tags?post=6"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}