android事件分发流程很长,从驱动到EventHub到Input模块再到view。这里不做那么复杂的验证,只是验证应用中需要Override的接口,以在不同的时机处理。
具体来说是这几个
ViewGroup、View
Activity:onTouchEvent,dispatchTouchEvent
ViewGroup:dispatchTouchEvent,onInterceptTouchEvent,onTouchEvent(继承了view)
View:onTouchEvent,dispatchTouchEvent
先上个代码:
public class MainActivity extends AppCompatActivity { private static final String TAG = "AndroidDemo"; private Button mClick; private LinearLayout mLayout = null; private TextView mTv = null; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); Log.d(TAG, "Android Demo onCreate"); createViewByCode(); } private void createViewByCode() { mLayout = new SLinearLayout(this); mLayout.setLayoutParams(new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT)); mLayout.setOrientation(LinearLayout.VERTICAL); mTv = new STextView(this); mTv.setText("Hello code"); mTv.setTextSize(19); LinearLayout.LayoutParams params = new LinearLayout.LayoutParams( ViewGroup.LayoutParams.MATCH_PARENT, ViewGroup.LayoutParams.MATCH_PARENT); mLayout.addView(mTv, params); setContentView(mLayout); } @Override public boolean onTouchEvent(MotionEvent event) { Log.d(TAG, "activity onTouchEvent"); return super.onTouchEvent(event); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { Log.d(TAG, "activity dispatchTouchEvent"); return super.dispatchTouchEvent(ev); } } class SLinearLayout extends LinearLayout { SLinearLayout(Context context) { super(context); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { Log.d("AndroidDemo", "layout dispatchTouchEvent"); return super.dispatchTouchEvent(ev); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { Log.d("AndroidDemo", "layout onInterceptTouchEvent"); return super.onInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { Log.d("AndroidDemo", "layout onTouchEvent"); return super.onTouchEvent(event); } } class STextView extends TextView { STextView(Context context) { super(context); } @Override public boolean onTouchEvent(MotionEvent event) { Log.d("AndroidDemo", "tv onTouchEvent"); return super.onTouchEvent(event); } @Override public boolean dispatchTouchEvent(MotionEvent event) { Log.d("AndroidDemo", "tv dispatchTouchEvent"); return super.dispatchTouchEvent(event); } }
模拟器截图
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: activity dispatchTouchEvent
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: layout dispatchTouchEvent
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: layout onInterceptTouchEvent
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: tv dispatchTouchEvent
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: tv onTouchEvent
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: layout onTouchEvent
09-12 03:38:34.744 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: activity onTouchEvent
09-12 03:38:34.791 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: activity dispatchTouchEvent
09-12 03:38:34.791 21402-21402/com.ali.yunos.androiddemo D/AndroidDemo: activity onTouchEvent
结果分析待续