android onTouchEvent响应测试

下午阅读onTouchEvent资料,发现方法调用有点混乱,没有完全按照api讲的执行,故挑了例子测试有MainActivity类和MySurfaceView类,

基本介绍见 http://blog.csdn.net/xiaominghimi/article/details/6127578

包括这个blog前面有关onTouchEvent介绍,

主要代码如下:

MainActivity

package com.s;

import android.app.Activity;
import android.os.Bundle;
import android.view.MotionEvent;

public class MainActivity extends Activity {

	@Override
	public void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(new MySurfaceView(this));
		
	}
	
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		boolean result;
		switch (event.getAction()) {
	        case MotionEvent.ACTION_UP:
	        	result = true;
	            break;
	        case MotionEvent.ACTION_DOWN:
	        	result = true;
	            break;
	        case MotionEvent.ACTION_CANCEL:
	        	result = true;
	            break;
	        case MotionEvent.ACTION_MOVE:
	        	result = false;
	            break;
	        default :
	        	result = true;
	        	break;
		}
		System.out.println("activity touch: " + result);
		return result;
	}
	
}


MySurfaceView

package com.s;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.SurfaceHolder;
import android.view.SurfaceHolder.Callback;
import android.view.SurfaceView;

/**
 * 
 * @author Himi
 * 
 */
public class MySurfaceView extends SurfaceView implements Callback, Runnable {
	public static MySurfaceView msrv;
	private int move_x = 2, x = 20;
	private Thread th;
	private SurfaceHolder sfh;
	private Canvas canvas;
	private Paint p;

	public MySurfaceView(Context context) {
		super(context);
		msrv = this;
		p = new Paint();
		p.setAntiAlias(true);
		sfh = this.getHolder();
		sfh.addCallback(this);
		th = new Thread(this);
		this.setKeepScreenOn(true);
		this.setFocusable(false);
		this.setFocusableInTouchMode(true);
	}

	int i = 0;
	boolean result = false;
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		int key = event.getAction();
		switch (key) {
	        case MotionEvent.ACTION_UP:
	        	result = false;
	            break;
	
	        case MotionEvent.ACTION_DOWN:
	        	result = true;
	            break;
	        case MotionEvent.ACTION_CANCEL:
	        	result = true;
	            break;
	
	        case MotionEvent.ACTION_MOVE:
	        	
	        	if (i > 0) {
	        		result = false;
	        	}
	        	i++;
	            break;
            default :
            	result = true;
            	break;
		}
		System.out.println("surface  touch: " + result);
		return result;
	}
	
	@Override
	public boolean onKeyDown(int keyCode, KeyEvent event) {
		boolean result = super.onKeyDown(keyCode, event);
		System.out.println("surface  key  : " + result);
		return true;
	}

	public void surfaceCreated(SurfaceHolder holder) {
		th.start();
	}

	public void draw() {
		canvas = sfh.lockCanvas();
		if (canvas != null) {
			canvas.drawColor(Color.WHITE);
			canvas.drawText("Surfaceview", x + move_x, 280, p);
			sfh.unlockCanvasAndPost(canvas);
		}
	}

	private void logic() {
		x += move_x;
		if (x > 200 || x < 80) {
			move_x = -move_x;
		}
	}

	public void run() {
		// TODO Auto-generated method stub
		while (true) {
			draw();
			logic();
			try {
				Thread.sleep(100);
			} catch (Exception ex) {
			}
		}
	}

	public void surfaceChanged(SurfaceHolder holder, int format, int width,
			int height) {
	}

	public void surfaceDestroyed(SurfaceHolder holder) {
	}

}


测试结果:

点击

touchevent-1

touchevent-2

touchevent-3

touchevent-4

MysurfaceView

false - down - 1

备注1

 

 

mainActivity

 down - 2

up -3

 

 

点击

 

 

 

 

MysurfaceView

true - down - 1

false - up - 2

 

 

mainActivity

 

 up - 3

 

 

点击

 

 

 

 

MysurfaceView

true - down - 1

true - up - 2

 

 

mainActivity

 

 

 

 

 

 

 

 

 

滑动

 

 

 

 

MysurfaceView

false - down - 1

 

 

 

mainActivity

 down - 2

move - 3

move - 4

up - 5

滑动

 

 

 

 

MysurfaceView

true - down - 1

false - move - 2

 true - move - 4

false - up - 5

mainActivity

 

move - 3

 

up - 6

滑动

 

 

 

 

MysurfaceView

true- down - 1

true - move - 2

false - move - 3

false - up - 5

mainActivity

 

 

move - 4

up - 6

备注1:本来应该有viewup - 3的,分析表格发现android没有这样处理。

true - down - 2 表示返回值设为true,动作为down,调用onTouchEvent第2次


  • 结论:
  • 1. 当surfaceview的touch事件为down且返回false,本次动作不再调用surfaceview的onTouchEvent而直接调用mainActivity          的onTouchEvent;
  • 2. 而surfaceview的touch事件为move且返回false,还是按照api里的解释——True if the event was handled, false otherwise.即结果为true表示处理完,false则需要继续回调mainActivity的onTouchEvent;

  • 注:单击一次,touch事件为down-up
  •     滑动一次,touch事件为down-move-move-up
  • 我觉得google应该在api中说的更明白些



  • 0
    点赞
  • 4
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值