android自定义控件系列教程-----touch事件的传递
前沿:
自定义控件阐述:
Touch事件的传递
@Override public boolean dispatchTouchEvent(MotionEvent event) { // TODO Auto-generated method stub return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub return super.onTouchEvent(event); }而能做为容器的类如ViewGroup以及继承它的类会有这几个函数:
@Override public boolean onInterceptTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub return super.onInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { // TODO Auto-generated method stub return super.onTouchEvent(event); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { // TODO Auto-generated method stub return super.dispatchTouchEvent(ev); }现在我们就分别来重写这几个函数分别走一遍流程,让我们更能清楚的看到android的touch事件是怎么传递的。我们来重写一下这两类的方法。重写一个TextView
public class MyTextView extends TextView{ private final String TAG = MyTextView.class.getSimpleName(); public MyTextView(Context context) { super(context); } @Override public boolean dispatchTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_DOWN"); break; case MotionEvent.ACTION_POINTER_DOWN: Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_POINTER_DOWN"); break; case MotionEvent.ACTION_POINTER_UP: Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_POINTER_UP"); break; case MotionEvent.ACTION_MOVE: Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_UP"); break; } return super.dispatchTouchEvent(event); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.d(TAG, TAG + "onTouchEvent+ACTION_DOWN"); break; case MotionEvent.ACTION_POINTER_DOWN: Log.d(TAG, TAG + "onTouchEvent+ACTION_POINTER_DOWN"); break; case MotionEvent.ACTION_POINTER_UP: Log.d(TAG, TAG + "onTouchEvent+ACTION_POINTER_UP"); break; case MotionEvent.ACTION_MOVE: Log.d(TAG, TAG + "onTouchEvent+ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.d(TAG, TAG + "onTouchEvent+ACTION_UP"); break; } return super.onTouchEvent(event); } }然后我们再重写一个LinearLayout
public class MyLinearLayout extends LinearLayout { private final String TAG = MyLinearLayout.class.getSimpleName(); public MyLinearLayout(Context context) { super(context); } @Override public boolean onInterceptTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: Log.d(TAG, TAG + "onInterceptTouchEvent+ACTION_DOWN"); break; case MotionEvent.ACTION_POINTER_DOWN: Log.d(TAG, TAG + "onInterceptTouchEvent+ACTION_POINTER_DOWN"); break; case MotionEvent.ACTION_POINTER_UP: Log.d(TAG, TAG + "onInterceptTouchEvent+ACTION_POINTER_UP"); break; case MotionEvent.ACTION_MOVE: Log.d(TAG, TAG + "onInterceptTouchEvent+ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.d(TAG, TAG + "onInterceptTouchEvent+ACTION_UP"); break; } return super.onInterceptTouchEvent(ev); } @Override public boolean onTouchEvent(MotionEvent event) { switch (event.getAction()) { case MotionEvent.ACTION_DOWN: Log.d(TAG, TAG + "onTouchEvent+ACTION_DOWN"); break; case MotionEvent.ACTION_POINTER_DOWN: Log.d(TAG, TAG + "onTouchEvent+ACTION_POINTER_DOWN"); break; case MotionEvent.ACTION_POINTER_UP: Log.d(TAG, TAG + "onTouchEvent+ACTION_POINTER_UP"); break; case MotionEvent.ACTION_MOVE: Log.d(TAG, TAG + "onTouchEvent+ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.d(TAG, TAG + "onTouchEvent+ACTION_UP"); break; } return super.onTouchEvent(event); } @Override public boolean dispatchTouchEvent(MotionEvent ev) { switch (ev.getAction()) { case MotionEvent.ACTION_DOWN: Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_DOWN"); break; case MotionEvent.ACTION_POINTER_DOWN: Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_POINTER_DOWN"); break; case MotionEvent.ACTION_POINTER_UP: Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_POINTER_UP"); break; case MotionEvent.ACTION_MOVE: Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_MOVE"); break; case MotionEvent.ACTION_UP: Log.d(TAG, TAG + "dispatchTouchEvent+ACTION_UP"); break; } return super.dispatchTouchEvent(ev); } }
public class TestActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); MyLinearLayout layout = new MyLinearLayout(this); MyTextView myTextView = new MyTextView(this); myTextView.setText("touch event"); layout.addView(myTextView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT)); setContentView(layout); } }我们来看看log
结论:
郑重声明:本站内容如果来自互联网及其他传播媒体,其版权均属原媒体及文章作者所有。转载目的在于传递更多信息及用于网络分享,并不代表本站赞同其观点和对其真实性负责,也不构成任何其他建议。