Android 滑動效果基礎篇(三)—— Gallery仿圖像集瀏覽

Android系統自帶一個Gallery瀏覽圖片的應用,通過手指拖動時能夠非常流暢的顯示圖片,用戶交互和體驗都很好。


本示例就是通過Gallery和自定義的View,模仿實現一個仿Gallery圖像集的圖片瀏覽效果。效果圖如下:



1、基本原理

在 Activity 中實現 OnGestureListener 的接口 onFling() 手勢事件,通過自定義的 View 繪製draw() 圖片


2、Activity

Activity中,通過onTouchEvent() 註冊 myGesture.onTouchEvent(event)

  1. @Override  
  2. public boolean onTouchEvent(MotionEvent event) {  
  3.     switch (event.getAction()) {  
  4.     case MotionEvent.ACTION_UP:  
  5.         flingView.onFling(0);           // 手指擡起後,重置滑動距離offsetX = 0   
  6.         break;  
  7.     }  
  8.   
  9.     return myGesture.onTouchEvent(event);  
  10. }  
	@Override
	public boolean onTouchEvent(MotionEvent event) {
		switch (event.getAction()) {
		case MotionEvent.ACTION_UP:
			flingView.onFling(0);			// 手指擡起後,重置滑動距離offsetX = 0
			break;
		}

		return myGesture.onTouchEvent(event);
	}

接着實現接口OnGestureListener 的 onScroll()方法,給繼承自View的 FlingView 的handleScroll()成員方法傳遞滑動參數,獲取滑動的x軸距離

  1. @Override  
  2. public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {  
  3.     flingView.handleScroll(-1 * (int) distanceX);  
  4.     return true;  
  5. }  
	@Override
	public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY) {
		flingView.handleScroll(-1 * (int) distanceX);
		return true;
	}

接着實現接口OnGestureListener 的 OnFling()方法,給繼承自View的 FlingView 的onFling()成員方法傳遞滑動參數,獲取手勢的速度

  1. @Override  
  2. public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {  
  3.     flingView.onFling((int) - velocityX);  
  4.     return true;  
  5. }  
	@Override
	public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) {
		flingView.onFling((int) - velocityX);
		return true;
	}

3、FlingView

FlingView中,獲取來自Activity中的手勢速度

  1. public void onFling(int paramFloat1) {  
  2.     if (offsetX > GalleryDemoActivity.deviceScreenWidth / 5) {  
  3.         if (fBitmap != null) {  
  4.             isFling = true;  
  5.             isFlingRight = true;  
  6.         }  
  7.     } else if (offsetX < -GalleryDemoActivity.deviceScreenWidth / 5) {  
  8.         if (nBitmap != null) {  
  9.             isFling = true;  
  10.             isFlingLeft = true;  
  11.         }  
  12.     }  
  13.     // 開始動畫效果   
  14.     startAnimation(new MyAnimation());  
  15. }  
	public void onFling(int paramFloat1) {
		if (offsetX > GalleryDemoActivity.deviceScreenWidth / 5) {
			if (fBitmap != null) {
				isFling = true;
				isFlingRight = true;
			}
		} else if (offsetX < -GalleryDemoActivity.deviceScreenWidth / 5) {
			if (nBitmap != null) {
				isFling = true;
				isFlingLeft = true;
			}
		}
		// 開始動畫效果
		startAnimation(new MyAnimation());
	}

在滑動過程中,通過實現View的Draw()方法繪製圖片,注意:此時需要同時繪製當前圖片(獲取焦點)和下一張圖片(即將獲取焦點)共兩張圖片

  1. @Override  
  2. public void draw(Canvas canvas) {  
  3.     Paint paint = new Paint();  
  4.     Rect rect = new Rect();  
  5.     canvas.drawColor(Color.BLACK);  
  6.   
  7.     // 繪製當前圖片   
  8.     if (bitmap != null) {  
  9.         int left = offsetX;  
  10.         int top = offsetY;  
  11.         int right = offsetX + GalleryDemoActivity.deviceScreenWidth;  
  12.         int bottom = offsetY + GalleryDemoActivity.deviceScreenHeight;  
  13.         rect.set(left, top, right, bottom);  
  14.         canvas.drawBitmap(bitmap, null, rect, paint);  
  15.     }  
  16.       
  17.     // 繪製下一張圖片   
  18.     if (offsetX < 0) {           // 向左滑動   
  19.         if (nBitmap != null) {  
  20.             int left = GalleryDemoActivity.deviceScreenWidth + 15 + offsetX;  
  21.             int top = 0;  
  22.             int right = left + GalleryDemoActivity.deviceScreenWidth;  
  23.             int bottom = GalleryDemoActivity.deviceScreenHeight;  
  24.             rect.set(left, top, right, bottom);  
  25.             canvas.drawBitmap(nBitmap, null, rect, paint);  
  26.         }  
  27.     } else if (offsetX > 0) {        // 向右滑動   
  28.         if (fBitmap != null) {  
  29.             int left = -GalleryDemoActivity.deviceScreenWidth - 15 + offsetX;  
  30.             int top = 0;  
  31.             int right = left + GalleryDemoActivity.deviceScreenWidth;  
  32.             int bottom = GalleryDemoActivity.deviceScreenHeight;  
  33.             rect.set(left, top, right, bottom);  
  34.             canvas.drawBitmap(fBitmap, null, rect, paint);  
  35.         }  
  36.     }  
  37. }  
	@Override
	public void draw(Canvas canvas) {
		Paint paint = new Paint();
		Rect rect = new Rect();
		canvas.drawColor(Color.BLACK);

		// 繪製當前圖片
		if (bitmap != null) {
			int left = offsetX;
			int top = offsetY;
			int right = offsetX + GalleryDemoActivity.deviceScreenWidth;
			int bottom = offsetY + GalleryDemoActivity.deviceScreenHeight;
			rect.set(left, top, right, bottom);
			canvas.drawBitmap(bitmap, null, rect, paint);
		}
		
		// 繪製下一張圖片
		if (offsetX < 0) {			// 向左滑動
			if (nBitmap != null) {
				int left = GalleryDemoActivity.deviceScreenWidth + 15 + offsetX;
				int top = 0;
				int right = left + GalleryDemoActivity.deviceScreenWidth;
				int bottom = GalleryDemoActivity.deviceScreenHeight;
				rect.set(left, top, right, bottom);
				canvas.drawBitmap(nBitmap, null, rect, paint);
			}
		} else if (offsetX > 0) {		// 向右滑動
			if (fBitmap != null) {
				int left = -GalleryDemoActivity.deviceScreenWidth - 15 + offsetX;
				int top = 0;
				int right = left + GalleryDemoActivity.deviceScreenWidth;
				int bottom = GalleryDemoActivity.deviceScreenHeight;
				rect.set(left, top, right, bottom);
				canvas.drawBitmap(fBitmap, null, rect, paint);
			}
		}
	}
