Android_GestureDetector
<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"
android:paddingBottom="@dimen/activity_vertical_margin"
android:paddingLeft="@dimen/activity_horizontal_margin"
android:paddingRight="@dimen/activity_horizontal_margin"
android:paddingTop="@dimen/activity_vertical_margin"
tools:context="com.example.android_gesturedetector.MainActivity" >
<ImageView
android:id="@+id/image"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:src="@drawable/ic_launcher"/>
</RelativeLayout>
源代码:
package com.example.android_gesturedetector; import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MenuItem;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;
/*
* 使用GestureDetector进行手势识别
* 手势交互过程(原理):
* 1.触屏一刹那,触发MotionEvent事件(移动事件)
* 2.被OnTouchListener监听,在onTouch()中获得MotionEvent对象
* 3.GestureDetector(手势识别器)转发MotionEvent对象至OnGestureListener
* 4.OnGestureListener获得该对象,根据该对象封装的信息做出合适的反馈
*
*
*
* MotionEvent:
* 1.用于封装手势,触摸笔,轨迹球等动作事件
* 2.内部封装用于记录横轴和纵轴坐标的属性x和y
*
* GestureDetector:(手势识别器)
* 识别各种手势
* 工作原理:
* 1.当接收到用户触摸消息时 ,将消息交给GestureDetector加工
* 2.通过设置监听器获得GestureDetector处理后的手势
* GestureDetector提供了两个接口
* OnGestureListener:(是被单击事件)
* 1.手势交互的监听接口,其提供多个抽象方法
* 2.根据GestureDetector的手势识别结果调用相对应的方法
* OnDoubleTapListener.
* SimpleOnGeistureListener实现了上面两种接口
* 1.继承SimpleOnGestureListener
* 2.可以只重载感兴趣的手势
*/
public class MainActivity extends Activity {
private ImageView image;
private GestureDetector myGestureDetector;
class MyGestureListener extends SimpleOnGestureListener{
@Override
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
float velocityY) {
// TODO Auto-generated method stub
if(e1.getX()-e2.getX()>50){
Toast.makeText(MainActivity.this, "从右往左滑", Toast.LENGTH_SHORT).show();
}else if(e2.getX()-e1.getX()>50){
Toast.makeText(MainActivity.this,"从左往右滑",Toast.LENGTH_SHORT).show();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
image = (ImageView) findViewById(R.id.image);
myGestureDetector = new GestureDetector(new MyGestureListener());//实例化GestureDetector
image.setOnTouchListener(new OnTouchListener() { @Override//可以捕获到触摸屏幕发生的event事件
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
myGestureDetector.onTouchEvent(event);//GestureDetector对象通过onTouchEvent方法将event事件转发给GestureListener接口的实现类
return true;
}
}); } }
Android_GestureDetector的更多相关文章
- 转:GestureDetector: GestureDetector 基本使用
Gesture在 ViewGroup中使用 GestureDetector类可以让我们快速的处理手势事件,如点击,滑动等. 使用GestureDetector分三步: 1. 定义GestureDete ...
- maven dependendency
登录|注册 zhengsj的专栏 目录视图 摘要视图 订阅 [公告]博客系统优化升级 [收藏]Html5 精品资源汇集 博乐招募开始啦 Maven De ...
随机推荐
- cdn是什么和作用有些
内容分发网络其基本思路是尽可能避开互联网上有可能影响数据传输速度和稳定性的瓶颈和环节,使内容传输的更快.更稳定.通过在网络各处放置节点服务器所构 成的在现有的互联网基础之上的一层智能虚拟网络,CDN系 ...
- 将UIWebView显示的内容转为图片和PDF
今天开发MarkEditor时要用到将 UIWebView 中显示的内容转为图片,方便转发到各个社交网络(Twiiter,Facebook,Weibo),这样内容就不受长度限制,类似于长微博. 之前关 ...
- bzoj 1432 [ZJOI2009]Function(找规律)
[题目链接] http://www.lydsy.com/JudgeOnline/problem.php?id=1432 [思路] 找(cha)规(ti)律(jie) 分析戳这儿 click here ...
- LightOJ 1422 Halloween Costumes
dp[i]][j]=min(dp[i+1][j]+1,dp[i+1][k-1]+dp[k][j]) 表示第i天到j的最小数量.如果第i天的衣服只自己穿的话,不考虑后面的就是dp[i][j]=dp[i+ ...
- MVC 部署出现错误未能写入输出文件xxxxxxx.
编译器错误消息: CS0016: 未能写入输出文件“c:\Windows\Microsoft.NET\Framework64\v4.0.30319\Temporary ASP.NET Files\ro ...
- Chrome 浏览器跨域和安全访问问题 使用 chrome的命令行标记:disable-web-security 参数联调线上数据
做前端的,用Ajax获取数据,是常有的事情,同域下自然没问题了,如果是不同域获取数据,浏览器就有个同源策略的限制. 如图: Origin * is not allowed by Access-Cont ...
- 从Web借鉴UI设计
从Web借鉴UI设计 用户体验已经成为衡量应用软件质量的重要标准.在过去我们可能会惊叹于某个Web应用的华丽界面,现在,随着HTML5的强势登场,各类表现层技术及开发框架的发布,Web与窗体应用的界限 ...
- ECSHOP在线手册布局参考图--商品分类页 category.dwt
A.购物车 1,设置方法 程序自动读取购物车的商品数量 2,代码相关 cart.lbi 中 {insert_scripts files='transport.js'} <div clas ...
- 制作第三方SDK静态库、.framework
静态库和动态库的存在形式 静态库: .a 和 .framework 动态库: .dylib 和 .framework 静态库和动态库的使用区别: 静态库:链接时,静态库会被完整地复制 到 可执行文件中 ...
- Codeforces Round #321 (Div. 2) E. Kefa and Watch 线段树hash
E. Kefa and Watch Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://codeforces.com/contest/580/prob ...