Android学习 多读官网,故意健康---手势
2024-08-30 04:08:12
官网地址 ttp://developer.android.com/training/gestures/detector.html:
一、能够直接覆盖Activity的onTouch方法
public class MainActivity extends Activity {
...
// This example shows an Activity, but you would use the same approach if
// you were subclassing a View.
@Override
public boolean onTouchEvent(MotionEvent event){
int action = MotionEventCompat.getActionMasked(event);
switch(action) {
case (MotionEvent.ACTION_DOWN) :
Log.d(DEBUG_TAG,"Action was DOWN");
return true;
case (MotionEvent.ACTION_MOVE) :
Log.d(DEBUG_TAG,"Action was MOVE");
return true;
case (MotionEvent.ACTION_UP) :
Log.d(DEBUG_TAG,"Action was UP");
return true;
case (MotionEvent.ACTION_CANCEL) :
Log.d(DEBUG_TAG,"Action was CANCEL");
return true;
case (MotionEvent.ACTION_OUTSIDE) :
Log.d(DEBUG_TAG,"Movement occurred outside bounds " +
"of current screen element");
return true;
default :
return super.onTouchEvent(event);
}
}
二:也能够给单个的view设置监听器
View myView = findViewById(R.id.my_view);
myView.setOnTouchListener(new OnTouchListener() {
public boolean onTouch(View v, MotionEvent event) {
// ... Respond to touch events
return true;
}
});
三:可是 onTouch方法毕竟仅仅能检測一些简单的手势,像滑动、双击、长按等。单用onTouch方法处理就显得棘手了,谷歌提供了方便的 GestureDetector
类来更方便的处理手势。
详细的须要实例 GestureDetectorCompat 类, 该类中的构造方法须要实现
监听器。GestureDetector.OnGestureListener 去通知用户,当各种事件(滑动、双击、长按等)发生。为了使
GestureDetector
正常监听事件,同一时候须要重写Activity的
nTouch方法。把事件交给Detector。
详细见官网demo:
public class MainActivity extends Activity implements
GestureDetector.OnGestureListener,
GestureDetector.OnDoubleTapListener{
private static final String DEBUG_TAG = "Gestures";
private GestureDetectorCompat mDetector; // Called when the activity is first created.
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Instantiate the gesture detector with the
// application context and an implementation of
// GestureDetector.OnGestureListener
mDetector = new GestureDetectorCompat(this,this);
// Set the gesture detector as the double tap
// listener.
mDetector.setOnDoubleTapListener(this);
} @Override
public boolean onTouchEvent(MotionEvent event){
this.mDetector.onTouchEvent(event);
// Be sure to call the superclass implementation
return super.onTouchEvent(event);
} @Override
public boolean onDown(MotionEvent event) {
Log.d(DEBUG_TAG,"onDown: " + event.toString());
return true;
} @Override
public boolean onFling(MotionEvent event1, MotionEvent event2,
float velocityX, float velocityY) {
Log.d(DEBUG_TAG, "onFling: " + event1.toString()+event2.toString());
return true;
} @Override
public void onLongPress(MotionEvent event) {
Log.d(DEBUG_TAG, "onLongPress: " + event.toString());
} @Override
public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
float distanceY) {
Log.d(DEBUG_TAG, "onScroll: " + e1.toString()+e2.toString());
return true;
} @Override
public void onShowPress(MotionEvent event) {
Log.d(DEBUG_TAG, "onShowPress: " + event.toString());
} @Override
public boolean onSingleTapUp(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapUp: " + event.toString());
return true;
} @Override
public boolean onDoubleTap(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTap: " + event.toString());
return true;
} @Override
public boolean onDoubleTapEvent(MotionEvent event) {
Log.d(DEBUG_TAG, "onDoubleTapEvent: " + event.toString());
return true;
} @Override
public boolean onSingleTapConfirmed(MotionEvent event) {
Log.d(DEBUG_TAG, "onSingleTapConfirmed: " + event.toString());
return true;
}
}
补充:
onScroll:在屏幕上慢慢的滚动。
onFling:在屏幕上高速的滚动,并高速移开手指。
Whether or not you useGestureDetector.OnGestureListener
, it's best practice to implement anonDown()
method that returnstrue
. This is because all gestures begin with anonDown()
message. If you returnfalse
fromonDown()
, asGestureDetector.SimpleOnGestureListener
does by default, the system assumes that you want to ignore the rest of the gesture, and the other methods ofGestureDetector.OnGestureListener
never get called. This has the potential to cause unexpected problems in your app. The only time you should returnfalse
fromonDown()
is if you truly want to ignore an entire gesture.
另外,官网上说最好复写onDown方法,并返回true,由于其它方法的消息都是从onDown方法開始的。并且onDown系统默认返回false,其它的方法也不会被调用。
但是我自己试了下,当onDown方法返回false,其它方法是能够被调用的。。这就奇怪了。。。哪位高手亲试过这个样例的,回复下。。
最新文章
- Android笔记:通知
- Number()数据类型转换
- CSS层模型
- Cwinux源码解析(一)
- onmouseenter与onmouseover
- csuoj 1337: 搞笑版费马大定理
- LinqToSql和ASP.NET Entity FrameWork 中使用事务
- POJ_3009——冰球,IDS迭代加深搜索
- NetAnalyzer笔记 之 十 通过邮件方式打造自己的bug反馈模块(C#)
- shell脚本定时备份数据库
- phpstorm 同步远程服务器代码
- updateByPrimaryKey和updateByPrimaryKeySelective insert和insertSelective
- 通用table样式
- mysql事务,视图,权限管理,索引,存储引擎(胖胖老师)
- 关于NB-IoT的十大问题和答案【转】
- Win10 安装SqlServer2012 无法访问数据库
- linux下umask的使用讲解
- Ubuntu16.04配置Eclipse开发OpenCV
- Java序列化机制原理
- quartz配置参数org.quartz.jobStore.misfireThreshold含义解释
热门文章
- Installing MySQL 5.7.23 on CentOS 7
- 查询SYS_ORG_TB树的层级
- 二手GTX650
- 性能优化 java 24 次阅读 · 读完需要 15 分钟 0
- CSS3---关于背景
- js layui 分页脚本
- 牛客网 牛可乐发红包脱单ACM赛 C题 区区区间间间
- HDU 1087 Super Jumping! Jumping! Jumping! (LIS的最大和)
- 【07】QQ群管理公告小结:
- UVALive 2957 Bring Them There