GestureDetector学习之左右滑动,上下滑动屏幕切换页面

要实现滑屏等触发事件,视情况而定;

如果实现的触屏或者手势效果较多,则使用第一种方法,实现OnGestureListener 接口(参考OnGestureListener);

如果只是实现较少的效果,则考虑使用SimpleOnGestureListener(参考SimpleOnGestureListenter);主要分为以下三步:

第一种方法:

1、继承OnTouchListener , OnGestureListener ;

//继承OnTouchListener,OnGestureListener
public class MainActivity extends Activity implements OnTouchListener,OnGestureListener{

:此处使用的OnGestureListener来自包 android.view.GestureDetector.OnGestureListener;而不是 android.gesture.GestureOverlayView.OnGestureListener;

2、初始化OnGestureListener ,即注册手势监听 ;获得当前接受操作的布局id ; 注册触屏监听;

//手势监听
mGestureDetector = new GestureDetector(MainActivity.this,(OnGestureListener) this);//(OnGestureListener) 可以省略
//获得当前接受操作的布局id
RelativeLayout mActivity = (RelativeLayout)findViewById(R.id.main_activity);
//触屏监听
mActivity.setOnTouchListener(this);
mActivity.setLongClickable(true);

3、重载onFling()和onTouch();
public boolean onTouch(View v, MotionEvent event) {
	return mGestureDetector.onTouchEvent(event);
}
	
public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
        //省略
}

第二种方法如下:

1、继承OnTouchListener  ;

//继承OnTouchListener
public class MainActivity extends Activity implements OnTouchListener{

2、初始化OnGestureListener ,即注册手势监听 ;获得当前接受操作的布局id ; 注册触屏监听;

//手势监听
mGestureDetector = new GestureDetector(MainActivity.this,new MySimpleOnGestureListener());//下边定义MySimpleOnGestureListener,
                                                                                          //并使其继承SimpleOnGestureListener类
//获得当前接受操作的布局id
RelativeLayout mActivity = (RelativeLayout)findViewById(R.id.main_activity);
//触屏监听
mActivity.setOnTouchListener(this);
mActivity.setLongClickable(true);
3、定义SimpleOnGestureListener子类,并重载onTouch()和所需方法;
class MySimpleGestureListener extends SimpleOnGestureListener{
		@Override 
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
			
			//大于设定的最小滑动距离并且在水平/竖直方向速度绝对值大于设定的最小速度,则执行相应方法
			if(e1.getX()-e2.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity){
	    		Toast.makeText(MainActivity.this, "turn left", Toast.LENGTH_SHORT).show();
		    		
		    }else if(e2.getX() - e1.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity){
	    		Toast.makeText(MainActivity.this, "turn right", Toast.LENGTH_SHORT).show();
	    		
		    }else if(e1.getY()-e2.getY() > 20 && Math.abs(velocityY) > 10){
		    	Toast.makeText(MainActivity.this, "turn up", Toast.LENGTH_SHORT).show();
		    	
		    }else if(e2.getY()-e1.getY() > 20 && Math.abs(velocityY) > 10){
		    	Toast.makeText(MainActivity.this, "turn down", Toast.LENGTH_SHORT).show();
		    }
	    	
	    	return false;
	    }
		
	     @Override
	     public boolean onDown(MotionEvent e){
		Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
		return false;
	     }
	}


具体代码如下:

(第一种方法)

import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.*;

public class MainActivity extends Activity implements OnTouchListener,OnGestureListener{
        private static final String tag = "mainActivity";
	
