ViewFlipper結合手勢OnGestureListener製作的滑動切換效果

http://gundumw100.iteye.com/blog/905033


OnGestureListener和OnDoubleTapListener接口定義: 

Java代碼  收藏代碼
  1. public interface OnGestureListener {  
  2.                 // Touch down時觸發, e爲down時的MotionEvent  
  3.                 boolean onDown(MotionEvent e);  
  4.                 // 在Touch down之後一定時間(115ms)觸發,e爲down時的MotionEvent  
  5.                 void onShowPress(MotionEvent e);  
  6.                 // Touch up時觸發,e爲up時的MotionEvent  
  7.                 boolean onSingleTapUp(MotionEvent e);  
  8.                 // 滑動時觸發,e1爲down時的MotionEvent,e2爲move時的MotionEvent  
  9.                 boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX, float distanceY);  
  10.                 // 在Touch down之後一定時間(500ms)觸發,e爲down時的MotionEvent  
  11.                 void onLongPress(MotionEvent e);  
  12.                 // 滑動一段距離,up時觸發,e1爲down時的MotionEvent,e2爲up時的MotionEvent  
  13.                 boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX, float velocityY);  
  14. }  
  15.    
  16. public interface OnDoubleTapListener {  
  17.                 // 完成一次單擊,並確定沒有二擊事件後觸發(300ms),e爲down時的MotionEvent  
  18.                 boolean onSingleTapConfirmed(MotionEvent e);  
  19.                 // 第二次單擊down時觸發,e爲第一次down時的MotionEvent  
  20.                 boolean onDoubleTap(MotionEvent e);  
  21.                 // 第二次單擊down,move和up時都觸發,e爲不同時機下的MotionEvent  
  22.                 boolean onDoubleTapEvent(MotionEvent e);  
  23. }  


實例: 
Java代碼  收藏代碼
  1. import android.app.Activity;  
  2. import android.os.Bundle;  
  3. import android.view.GestureDetector;  
  4. import android.view.MotionEvent;  
  5. import android.view.View;  
  6. import android.widget.Button;  
  7. import android.widget.ViewFlipper;  
  8.   
  9. public class FlingSlideActivity extends Activity implements GestureDetector.OnGestureListener,GestureDetector.OnDoubleTapListener{  
  10.     private ViewFlipper mViewFlipper;  
  11.     private GestureDetector mGestureDetector;  
  12.     /** Called when the activity is first created. */  
  13.     @Override  
  14.     public void onCreate(Bundle savedInstanceState) {  
  15.         super.onCreate(savedInstanceState);  
  16.         setContentView(R.layout.main);  
  17.         mViewFlipper = (ViewFlipper) findViewById(R.id.flipper);  
  18.         Button button1 = (Button) findViewById(R.id.Button1);     
  19.         button1.setOnClickListener(new View.OnClickListener() {     
  20.             public void onClick(View view) {  
  21.                 mViewFlipper.showNext();     
  22.             }     
  23.         });  
  24.         Button button2 = (Button) findViewById(R.id.Button2);     
  25.         button2.setOnClickListener(new View.OnClickListener() {     
  26.             public void onClick(View view) {  
  27.                 mViewFlipper.showNext();     
  28.             }     
  29.         });        
  30.         Button button3 = (Button) findViewById(R.id.Button3);     
  31.         button3.setOnClickListener(new View.OnClickListener() {     
  32.             public void onClick(View view) {  
  33.                 mViewFlipper.showNext();     
  34.             }  
  35.         });  
  36.         mGestureDetector = new GestureDetector(this);  
  37.     }  
  38.     //別忘了覆蓋onTouchEvent方法  
  39.     @Override  
  40.     public boolean onTouchEvent(MotionEvent event) {  
  41.         return mGestureDetector.onTouchEvent(event);  
  42.     }  
  43.     //以下是OnGestureListener需要實現的方法  
  44.     public boolean onDown(MotionEvent e) {  
  45.         // TODO Auto-generated method stub  
  46.         return false;  
  47.     }  
  48.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  49.             float velocityY) {  
  50.         // TODO Auto-generated method stub  
  51.         if(e1.getX() > e2.getX()) {//向左滑動  
  52.             mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_left_in);     
  53.             mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_left_out);     
  54.            mViewFlipper.showNext();     
  55.        }else if(e1.getX() < e2.getX()) {//向右滑動  
  56.            mViewFlipper.setInAnimation(getApplicationContext(), R.anim.push_right_in);     
  57.            mViewFlipper.setOutAnimation(getApplicationContext(), R.anim.push_right_out);     
  58.            mViewFlipper.showPrevious();     
  59.        }else {     
  60.            return false;     
  61.        }     
  62.        return true;  
  63.     }  
  64.   
  65.     public void onLongPress(MotionEvent e) {  
  66.         // TODO Auto-generated method stub  
  67.           
  68.     }  
  69.   
  70.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  71.             float distanceY) {  
  72.         // TODO Auto-generated method stub  
  73.         return false;  
  74.     }  
  75.   
  76.     public void onShowPress(MotionEvent e) {  
  77.         // TODO Auto-generated method stub  
  78.           
  79.     }  
  80.   
  81.     public boolean onSingleTapUp(MotionEvent e) {  
  82.         // TODO Auto-generated method stub  
  83.         return false;  
  84.     }  
  85.     //以下是OnDoubleTapListener需要實現的方法  
  86.     public boolean onDoubleTap(MotionEvent e) {  
  87.         // TODO Auto-generated method stub  
  88.            mViewFlipper.startFlipping(); //雙擊自動切換界面  
  89.            return true;  
  90.     }  
  91.     public boolean onDoubleTapEvent(MotionEvent e) {  
  92.         // TODO Auto-generated method stub  
  93.         return false;  
  94.     }  
  95.     public boolean onSingleTapConfirmed(MotionEvent e) {  
  96.         // TODO Auto-generated method stub  
  97.         if(mViewFlipper.isFlipping()){ //單擊結束自動切換  
  98.             mViewFlipper.stopFlipping();  
  99.         }  
  100.         return false;  
  101.     }  
  102.       
  103. }  

