Android手势滑动GestureDetector和OnGestureListener(一)

main.xml如下:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
   >

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_centerHorizontal="true"
        android:text="分别触摸空白区域和Button以测试Gesture"
        android:gravity="center"
        android:textSize="25sp"
     />
    
    <Button
        android:id="@+id/button"
        android:layout_width="150dip"
        android:layout_height="150dip"
        android:text="Button"
        android:layout_centerInParent="true"
        />

</RelativeLayout>


 

MainActivity如下:

package cn.com.testgesture1;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.View;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View.OnTouchListener;
import android.widget.Button;
import android.app.Activity;

/**
 * Demo描述:
 * Activity和控件的手势操作GestureDetector
 * 
 * Activity手势操作步骤:
 * 1 覆写Activity的onTouchEvent方法
 * 2 在onTouchEvent中将Touch事件交给GestureDetector处理即:
 *   return mGestureDetector.onTouchEvent(event);
 * 
 * 控件的手势操作步骤:
 * 1 为控件设置setOnTouchListener
 * 2 在该监听的onTouch方法中将事件交给GestureDetector处理即:
 *   return mGestureDetector.onTouchEvent(event);
 *   
 * 注意:
 * onScroll()和onFling()方法的区别:
 * 1 onScroll() happens after the user puts his finger down on
 * the screen and slides his finger across the screen without lifting it.
 * onFling() happens if the user scrolls and then lifts his finger.
 * A fling is triggered only if the motion was fast enough.
 * 2 onScroll()方法中两个参数MotionEvent e1, MotionEvent e2分别对应:
 *  ACTION_DOWN(0)和ACTION_MOVE(2)
 *  但是:onFling()方法中两个参数MotionEvent e1, MotionEvent e2分别对应:
 *  ACTION_DOWN(0)和ACTION_UP(1)
 */
public class MainActivity extends Activity {
	private GestureDetector mGestureDetector;
    private Button mButton;
	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.main);
		init();
	}

	private void init() {
		mGestureDetector = new GestureDetector(MainActivity.this,new GestureListenerImpl());
		mButton=(Button) findViewById(R.id.button);
		mButton.setOnTouchListener(new OnTouchListener() {			
			@Override
			public boolean onTouch(View arg0, MotionEvent event) {
				return mGestureDetector.onTouchEvent(event);
			}
		});
	}

	//覆写Activity的onTouchEvent方法
	//将Touch事件交给GestureDetector处理
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		return mGestureDetector.onTouchEvent(event);
	}

	//以下为OnGestureListener的实现
	private class GestureListenerImpl implements OnGestureListener {
		//触摸屏幕时均会调用该方法
		@Override
		public boolean onDown(MotionEvent e) {
			System.out.println("---> 手势中的 onDown 方法");
			return false;
		}

		//手指在屏幕上拖动时会调用该方法
		@Override
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,float velocityY) {
			System.out.println("---> 手势中的 onFling 方法");
			System.out.println("e1.getAction()="+e1.getAction()+",e2.getAction()="+e2.getAction());
			System.out.println("velocityX="+velocityX+",velocityY="+velocityY);
			return false;
		}

		//长按触摸屏幕时均会调用该方法
		@Override
		public void onLongPress(MotionEvent e) {
			System.out.println("---> 手势中的 onLongPress 方法");
		}

		//手指在屏幕上滚动时会调用该方法
		@Override
		public boolean onScroll(MotionEvent e1, MotionEvent e2,float distanceX, float distanceY) {
			System.out.println("---> 手势中的 onScroll 方法");
			System.out.println("e1.getAction()="+e1.getAction()+",e2.getAction()="+e2.getAction());
			System.out.println("distanceX="+distanceX+",distanceY="+distanceY);
			return false;
		}

		//在触摸屏上按下,且未移动和松开时调用该方法
		@Override
		public void onShowPress(MotionEvent e) {
			System.out.println("---> 手势中的 onShowPress 方法");
		}

		//轻击屏幕时调用该方法
		@Override
		public boolean onSingleTapUp(MotionEvent e) {
			System.out.println("---> 手势中的 onSingleTapUp 方法");
			return false;
		}
	}
}


 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

谷哥的小弟

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值