I found something that confuses me about propagation of touch events. I wrote a simple test application to demonstrate. The Activity object:
The View displayed in the Activity:
Finally, here are the relevant log entries:
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.
Code:
package org.doorways.touchie;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.MotionEvent;
public class Touchie extends Activity {
private static final String TAG = Touchie.class.getSimpleName();
private TouchView touchView;
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
touchView = new TouchView(this);
setContentView(touchView);
}
public boolean onTouchEvent (MotionEvent event) {
Log.d(TAG, "onTouchEvent(" + event + ") ");
return touchView.onTouchEvent(event);
}
}
Code:
package org.doorways.touchie;
import android.content.Context;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
public class TouchView
extends View
{
private static final String TAG = TouchView.class.getSimpleName();
private final GestureDetector gesture;
public TouchView (Context context) {
super(context);
gesture = new GestureDetector(context, new ScrollGesture());
}
public boolean onTouchEvent (MotionEvent event) {
Log.d(TAG, "onTouchEvent(" + event + ") ");
return gesture.onTouchEvent(event);
}
public class ScrollGesture
implements GestureDetector.OnGestureListener
{
@Override
public boolean onDown (MotionEvent event) {
Log.d(TAG, "onDown(" + event + ")");
return false;
}
@Override
public boolean onFling (
MotionEvent e1, MotionEvent e2, float velocityX, float velocityY)
{
Log.d(TAG, "onFling(" + velocityX + ", " + velocityY + ")");
return false;
}
@Override
public boolean onScroll (
MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)
{
Log.d(TAG, "onScroll(" + distanceX + ", " + distanceY + ")");
return false;
}
@Override
public void onLongPress (MotionEvent event) {
Log.d(TAG, "onLongPress(" + event + ")");
}
@Override
public void onShowPress (MotionEvent event) {
Log.d(TAG, "onShowPress(" + event + ")");
}
@Override
public boolean onSingleTapUp (MotionEvent event) {
Log.d(TAG, "onSingleTapUp(" + event + ")");
return false;
}
}
}
Code:
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})
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})
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})
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})
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})
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})
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})
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})
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})
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})
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})
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})
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})
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})
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})
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})
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})
02-17 08:24:40.522: DEBUG/TouchView(958): onScroll(0.0, -151.0)
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})
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})
02-17 08:24:40.542: DEBUG/TouchView(958): onScroll(0.0, -11.0)
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})
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})
02-17 08:24:40.582: DEBUG/TouchView(958): onFling(0.0, 545.22314)
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})
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})
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})
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})
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})
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})
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})
02-17 08:24:41.932: DEBUG/TouchView(958): onScroll(-26.0, 26.0)
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})
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})
02-17 08:24:41.972: DEBUG/TouchView(958): onScroll(-208.0, 190.0)
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})
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})
02-17 08:24:41.982: DEBUG/TouchView(958): onFling(1169.8003, -1033.79)
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.