首先,我们都知道OnTouchListener类的用法,我们通常要创建一个OnTouchListener类的一个对象,然后通过覆写onTouch(View v, MotionEvent e)方法来处理对应的各个动作事件的。MotionEvent中的三个动作我们都知道,MotionEvent.ACTION_DOWN是指触摸起始,MotionEvent.ACTION_MOVE是指触摸滑动,MotionEvent.ACTION_UP是指触摸拿起。当然这里不会说这些内容,我要说的是具体返回值的问题,都知道onTouch(View v, MotionEvent e)的返回值是个布尔值。那么具体的布尔值是什么含义呢?我们先上个例子:
代码示例:
xml文件如下:
<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" >
<View
android:id="@+id/myView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="#00FF00" />
</RelativeLayout>
源代码如下:
package com.example.test;
import android.Manifest;
import android.app.Activity;
import android.os.Bundle;
import android.util.DisplayMetrics;
import android.util.Log;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;
import android.view.Window;
import android.view.WindowManager;
public class MainActivity extends Activity {
private View myView = null;
private boolean oldTouch[][] = null; //用Boolean类型值存储触摸板的第一次触摸轨迹
private boolean newTouch[][] = null; //用Boolean类型值存储触摸板的第二次触摸轨迹
private boolean flag = true; //设置标志来判断第一次触屏还是第二次触屏,进而记录两次触屏的数组值
private int w; //存储View的宽度
private int h; //存储View的高度
private final String tag="output";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
this.requestWindowFeature(Window.FEATURE_NO_TITLE); //使myView控件全屏化
this.getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN,
WindowManager.LayoutParams.FLAG_FULLSCREEN);
setContentView(R.layout.activity_main);
DisplayMetrics display = new DisplayMetrics(); //用DisplayMetrics类的对象来存储手机屏幕的信息
getWindow().getWindowManager().getDefaultDisplay().getMetrics(display); //通过getMetrics()方法类获取手机屏幕信息,并将其放入display对象中
w=display.widthPixels;
h=display.heightPixels;
oldTouch = new boolean[h][w]; //实例化oldTouch对象,并定义boolean数组的宽度和长度跟屏幕的分辨率的像素点宽度长度一致
for(int i = 0;i<h;i++){ //初始化oldTouch的值为false
for(int j = 0;j<w;j++){
oldTouch[i][j]=false;
}
}
newTouch = new boolean[h][w]; //初始化第二个boolean数组内容
for(int i = 0;i<h;i++){
for(int j = 0;j<w;j++){
oldTouch[i][j]=false;
}
}
Log.d(tag, "w="+w+", h="+h);
myView = (View)findViewById(R.id.myView);
//w = (int) myView.getWidth();
//h = (int) myView.getHeight();
//
/*int width = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
int height = View.MeasureSpec.makeMeasureSpec(0,View.MeasureSpec.UNSPECIFIED);
myView.measure(width, height);
w = myView.getMeasuredWidth();
h = myView.getMeasuredHeight();*/
myView.setOnTouchListener(new OnTouchListener() { //设置监听器
@Override
public boolean onTouch(View v, MotionEvent e) { //覆写onTouch方法来处理TouchMontion动作
int x;
int y;
switch(e.getAction()){
case MotionEvent.ACTION_DOWN:
x = (int)e.getX();
y = (int)e.getY();
Log.d(tag,"isOk");
if(flag==true){
oldTouch[y][x] = true; //凡是触摸过的轨迹全部记录在boolean数组中,并设置标志为真
Log.d(tag,"x="+x+", y="+y+", oldTouch["+y+"]"+
"["+(x)+"]="+oldTouch[x-1][y-1]);
}else{
newTouch[y][x] = true; //凡是触摸过的轨迹全部记录在boolean数组中,并设置标志为真
Log.d(tag,"x="+x+", y="+y+", newTouch["+y+"]"+
"["+x+"]="+newTouch[y][x]);
}
return true;
//System.out.println("x="+x+" y="+y);
//break;
case MotionEvent.ACTION_MOVE:
x=(int)e.getX();
y=(int)e.getY();
if(flag==true){
oldTouch[y][x] = true; //凡是触摸过的轨迹全部记录在boolean数组中,并设置标志为真
Log.d(tag,"x="+x+", y="+y+", oldTouch["+y+"]"+
"["+x+"]="+oldTouch[y][x]);
System.out.println("x="+x+" y="+y);
}else{
newTouch[y][x] = true; //凡是触摸过的轨迹全部记录在boolean数组中,并设置标志为真
Log.d(tag,"x="+x+", y="+y+", newTouch["+y+"]"+
"["+x+"]="+newTouch[y][x]);
}
return true;
//break;
case MotionEvent.ACTION_UP:
x=(int)e.getX();
y=(int)e.getY();
if(flag==true){
oldTouch[y][x] = true; //凡是触摸过的轨迹全部记录在boolean数组中,并设置标志为真
Log.d(tag,"x="+x+", y="+y+", oldTouch["+y+"]"+
"["+x+"]="+oldTouch[y][x]);
}else{
newTouch[y][x] = true; //凡是触摸过的轨迹全部记录在boolean数组中,并设置标志为真
Log.d(tag,"x="+x+", y="+y+", newTouch["+y+"]"+
"["+x+"]="+newTouch[y][x]);
}
flag = !flag;
return false;
//break;
}
// TODO 自动生成的方法存根
//return false;
}
});
}
@Override
public boolean onCreateOptionsMenu(Menu menu) {
// Inflate the menu; this adds items to the action bar if it is present.
getMenuInflater().inflate(R.menu.main, menu);
return true;
}
}
结果如下:
通过这个例子我们可以知道在完成一个动作以后如果返回值为true的话,那么该动作执行完以后会继续在OnTouchListener(View v,MotionEvent e)中等待下一次触摸动作的到来不会跳出本次方法,然而如果返回值是false的话,那么执行完本次触摸动作以后,会跳出本次方法,等待下一次的方法,也就是说,如果是false的话它不会继续在本次监听中继续监听,而是执行完本次监听,等待下次的监听,总结来讲:
1.返回值是true,执行完一个动作以后不会结束本次事件的监听,而是继续监听本次事件余下的动作
2.返回值是false,执行完一个动作以后结束本次事件的监听,不会在监听余下的动作,等待下一次触摸事件的到来