main.xml 
Xml代碼  收藏代碼
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical"  
  4.     android:layout_width="fill_parent"  
  5.     android:layout_height="fill_parent"  
  6.     >  
  7.     <ViewFlipper android:id="@+id/flipper"    
  8.        android:layout_width="fill_parent"      
  9.        android:layout_height="fill_parent"    
  10.        android:persistentDrawingCache="animation"    
  11.        android:flipInterval="3000"  
  12.        android:inAnimation="@anim/push_left_in"    
  13.        android:outAnimation="@anim/push_left_out"  
  14.        >  
  15.        <LinearLayout     
  16.            android:orientation="vertical"    
  17.            android:layout_width="fill_parent"      
  18.            android:layout_height="fill_parent">     
  19.            <Button     
  20.               android:text="Next1"      
  21.               android:id="@+id/Button1"    
  22.               android:layout_width="fill_parent"      
  23.               android:layout_height="wrap_content">     
  24.            </Button>     
  25.            <ImageView     
  26.               android:id="@+id/image1"      
  27.               android:src="@drawable/icon"    
  28.               android:layout_width="fill_parent"    
  29.               android:layout_height="wrap_content">     
  30.            </ImageView>     
  31.        </LinearLayout>     
  32.       
  33.        <LinearLayout     
  34.            android:orientation="vertical"    
  35.            android:layout_width="fill_parent"      
  36.            android:layout_height="fill_parent">     
  37.            <Button     
  38.               android:text="Next2"      
  39.               android:id="@+id/Button2"    
  40.               android:layout_width="fill_parent"      
  41.               android:layout_height="wrap_content">     
  42.            </Button>     
  43.            <ImageView     
  44.               android:id="@+id/image2"      
  45.               android:src="@drawable/icon"    
  46.               android:layout_width="fill_parent"    
  47.               android:layout_height="wrap_content">     
  48.            </ImageView>     
  49.        </LinearLayout>     
  50.             
  51.        <LinearLayout     
  52.            android:orientation="vertical"    
  53.            android:layout_width="fill_parent"      
  54.            android:layout_height="fill_parent">     
  55.            <Button     
  56.               android:text="Next3"      
  57.               android:id="@+id/Button3"    
  58.               android:layout_width="fill_parent"      
  59.               android:layout_height="wrap_content">     
  60.            </Button>     
  61.            <ImageView     
  62.               android:id="@+id/image3"      
  63.               android:src="@drawable/icon"    
  64.               android:layout_width="fill_parent"    
  65.               android:layout_height="wrap_content">     
  66.            </ImageView>     
  67.        </LinearLayout>  
  68.     </ViewFlipper>  
  69. </LinearLayout>  

4個動畫文件: 
Xml代碼  收藏代碼
  1. push_left_in.xml  
  2. <?xml version="1.0" encoding="utf-8"?>     
  3. <set xmlns:android="http://schemas.android.com/apk/res/android">     
  4.     <translate     
  5.     android:fromXDelta="100%p"      
  6.     android:toXDelta="0"      
  7.     android:duration="500"/>     
  8.     <alpha     
  9.     android:fromAlpha="0.0"      
  10.     android:toAlpha="1.0"    
  11.     android:duration="500" />     
  12. </set>  
  13. push_left_out.xml  
  14. <?xml version="1.0" encoding="utf-8"?>     
  15. <set xmlns:android="http://schemas.android.com/apk/res/android">     
  16.     <translate     
  17.     android:fromXDelta="0"      
  18.     android:toXDelta="-100%p"      
  19.     android:duration="500"/>     
  20.     <alpha     
  21.     android:fromAlpha="1.0"      
  22.     android:toAlpha="0.0"      
  23.     android:duration="500" />     
  24. </set>  
  25. push_right_in.xml  
  26. <?xml version="1.0" encoding="utf-8"?>     
  27. <set xmlns:android="http://schemas.android.com/apk/res/android">     
  28.     <translate     
  29.     android:fromXDelta="-100%p"      
  30.     android:toXDelta="0"      
  31.     android:duration="500"/>     
  32.     <alpha     
  33.     android:fromAlpha="0.0"      
  34.     android:toAlpha="1.0"    
  35.     android:duration="500" />     
  36. </set>  
  37. push_right_out.xml  
  38. <?xml version="1.0" encoding="utf-8"?>     
  39. <set xmlns:android="http://schemas.android.com/apk/res/android">     
  40.     <translate     
  41.     android:fromXDelta="0"      
  42.     android:toXDelta="100%p"      
  43.     android:duration="500"/>     
  44.     <alpha     
  45.     android:fromAlpha="1.0"      
  46.     android:toAlpha="0.0"      
  47.     android:duration="500" />     
  48. </set>  
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章