概述:
在上回,从原理上讲解了android中触摸机制的运行模式,那么这次我们通过代码来验证一下这个结论。是驴子是马拿出来遛一遛吧。另外,所学知识有限,如果有任何问题欢迎拍砖和交流。
补充一下,其实我写的(一)中,存在一些问题,对于三个与触摸机制息息相关的方法,并不是上文说的那么容统,细分而言。只有ViewGroup才完全拥有这三个方法(onTouchEvent,onInterceptTouchEvent,dispatchTouchEvent),而对于View并没有onInterceptTouchEvent,其实理由很简单,我已经是最小单位了,我还需要拦截么?我下面已经木有东东可以接收了~
案例驱动:
MainActivity.class
这里抛出一个问题,有兴趣可以回答一下:activity和View是什么关系呢?是不是感觉两者好像都是界面?还是另有隐情呢?
public class MainActivity extends Activity{
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
switch (action) {
case MotionEvent.ACTION_DOWN:
System.out.println("MainActivity---dispatchTouchEvent---ACTION_DOWN");
break;
case MotionEvent.ACTION_UP:
System.out.println("MainActivity---dispatchTouchEvent---ACTION_UP");
break;
default:
break;
}
boolean result = super.dispatchTouchEvent(event);
System.out.println("MainActivity---dispatchTouchEvent---result:"+result);
return result;
}
}
自定义Button,作为最小View单位,是没有onInterceptTouchEvent方法可以复写的。其中只复写了其他两个方法。
public class MyButton extends Button {
public MyButton(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
System.out.println("MyButton---onTouchEvent---ACTION_DOWN");
if (action == MotionEvent.ACTION_MOVE)
System.out.println("MyButton---onTouchEvent---ACTION_MOVE");
if (action == MotionEvent.ACTION_UP)
System.out.println("MyButton---onTouchEvent---ACTION_UP");
return true;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
System.out.println("MyButton---dispatchTouchEvent---ACTION_DOWN");
if (action == MotionEvent.ACTION_MOVE)
System.out.println("MyButton---dispatchTouchEvent---ACTION_MOVE");
if (action == MotionEvent.ACTION_UP)
System.out.println("MyButton---dispatchTouchEvent---ACTION_UP");
boolean result = super.dispatchTouchEvent(event);
System.out.println("MyButton---dispatchTouchEvent---result:"+result);
return result;
}
}
自定义ViewGroup的子类,MyLinearLayout,可以看到它是存在onInterceptTouchEvent方法,对于其中的子View,它可以进行拦截或者不拦截。
public class MyLinearLayout extends LinearLayout {
public MyLinearLayout(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
System.out.println("MyLinearLayout---onTouchEvent---ACTION_DOWN");
if (action == MotionEvent.ACTION_MOVE)
System.out.println("MyLinearLayout---onTouchEvent---ACTION_MOVE");
if (action == MotionEvent.ACTION_UP)
System.out.println("MyLinearLayout---onTouchEvent---ACTION_UP");
boolean result = super.onTouchEvent(event);
System.out.println("MyLinearLayout---onTouchEvent---result:"+result);
return result;
}
@Override
public boolean onInterceptTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
System.out.println("MyLinearLayout---onInterceptTouchEvent---ACTION_DOWN");
if (action == MotionEvent.ACTION_MOVE)
System.out.println("MyLinearLayout---onInterceptTouchEvent---ACTION_MOVE");
if (action == MotionEvent.ACTION_UP)
System.out.println("MyLinearLayout---onInterceptTouchEvent---ACTION_UP");
boolean result = super.onInterceptTouchEvent(event);
result = true;
System.out.println("MyLinearLayout---onInterceptTouchEvent---result:"+result);
return result;
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
System.out.println("MyLinearLayout---dispatchTouchEvent---ACTION_DOWN");
if (action == MotionEvent.ACTION_MOVE)
System.out.println("MyLinearLayout---dispatchTouchEvent---ACTION_MOVE");
if (action == MotionEvent.ACTION_UP)
System.out.println("MyLinearLayout---dispatchTouchEvent---ACTION_UP");
boolean result = super.dispatchTouchEvent(event);
System.out.println("MyLinearLayout---dispatchTouchEvent---result:"+result);
return result;
}
}
自定义ImageView,和自定义Button相同,作为最小View单位,不存在onInterceptTouchEvent
public class MyImageView extends ImageView {
public MyImageView(Context context, AttributeSet attrs) {
super(context, attrs);
}
@Override
public boolean dispatchTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
System.out.println("MyImageView---dispatchTouchEvent---ACTION_DOWN");
if (action == MotionEvent.ACTION_MOVE)
System.out.println("MyImageView---dispatchTouchEvent---ACTION_MOVE");
if (action == MotionEvent.ACTION_UP)
System.out.println("MyImageView---dispatchTouchEvent---ACTION_UP");
boolean result = super.dispatchTouchEvent(event);
System.out.println("MyImageView---dispatchTouchEvent---result:"+result);
return result;
}
@Override
public boolean onTouchEvent(MotionEvent event) {
int action = event.getAction();
if (action == MotionEvent.ACTION_DOWN)
System.out.println("MyImageView---onTouchEvent---ACTION_DOWN");
if (action == MotionEvent.ACTION_MOVE)
System.out.println("MyImageView---onTouchEvent---ACTION_MOVE");
if (action == MotionEvent.ACTION_UP)
System.out.println("MyImageView---onTouchEvent---ACTION_UP");
return true;
}
}
最后看一下布局文件,很简单,这里不赘述了。
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context=".MainActivity" >
<TextView
android:id="@+id/title"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:textStyle="bold"
android:textColor="@color/blue"
android:text="@string/touch" />
<com.example.touchevent.MyButton
android:id="@+id/button1"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@id/title"
android:layout_marginTop="10dp"
android:text="@string/mybutton1" />
<com.example.touchevent.MyLinearLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@id/button1"
android:layout_marginTop="50dp"
android:background="@color/blue"
android:gravity="center" >
<com.example.touchevent.MyRelativeLayout
android:layout_width="200dp"
android:layout_height="200dp"
android:gravity="center"
android:background="@color/dark_gray" >
<com.example.touchevent.MyImageView
android:layout_width="100dp"
android:layout_height="100dp"
android:scaleType="fitCenter"
android:src="@drawable/jacky"
/>
</com.example.touchevent.MyRelativeLayout>
</com.example.touchevent.MyLinearLayout>
</RelativeLayout>