手势交互过程
OnTouchlistenner:手势交互接口根据手势识别结果调用相对应的方法
GestureDetector:手势识别器
MotionEvent:用封装手势、触摸板、轨迹球等动作事件记录属性X和Y
1、触屏的一刹那,触发MotionEvent事件
2、被OnTouchlistenner监听,在ontouch()中获得MotionEvent对象
3、GestureDetector转发MotionEvent对象至OnTouchlistenner----
----myGestureDetector.onTouchEvent(event)改变ontouch方法状态(true)
4、OnTouchlistenner获的该对象,根据该对象封装的信息做出合适的反馈
GestureDetector:手势识别器
按下、移动、抬起。
OnTouchlistenner:手势交互接口根据手势识别结果调用相对应的方法
GestureDetector:手势识别器
MotionEvent:用封装手势、触摸板、轨迹球等动作事件记录属性X和Y
1、触屏的一刹那,触发MotionEvent事件
2、被OnTouchlistenner监听,在ontouch()中获得MotionEvent对象
3、GestureDetector转发MotionEvent对象至OnTouchlistenner----
----myGestureDetector.onTouchEvent(event)改变ontouch方法状态(true)
4、OnTouchlistenner获的该对象,根据该对象封装的信息做出合适的反馈
GestureDetector:手势识别器
按下、移动、抬起。
GestureDetector:手势识别器——原理:
1、当收到用户触摸消息时,将消息交给GestureDetector加工
2、通过设置监听器获取GestureDetector处理后的手势
GestureDetector提供两个监听器
1、Ongesturelistener:处理单击类消息
2、OnDoubleTaplistener:处理双击类消息
Ongesturelistener:接口有6个抽象方法:
1、当收到用户触摸消息时,将消息交给GestureDetector加工
2、通过设置监听器获取GestureDetector处理后的手势
GestureDetector提供两个监听器
1、Ongesturelistener:处理单击类消息
2、OnDoubleTaplistener:处理双击类消息
Ongesturelistener:接口有6个抽象方法:
1、单击:onDown(MotionEvent e)
2、抬起:onSingleTapUp(MotionEvent e);
3、短按:onshowpress(MotionEvent e);
4、长按:onLongprss(MotionEvent e);
5、滚动:onScroll(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY);
6、滑动:onfling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY);
OnDoubleTaplistener:3个抽象方法:
双击:onDoubleTap(MotionEvent e);
双击按下和抬起各触发一次:onDoubleTapEvent(MotionEvent e)
单击确定:OnSingleTapConfirmed(MotionEvent e);
2、抬起:onSingleTapUp(MotionEvent e);
3、短按:onshowpress(MotionEvent e);
4、长按:onLongprss(MotionEvent e);
5、滚动:onScroll(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY);
6、滑动:onfling(MotionEvent e1,MotionEvent e2,float velocityX,float velocityY);
OnDoubleTaplistener:3个抽象方法:
双击:onDoubleTap(MotionEvent e);
双击按下和抬起各触发一次:onDoubleTapEvent(MotionEvent e)
单击确定:OnSingleTapConfirmed(MotionEvent e);
GestureDetector进行手势识别
一、继承SimpleOnGesturelistener;只有抽象方法(onfling())
实例GestureDetector:new GestureDetector( new GestureDetector.SimpleOnGestureListener()
二、重载感兴趣的手势
package zxx.clipboard;
import android.app.Activity;
import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.ImageView;
import android.widget.Toast;
public class MainActivity extends Activity {
GestureDetector myGestureDetector;
@SuppressWarnings("deprecation")//添加兼容
@Override
protected void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView image=(ImageView) findViewById(R.id.imag);
image.setOnTouchListener(new OnTouchListener() {
@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
myGestureDetector.onTouchEvent(arg1);
return true;
}
});
myGestureDetector=new GestureDetector( new GestureDetector.SimpleOnGestureListener(){
@Override
public boolean onFling(final MotionEvent e1, final MotionEvent e2, final float velocityX, final float velocityY) {
Log.i("*****", "jin滑動了");
if (e1.getX()-e2.getX()>20) {
Toast.makeText(getApplicationContext(), "從左往右滑", Toast.LENGTH_LONG).show();
}
if (e2.getX()-e1.getX()>20) {
Toast.makeText(getApplicationContext(), "從右往左滑", Toast.LENGTH_LONG).show();
}
return super.onFling(e1, e2, velocityX, velocityY);
}
});
}
}