android开发之为activity增加左右手势识别示例
时间:2021-09-30 12:41:50
android开发中为activity增加左右手势识别,如右滑关闭当前页面。
/*
* for左右手势
* 1.复制下面的内容到目标Activity
* 2.目标Activity的onCreate()调用initGesture()
* 3.目标Activity需implements OnTouchListener, OnGestureListener
*/
private GestureDetector mGestureDetector;
private int verticalMinDistance = 180;
private int minVelocity = 0;
private void initGesture() {
mGestureDetector = new GestureDetector((OnGestureListener) this);
}
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
if (e1.getX() - e2.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
// 切换Activity
// Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
// startActivity(intent);
//Toast.makeText(this, "向左手势", Toast.LENGTH_SHORT).show();
} else if (e2.getX() - e1.getX() > verticalMinDistance && Math.abs(velocityX) > minVelocity) {
// 切换Activity
// Intent intent = new Intent(ViewSnsActivity.this, UpdateStatusActivity.class);
// startActivity(intent);
//Toast.makeText(this, "向右手势", Toast.LENGTH_SHORT).show();
finish();
overridePendingTransition(R.anim.push_right_in, R.anim.push_right_out);
}
return false;
}
@Override
public void onLongPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onScroll(MotionEvent arg0, MotionEvent arg1, float arg2,
float arg3) {
// TODO Auto-generated method stub
return false;
}
@Override
public void onShowPress(MotionEvent arg0) {
// TODO Auto-generated method stub
}
@Override
public boolean onSingleTapUp(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean onTouch(View v, MotionEvent event) {
// TODO Auto-generated method stub
return mGestureDetector.onTouchEvent(event);
}
@Override
public boolean onDown(MotionEvent arg0) {
// TODO Auto-generated method stub
return false;
}
@Override
public boolean dispatchTouchEvent(MotionEvent ev) {
mGestureDetector.onTouchEvent(ev);
return super.dispatchTouchEvent(ev);
}
push_right_in.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="-100%p" android:toXDelta="0"
android:duration="500" />
<alpha android:fromAlpha="0.1" android:toAlpha="1.0"
android:duration="500"/>
</set>
push_right_out.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
<translate android:fromXDelta="0" android:toXDelta="100%p"
android:duration="500" />
<alpha android:fromAlpha="1.0" android:toAlpha="0.1"
android:duration="500"/>
</set>
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Java使用Apache.POI中HSSFWorkbook导出到Excel的实现方法
![](https://img.aspxhome.com/file/2023/3/63323_0s.png)
Java实现冒泡排序算法
详解Android的.aar文件生成方法以及使用技巧
![](https://img.aspxhome.com/file/2023/9/85459_0s.jpg)
Java8中forEach语句循环一个List和Map
java中Class.getMethods()和Class.getDeclaredMethods()方法的区别
Java中实现获取路径的方法汇总
深入理解Java中的接口
详解用Eclipse如何创建Web项目
![](https://img.aspxhome.com/file/2023/3/59393_0s.png)
详解Mybatis框架SQL防注入指南
![](https://img.aspxhome.com/file/2023/6/66046_0s.jpg)
SpringCloud Gateway HttpWebHandlerAdapter链路调用请求流程介绍
![](https://img.aspxhome.com/file/2023/0/61800_0s.png)
C#获取程序文件相关信息的方法
Android使用JobScheduler定期推送本地通知实例代码
Java 函数式编程梳理
![](https://img.aspxhome.com/file/2023/7/66657_0s.png)
android图片处理 让图片变成圆形
![](https://img.aspxhome.com/file/2023/6/83536_0s.jpg)
SpringBoot配置拦 截器实现过程详解
springboot中的pom文件 project报错问题
![](https://img.aspxhome.com/file/2023/9/62519_0s.png)
Java多线程 Callable、Future 和FutureTask
![](https://img.aspxhome.com/file/2023/2/78872_0s.png)
springboot整合spring-retry的实现示例
![](https://img.aspxhome.com/file/2023/0/80170_0s.png)
SpringMVC结合天气api实现天气查询
![](https://img.aspxhome.com/file/2023/1/72321_0s.png)