android ontouchevent

本文详细探讨了Android应用开发中遇到的触摸事件处理混乱问题,并通过实例展示了如何正确实现onTouchEvent方法,避免常见错误。文章还提供了解决方案及测试结果,帮助开发者提升触摸交互体验。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

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

主要代码如下:

MainActivity
[java] view plaincopyprint?

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

[java] view plaincopyprint?

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

滑动
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值