在滑動圖片結束後,需要做滑動動畫後的處理,重新設置當前圖片和當前圖片的上一張和下一張的狀態,爲下次滑動做準備

  1. @Override  
  2. protected void onAnimationEnd() {  
  3.     if (isFlingRight) {         // 向右滑動,position減1   
  4.         nBitmap = bitmap;  
  5.         bitmap = fBitmap;  
  6.         fBitmap = null;  
  7.         postion = postion - 1;  
  8.     } else if (isFlingLeft) {       // 向左滑動,position加1   
  9.         fBitmap = bitmap;  
  10.         bitmap = nBitmap;  
  11.         nBitmap = null;  
  12.         postion = postion + 1;  
  13.     }  
  14.       
  15.     isFlingRight = false;             
  16.     isFlingLeft = false;  
  17.     isFling = false;  
  18.     offsetX = 0;  
  19.     if (fBitmap == null && offsetX == 0) {          // 如果前一張圖片爲空(向右滑),則重置前一張圖片(position - 1)   
  20.         if (postion > 0) {  
  21.             fBitmap = getBitmap(postion - 1);  
  22.         }  
  23.   
  24.     } else if (nBitmap == null && offsetX == 0) {       // 如果後一張圖片爲空(向左滑),則重置後一張圖片(position + 1)   
  25.         if (postion < bitmaps.length - 1) {  
  26.             nBitmap = getBitmap(postion + 1);  
  27.         }  
  28.     }  
  29.     clearAnimation();             
  30. }  
	@Override
	protected void onAnimationEnd() {
		if (isFlingRight) {			// 向右滑動,position減1
			nBitmap = bitmap;
			bitmap = fBitmap;
			fBitmap = null;
			postion = postion - 1;
		} else if (isFlingLeft) {		// 向左滑動,position加1
			fBitmap = bitmap;
			bitmap = nBitmap;
			nBitmap = null;
			postion = postion + 1;
		}
		
		isFlingRight = false;			
		isFlingLeft = false;
		isFling = false;
		offsetX = 0;
		if (fBitmap == null && offsetX == 0) {			// 如果前一張圖片爲空(向右滑),則重置前一張圖片(position - 1)
			if (postion > 0) {
				fBitmap = getBitmap(postion - 1);
			}

		} else if (nBitmap == null && offsetX == 0) {		// 如果後一張圖片爲空(向左滑),則重置後一張圖片(position + 1)
			if (postion < bitmaps.length - 1) {
				nBitmap = getBitmap(postion + 1);
			}
		}
		clearAnimation();			
	}


4、手勢座標介紹

本示例中,用到了OnGestureListener接口的onScroll()和OnFling()方法,涉及到了Android系統座標及觸摸MotionEvent e1和e2、速度velocityX、velocityY等值

Android屏幕座標系如下圖(左)


(1)MotionEvent中 e1是手指第一次按上屏幕的起點,e2是擡起手指離開屏幕的終點,根據上圖Android屏幕座標系可知:

手指向滑動,終點(e2)在起點(e1)的右側,有e2.getX() - e1.getX() 大於0
手指向左滑動,終點(e2)在起點(e1)的左側,有e2.getX() - e1.getX() 小於0
手指向滑動,終點(e2)在起點(e1)的下側,有e2.getY() - e1.getY() 大於0
手指向上滑動,終點(e2)在起點(e1)的上側,有e2.getY() - e1.getY() 小於0


(2)onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY)

distanceX,是前後兩次call的X距離,不是e2與e1的水平距離

distanceX,是前後兩次call的Y距離,不是e2與e1的垂直距離

具體數值的方向,請詳見上圖(中)


(3)onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY) 

velocityX,是X軸的每秒速度

velocityY,是Y軸的每秒速度

具體數值的方向,請詳見上圖(右)

仔細觀察可以發現:velocityX、velocityY的方向與distanceX、distanceY方向正好相反


更多OnGestureListener接口函數介紹,請見上一篇博客 Android 滑動效果入門篇(一)—— ViewFlipper



示例源碼下載

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