- 浏览: 63063 次
- 性别:
- 来自: 沈阳
文章分类
最新评论
下午阅读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
滑动
基本介绍见 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
滑动
发表评论
-
android 备用
2014-08-03 13:26 422http://blog.csdn.net/rain_butte ... -
Android中px与dip,sp与dip等的转换工具类(转)
2014-07-16 20:07 577http://www.cnblogs.com/wader201 ... -
android 字体总结
2014-07-15 21:53 528Android提供三种字体:“Sans”,“serif“和“m ... -
mina
2014-07-03 14:12 483http://chwshuang.iteye.com/blog ... -
android获取IMEI
2014-06-17 08:34 507核心代码: Imei = ((TelephonyMan ... -
andorid ui
2014-06-04 22:39 0http://blog.csdn.net/ddddpow/ar ... -
android alarmManage
2014-05-28 21:47 671转自:http://jinguo.iteye.com/bl ... -
adaf
2014-05-27 22:32 0http://www.eoeandroid.com/thr ... -
weixin
2014-05-13 22:42 0android weisixn -
jpajpa
2014-01-28 15:06 487http://www.iteye.com/problems/9 ... -
吉安帕
2014-01-23 22:30 0dfsgdaffgadrgad -
CrudRepository
2013-11-28 19:58 0PagingAndSortingRepository Cr ... -
Android 的第三方报表组件,AChartEngine 案列
2013-08-19 16:48 0http://www.kuaipan.cn/file/id_3 ... -
android sdk adress
2013-08-12 13:23 450developer.android.com/sdk/index ... -
android 结束进程
2013-07-29 22:01 707Android获取apk的版本及包名等信息 http://bl ... -
adnroid 4.0.3去掉虚拟导航栏
2013-07-27 20:54 1015http://blog.csdn.net/wfh1988/ar ... -
andorid全屏
2013-07-27 10:15 771super.onCreate(savedIns ... -
Foursquare
2012-10-12 17:22 0Foursquare -
sqllite brower
2012-09-11 23:05 644http://www.oschina.net/p/sqlite ... -
notice
2012-09-03 21:13 671http://tibaloga.iteye.com/blog/ ...
相关推荐
下面小编就为大家带来一篇android onTouchEvent处理机制总结(必看)小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
重写onTouchEvent事件,实现上下左右滑动的小例子。
本篇文章小编为大家介绍,Android onTouchEvent 与 onInterceptTouchEvent的区别详解。需要的朋友参考下
下面小编就为大家带来一篇Android onTouchEvent事件中onTouch方法返回值(介绍)。小编觉得挺不错的,现在就分享给大家,也给大家做个参考。一起跟随小编过来看看吧
继承View类,实现触摸事件OnTouchEvent,使用菜单选项。
主要为大家详细介绍了Android运用onTouchEvent写一个上下滑动的布局,具有一定的参考价值,感兴趣的小伙伴们可以参考一下
此Demo是我进行改进的,既可以通过OnTouch对图片进行放大缩小移动,也可以对图片进行OnClick点击,OnLongClick点击。不会出现冲突。
这几天遇到点关于Android的触摸事件相关的,还跟onClick有关,暂且记下: LinearLayout分别设置了onTouchListener,onClickListener,onLongClickListener及onTouchEvent回调 1、在屏幕上触摸之后基本的执行流程如下...
NULL 博文链接:https://johnnycmj.iteye.com/blog/2234305
NULL 博文链接:https://trylovecatch.iteye.com/blog/1161450
NULL 博文链接:https://forlan.iteye.com/blog/2261417
NULL 博文链接:https://2528.iteye.com/blog/1056731
关于onInterceptTouchEvent和onTouchEvent使用demo,很不错的一个demo,可以帮助Android的触摸事件。
轻松弄懂android事件分发机制解析 android 中的事件处理一直以来困扰不少刚刚从事android开发的同学,网上也有不少讲解android事件分发的文章,然而讲解的都不够简洁!现在我将用另一种简洁的方式来讲解android事件...
传智播客_Andorid_20天精通Android开发视频_第14天视频_视频_7_画画板_副本创建_ontouchevent说明.avi