	private GestureDetector mGestureDetector;
	private int verticalMinistance = 20;	        //水平最小识别距离
	private int minVelocity = 10;			//最小识别速度
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //手势监听
        mGestureDetector = new GestureDetector(MainActivity.this,(OnGestureListener) this);//(OnGestureListener) 可以省略
        //获得当前接受操作的布局id
        RelativeLayout mActivity = (RelativeLayout)findViewById(R.id.main_activity);
        //触屏监听
        mActivity.setOnTouchListener(this);
        mActivity.setLongClickable(true);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return mGestureDetector.onTouchEvent(event);
	}
	
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		
	     //大于设定的最小滑动距离并且在水平/竖直方向速度绝对值大于设定的最小速度,则执行相应方法
	    if(e1.getX()-e2.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity){
                //在此处实现跳转
                //Intent intent  = new Intent(MainActivity.this,nextActivity.class);
    		//startActivity(intent);
               
    		Toast.makeText(MainActivity.this, "turn left", Toast.LENGTH_SHORT).show();
	    		
	    }else if(e2.getX() - e1.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity){
    		Toast.makeText(MainActivity.this, "turn right", Toast.LENGTH_SHORT).show();
    		
	    }else if(e1.getY()-e2.getY() > 20 && Math.abs(velocityY) > 10){
	    	Toast.makeText(MainActivity.this, "turn up", Toast.LENGTH_SHORT).show();
	    	
	    }else if(e2.getY()-e1.getY() > 20 && Math.abs(velocityY) > 10){
	    	Toast.makeText(MainActivity.this, "turn down", Toast.LENGTH_SHORT).show();
	    }
	    	
	    return false;
	}

	//只要有触发就会调用次方法
	@Override
	public boolean onDown(MotionEvent e) {
		Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
		return false;
	}

	@Override
	public void onLongPress(MotionEvent e) {
		Toast.makeText(MainActivity.this, "onLongPress", Toast.LENGTH_SHORT).show();
		
	}

	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		
//		Toast.makeText(MainActivity.this, "onScroll", Toast.LENGTH_SHORT).show();
		return false;
	}


	@Override
	public void onShowPress(MotionEvent e) {
//		Toast.makeText(MainActivity.this, "onShowPress", Toast.LENGTH_SHORT).show();
	}

	@Override
	public boolean onSingleTapUp(MotionEvent e) {
//		Toast.makeText(MainActivity.this, "onsingleTapup", Toast.LENGTH_SHORT).show();
		return false;
	}	  
}

(第二种方法)
import android.app.Activity;
import android.content.Intent;

import android.os.Bundle;
import android.util.Log;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.GestureDetector.SimpleOnGestureListener;
import android.view.Menu;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.*;

public class MainActivity extends Activity implements OnTouchListener{
	private static final String tag = "mainActivity";
	
	private GestureDetector mGestureDetector;
	private int verticalMinistance = 20;	        //水平最小识别距离
	private int minVelocity = 10;			//最小识别速度
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        
        //手势监听
        mGestureDetector = new GestureDetector(MainActivity.this,new LearnGestureListener());
        //获得当前接受操作的布局id
        RelativeLayout mActivity = (RelativeLayout)findViewById(R.id.main_activity);
        //触屏监听
        mActivity.setOnTouchListener(this);
        mActivity.setLongClickable(true);
        
    }

    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        // Inflate the menu; this adds items to the action bar if it is present.
        getMenuInflater().inflate(R.menu.main, menu);
        return true;
    }

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return mGestureDetector.onTouchEvent(event);
	}
	
	class LearnGestureListener extends SimpleOnGestureListener{
		@Override 
		public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
			
			//大于设定的最小滑动距离并且在水平/竖直方向速度绝对值大于设定的最小速度,则执行相应方法
			if(e1.getX()-e2.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity){
	    		Toast.makeText(MainActivity.this, "turn left", Toast.LENGTH_SHORT).show();
		    		
		    }else if(e2.getX() - e1.getX() > verticalMinistance && Math.abs(velocityX) > minVelocity){
	    		Toast.makeText(MainActivity.this, "turn right", Toast.LENGTH_SHORT).show();
	    		
		    }else if(e1.getY()-e2.getY() > 20 && Math.abs(velocityY) > 10){
		    	Toast.makeText(MainActivity.this, "turn up", Toast.LENGTH_SHORT).show();
		    	
		    }else if(e2.getY()-e1.getY() > 20 && Math.abs(velocityY) > 10){
		    	Toast.makeText(MainActivity.this, "turn down", Toast.LENGTH_SHORT).show();
		    }
	    	
	    	return false;
	    }
		
		@Override
		public boolean onDown(MotionEvent e){
			Toast.makeText(MainActivity.this, "onDown", Toast.LENGTH_SHORT).show();
			return false;
		}
	}
 
}






评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值