Touch Event Propagation


Last Updated:

  1. ranxerox

    ranxerox New Member This Topic's Starter

    Joined:
    Nov 19, 2009
    Messages:
    2
    Likes Received:
    0
    I found something that confuses me about propagation of touch events. I wrote a simple test application to demonstrate. The Activity object:

    Code (Text):
    1. package org.doorways.touchie;
    2.  
    3. import android.app.Activity;
    4. import android.os.Bundle;
    5. import android.util.Log;
    6. import android.view.MotionEvent;
    7.  
    8. public class Touchie extends Activity {
    9.    
    10.     private static final String TAG = Touchie.class.getSimpleName();
    11.    
    12.     private TouchView touchView;
    13.    
    14.     /** Called when the activity is first created. */
    15.     @Override
    16.     public void onCreate(Bundle savedInstanceState) {
    17.         super.onCreate(savedInstanceState);
    18.         touchView = new TouchView(this);
    19.         setContentView(touchView);
    20.     }
    21.  
    22.     public boolean onTouchEvent (MotionEvent event) {
    23.         Log.d(TAG, "onTouchEvent(" + event + ") ");
    24.         return touchView.onTouchEvent(event);
    25.     }
    26.    
    27. }
    The View displayed in the Activity:

    Code (Text):
    1. package org.doorways.touchie;
    2.  
    3. import android.content.Context;
    4. import android.util.Log;
    5. import android.view.GestureDetector;
    6. import android.view.MotionEvent;
    7. import android.view.View;
    8.  
    9. public class TouchView
    10. extends View
    11. {
    12.    
    13.     private static final String TAG = TouchView.class.getSimpleName();
    14.    
    15.     private final GestureDetector gesture;
    16.  
    17.     public TouchView (Context context) {
    18.         super(context);
    19.         gesture = new GestureDetector(context, new ScrollGesture());
    20.     }
    21.  
    22.     public boolean onTouchEvent (MotionEvent event) {
    23.         Log.d(TAG, "onTouchEvent(" + event + ") ");
    24.         return gesture.onTouchEvent(event);
    25.     }
    26.    
    27.     public class ScrollGesture
    28.     implements GestureDetector.OnGestureListener
    29.     {
    30.        
    31.         @Override
    32.         public boolean onDown (MotionEvent event) {
    33.             Log.d(TAG, "onDown(" + event + ")");
    34.             return false;
    35.         }
    36.  
    37.         @Override
    38.         public boolean onFling (
    39.             MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
    40.         {
    41.             Log.d(TAG, "onFling(" + velocityX + ", " + velocityY + ")");
    42.             return false;
    43.         }
    44.  
    45.         @Override
    46.         public boolean onScroll (
    47.             MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
    48.         {
    49.             Log.d(TAG, "onScroll(" + distanceX + ", " + distanceY + ")");
    50.             return false;
    51.         }
    52.  
    53.         @Override
    54.         public void onLongPress (MotionEvent event) {
    55.             Log.d(TAG, "onLongPress(" + event + ")");
    56.         }
    57.  
    58.         @Override
    59.         public void onShowPress (MotionEvent event) {
    60.             Log.d(TAG, "onShowPress(" + event + ")");
    61.         }
    62.  
    63.         @Override
    64.         public boolean onSingleTapUp (MotionEvent event) {
    65.             Log.d(TAG, "onSingleTapUp(" + event + ")");
    66.             return false;
    67.         }
    68.  
    69.     }
    70.  
    71. }
    Finally, here are the relevant log entries:

    Code (Text):
    1. 02-17 08:24:38.992: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4376aac0 action=0 x=89.0 y=90.0 pressure=1.0 size=0.0})
    2. 02-17 08:24:39.002: DEBUG/TouchView(958): onDown(MotionEvent{4376aac0 action=0 x=89.0 y=90.0 pressure=1.0 size=0.0})
    3. 02-17 08:24:39.022: DEBUG/Touchie(958): onTouchEvent(MotionEvent{4376aac0 action=0 x=89.0 y=140.0 pressure=1.0 size=0.0})
    4. 02-17 08:24:39.032: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4376aac0 action=0 x=89.0 y=140.0 pressure=1.0 size=0.0})
    5. 02-17 08:24:39.052: DEBUG/TouchView(958): onDown(MotionEvent{4376aac0 action=0 x=89.0 y=140.0 pressure=1.0 size=0.0})
    6. 02-17 08:24:39.102: DEBUG/Touchie(958): onTouchEvent(MotionEvent{4377f7b0 action=1 x=89.0 y=140.0 pressure=1.0 size=0.0})
    7. 02-17 08:24:39.125: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4377f7b0 action=1 x=89.0 y=140.0 pressure=1.0 size=0.0})
    8. 02-17 08:24:39.142: DEBUG/TouchView(958): onSingleTapUp(MotionEvent{4377f7b0 action=1 x=89.0 y=140.0 pressure=1.0 size=0.0})
    9. 02-17 08:24:39.862: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4377f7b0 action=0 x=195.0 y=79.0 pressure=1.0 size=0.0})
    10. 02-17 08:24:39.872: DEBUG/TouchView(958): onDown(MotionEvent{4377f7b0 action=0 x=195.0 y=79.0 pressure=1.0 size=0.0})
    11. 02-17 08:24:39.872: DEBUG/Touchie(958): onTouchEvent(MotionEvent{4377f7b0 action=0 x=195.0 y=129.0 pressure=1.0 size=0.0})
    12. 02-17 08:24:39.872: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4377f7b0 action=0 x=195.0 y=129.0 pressure=1.0 size=0.0})
    13. 02-17 08:24:39.882: DEBUG/TouchView(958): onDown(MotionEvent{4377f7b0 action=0 x=195.0 y=129.0 pressure=1.0 size=0.0})
    14. 02-17 08:24:40.082: DEBUG/Touchie(958): onTouchEvent(MotionEvent{4377f7b0 action=2 x=195.0 y=130.0 pressure=1.0 size=0.0})
    15. 02-17 08:24:40.432: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4377f7b0 action=2 x=195.0 y=130.0 pressure=1.0 size=0.0})
    16. 02-17 08:24:40.512: DEBUG/Touchie(958): onTouchEvent(MotionEvent{4377f7b0 action=2 x=195.0 y=280.0 pressure=1.0 size=0.0})
    17. 02-17 08:24:40.512: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4377f7b0 action=2 x=195.0 y=280.0 pressure=1.0 size=0.0})
    18. 02-17 08:24:40.522: DEBUG/TouchView(958): onScroll(0.0, -151.0)
    19. 02-17 08:24:40.532: DEBUG/Touchie(958): onTouchEvent(MotionEvent{4377f7b0 action=2 x=195.0 y=291.0 pressure=1.0 size=0.0})
    20. 02-17 08:24:40.532: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4377f7b0 action=2 x=195.0 y=291.0 pressure=1.0 size=0.0})
    21. 02-17 08:24:40.542: DEBUG/TouchView(958): onScroll(0.0, -11.0)
    22. 02-17 08:24:40.572: DEBUG/Touchie(958): onTouchEvent(MotionEvent{4377f7b0 action=1 x=195.0 y=291.0 pressure=1.0 size=0.0})
    23. 02-17 08:24:40.582: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4377f7b0 action=1 x=195.0 y=291.0 pressure=1.0 size=0.0})
    24. 02-17 08:24:40.582: DEBUG/TouchView(958): onFling(0.0, 545.22314)
    25. 02-17 08:24:41.392: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4377f7b0 action=0 x=40.0 y=323.0 pressure=1.0 size=0.0})
    26. 02-17 08:24:41.392: DEBUG/TouchView(958): onDown(MotionEvent{4377f7b0 action=0 x=40.0 y=323.0 pressure=1.0 size=0.0})
    27. 02-17 08:24:41.402: DEBUG/Touchie(958): onTouchEvent(MotionEvent{4377f7b0 action=0 x=40.0 y=373.0 pressure=1.0 size=0.0})
    28. 02-17 08:24:41.402: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4377f7b0 action=0 x=40.0 y=373.0 pressure=1.0 size=0.0})
    29. 02-17 08:24:41.412: DEBUG/TouchView(958): onDown(MotionEvent{4377f7b0 action=0 x=40.0 y=373.0 pressure=1.0 size=0.0})
    30. 02-17 08:24:41.842: DEBUG/Touchie(958): onTouchEvent(MotionEvent{4377f7b0 action=2 x=66.0 y=347.0 pressure=1.0 size=0.0})
    31. 02-17 08:24:41.862: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4377f7b0 action=2 x=66.0 y=347.0 pressure=1.0 size=0.0})
    32. 02-17 08:24:41.932: DEBUG/TouchView(958): onScroll(-26.0, 26.0)
    33. 02-17 08:24:41.962: DEBUG/Touchie(958): onTouchEvent(MotionEvent{437941f0 action=2 x=274.0 y=157.0 pressure=1.0 size=0.0})
    34. 02-17 08:24:41.972: DEBUG/TouchView(958): onTouchEvent(MotionEvent{437941f0 action=2 x=274.0 y=157.0 pressure=1.0 size=0.0})
    35. 02-17 08:24:41.972: DEBUG/TouchView(958): onScroll(-208.0, 190.0)
    36. 02-17 08:24:41.982: DEBUG/Touchie(958): onTouchEvent(MotionEvent{4377f7b0 action=1 x=274.0 y=157.0 pressure=1.0 size=0.0})
    37. 02-17 08:24:41.982: DEBUG/TouchView(958): onTouchEvent(MotionEvent{4377f7b0 action=1 x=274.0 y=157.0 pressure=1.0 size=0.0})
    38. 02-17 08:24:41.982: DEBUG/TouchView(958): onFling(1169.8003, -1033.79)
    39.  
    The way I count them, it looks like for each initial down event there's an extra onTouchEvent() call to TouchView in addition to the sequence that hits Touchie, the containing Activity. It seems odd that there should just be the initial down event to TouchView, but not for the following touch events (move, etc.). But perhaps I'm interpreting the data wrong.

    I'm still running against 1.6 because that's what is on my G1. Maybe this is fixed in 2.x. Assuming that there's anything wrong to begin with, and not just confusion.
     

    Advertisement

Share This Page

Loading...