Android左右滑動切換背景(GestureDetector)

本文利用OnGestureListener,  OnTouchListener這兩個接口來實現一個左右切換背景圖片的demo

其中OnTouchListener用於獲取用戶對手機的操作,如觸摸、拖動等


實現方式比較簡單,但是實現效果也比較粗糙

 

學習內容來源於以下兩篇文章:

http://wayfarer.iteye.com/blog/460284

http://blog.csdn.net/zqiang_55/article/details/8009127



import android.app.Activity;
import android.os.Bundle;
import android.view.GestureDetector;
import android.view.GestureDetector.OnGestureListener;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnTouchListener;
import android.widget.LinearLayout;

public class MainActivity extends Activity implements OnGestureListener,OnTouchListener{

	int flag =0;
	LinearLayout layout = null;
	GestureDetector gd =  null;
	
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);
		setContentView(R.layout.activity_main);
		
		gd = new GestureDetector(this);
		layout = (LinearLayout) findViewById(R.id.LinearLayout1);
		
		layout.setBackgroundResource(R.drawable.page1);
		layout.setOnTouchListener(this);
		layout.setLongClickable(true);
		
	}

	 /*  
	 * 在onTouch()方法中,我們調用GestureDetector的onTouchEvent()方法,將捕捉到的MotionEvent交給GestureDetector  
	 * 來分析是否有合適的callback函數來處理用戶的手勢  
	 */  
	public boolean onTouch(View v, MotionEvent event) {
		// TODO Auto-generated method stub
		return this.gd.onTouchEvent(event);
	}
	
        // 用戶按下觸摸屏、快速移動後鬆開,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE, 1個ACTION_UP觸發 
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,
			float velocityY) {
		
	    // 參數解釋:   
	    // e1:第1個ACTION_DOWN MotionEvent   
	    // e2:最後一個ACTION_MOVE MotionEvent   
	    // velocityX:X軸上的移動速度,像素/秒   
	    // velocityY:Y軸上的移動速度,像素/秒   
	  
	    // 觸發條件 :   
	    // X軸的座標位移大於FLING_MIN_DISTANCE,且移動速度大於FLING_MIN_VELOCITY個像素/秒 
		
		if (e1.getX() - e2.getX() > 100) {
			if(flag == 0){
				layout.setBackgroundResource(R.drawable.page1);
				flag++;
				return true;
			}
			if(flag == 1){
				layout.setBackgroundResource(R.drawable.page2);
				flag++;
				return true;
			}
			if(flag == 2){
				layout.setBackgroundResource(R.drawable.page3);
				flag = 0;
				return true;
			}
		}else if (e1.getX() - e2.getX() < -100) {
			if(flag == 0){
				layout.setBackgroundResource(R.drawable.page1);
				flag = 2;
				return true;
			}
			if(flag == 1){
				layout.setBackgroundResource(R.drawable.page2);
				flag--;
				return true;
			}
			if(flag == 2){
				layout.setBackgroundResource(R.drawable.page3);
				flag--;
				return true;
			}
		}
		return false;
	}

	// 用戶輕觸觸摸屏,由1個MotionEvent ACTION_DOWN觸發 
	public boolean onDown(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}


	// 用戶長按觸摸屏,由多個MotionEvent ACTION_DOWN觸發 
	public void onLongPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	// 用戶按下觸摸屏,並拖動,由1個MotionEvent ACTION_DOWN, 多個ACTION_MOVE觸發 
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
			float distanceY) {
		// TODO Auto-generated method stub
		return false;
	}

        /*  
        * 用戶輕觸觸摸屏,尚未鬆開或拖動,由一個1個MotionEvent ACTION_DOWN觸發  
        * 注意和onDown()的區別,強調的是沒有鬆開或者拖動的狀態  
        */  
	public void onShowPress(MotionEvent e) {
		// TODO Auto-generated method stub
		
	}

	// 用戶(輕觸觸摸屏後)鬆開,由一個1個MotionEvent ACTION_UP觸發 
	public boolean onSingleTapUp(MotionEvent e) {
		// TODO Auto-generated method stub
		return false;
	}


}


發佈了31 篇原創文章 · 獲贊 34 · 訪問量 5萬+
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章