Android获取触摸手势实现左右滑动

一、Android提供的两种手势:
①Android提供了手势检测,并为手势提供了相应的监听器
②Android允许开发者添加手势,并提供了相应的API识别用户手势

二、手势检测:手势检测器类:GestureDetector
监听器:OnGestureListener,负责对用户的手势行为提供响应
时间处理方法:boolean OnDraw(MotionEvent e):当触摸事件按下时触发该方法
boolean OnFing(MotionEvent e1,MotionEvent e2,float velocity X,float velocity Y):当用户在触摸屏上“拖过”时触发该方法。其中 velocity X,float velocity Y代表“拖过”动作在横向,纵向上的速度
abstract void onLongPress(MotionEvent e):当用户在屏幕上长按时触发该方法
onScroll(MotionEvent e,MotionEvent e2,float distanceX,float distanceY):当用户在屏幕上“滚动”时触发该方法
void onShowPress(MotionEvent e):当用户在触摸屏上按下,而还未移动和松开时触发该方法

Android收拾检测步骤:第一步:创建一个GestureDetector对象。创建该对象时必须实现一个GestureDetector.OnGestureListener监听器实例:例如:GestureDetector detector=new GestureDetector(this,this)

应用程序的Activity的TouchEvent事件绑定监听器,在事件处理中指定Activity上的TouchEvent事件交给GestureDetector处理。例如:detector.OnTouchEvent(event)

例子:①演示事件处理的方法
②通过手势实现翻页效果:
ViewFlipper组件,该组件可使用动画控制多个组件之间的切换效果
flipper.setInAnimation()设置组件进入的动画
flipper.setOutAnimation()设置组件出去的动画
flipper.showPrevious()显示上一个视图
flipper.showNext()显示下一个视图

1.MXL如下:

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout
    xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent"
    android:layout_height="match_parent" tools:context="com.example.android_gesture.MainActivity">

    <ViewFlipper
        android:id="@+id/rs_ViewFlipper"
        android:layout_width="match_parent"
        android:layout_heigh
  • 0
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值