Android ViewFlipper 手势触摸案例

来源:互联网 发布:上虞区干部网络教育 编辑:程序博客网 时间:2024/04/28 17:52

广大博友,看过后帮忙顶顶,谢谢大家!!!

转载请注明: http://blog.csdn.net/richway2010/archive/2011/06/29/6574987.aspx

博主:各位博友,网友们,大家网上好!欢迎光临本博客。欢迎多多交流,多提意见,互相学习,互相进步,我们的口号是:好好学习,天天向上。】

直接进入主题,先来看看效果:

 

从右往左触摸屏幕:

 

实现:main.xml

[xhtml] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout android:layout_height="fill_parent"   
  3.               android:layout_width="fill_parent"   
  4.               android:orientation="vertical" xmlns:android="http://schemas.android.com/apk/res/android"          
  5.               >  
  6.     <ViewFlipper android:id="@+id/flipper"  
  7.                  android:layout_height="fill_parent" android:layout_width="fill_parent">  
  8.         <include android:id="@+id/firstlayout" layout="@layout/left"/>  
  9.         <include android:id="@+id/secondlayout" layout="@layout/center"/>  
  10.         <include android:id="@+id/thirdlayout" layout="@layout/right"/>  
  11.     </ViewFlipper>  
  12. </LinearLayout>  

 

center.xml

[xhtml] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout android:gravity="center_vertical"   
  3.               android:layout_height="fill_parent"   
  4.               android:layout_width="fill_parent" xmlns:android="http://schemas.android.com/apk/res/android">  
  5.   <ImageView android:layout_height="wrap_content" android:layout_width="wrap_content" android:src="@drawable/a3">  
  6. </ImageView></LinearLayout>  

 

left.xml

[xhtml] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout android:gravity="center_vertical"  
  3.     android:layout_height="fill_parent" android:layout_width="fill_parent"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android">  
  5.     <ImageView android:layout_height="wrap_content"  
  6.         android:layout_width="wrap_content" android:src="@drawable/a2">  
  7.     </ImageView>  
  8. </LinearLayout>  

 

right.xml

[xhtml] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout android:gravity="center_vertical"  
  3.     android:layout_height="fill_parent" android:layout_width="fill_parent"  
  4.     xmlns:android="http://schemas.android.com/apk/res/android">  
  5.     <ImageView android:layout_height="wrap_content"  
  6.         android:layout_width="wrap_content" android:src="@drawable/a1"></ImageView>  
  7. </LinearLayout>  

 

主配置文件:AndroidManifest.xml

[xhtml] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <manifest xmlns:android="http://schemas.android.com/apk/res/android"  
  3.       package="org.flipper"  
  4.       android:versionCode="1"  
  5.       android:versionName="1.0">  
  6.     <uses-sdk android:minSdkVersion="8" />  
  7.   
  8.     <application android:icon="@drawable/icon" android:label="@string/app_name">  
  9.         <activity android:name=".ViewFlipperDemoActivity"  
  10.                   android:label="@string/app_name">  
  11.             <intent-filter>  
  12.                 <action android:name="android.intent.action.MAIN" />  
  13.                 <category android:name="android.intent.category.LAUNCHER" />  
  14.             </intent-filter>  
  15.         </activity>  
  16.   
  17.     </application>  
  18. </manifest>  

 

主类:ViewFlipperDemoActivity.java

[java] view plaincopyprint?
  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.view.GestureDetector.OnGestureListener;  
  7. import android.view.View.OnTouchListener;  
  8. import android.view.animation.AccelerateInterpolator;  
  9. import android.view.animation.Animation;  
  10. import android.view.animation.TranslateAnimation;  
  11. import android.widget.ViewFlipper;  
  12. public class ViewFlipperDemoActivity extends Activity implements OnGestureListener,OnTouchListener{  
  13.     private ViewFlipper mFlipper;  
  14.     GestureDetector mGestureDetector;  
  15.     private int mCurrentLayoutState;  
  16.     private static final int FLING_MIN_DISTANCE = 120;  
  17.     private static final int FLING_MIN_VELOCITY = 240;  
  18.   
  19.     @Override  
  20.     public void onCreate(Bundle savedInstanceState) {  
  21.         super.onCreate(savedInstanceState);  
  22.         setContentView(R.layout.main);  
  23.         mFlipper = (ViewFlipper) findViewById(R.id.flipper);  
  24.         //注册一个用于手势识别的类  
  25.         mGestureDetector = new GestureDetector(this);  
  26.         //给mFlipper设置一个listener  
  27.         mFlipper.setOnTouchListener(this);  
  28.         mCurrentLayoutState = 0;  
  29.         //允许长按住ViewFlipper,这样才能识别拖动等手势  
  30.         mFlipper.setLongClickable(true);  
  31.   
  32.     /** 
  33.      * 定义从右侧进入的动画效果 
  34.      * @return 
  35.      */  
  36.     protected Animation inFromRightAnimation() {  
  37.         Animation inFromRight = new TranslateAnimation(  
  38.                 Animation.RELATIVE_TO_PARENT, +1.0f,  
  39.                 Animation.RELATIVE_TO_PARENT, 0.0f,  
  40.                 Animation.RELATIVE_TO_PARENT, 0.0f,  
  41.                 Animation.RELATIVE_TO_PARENT, 0.0f);  
  42.         inFromRight.setDuration(500);  
  43.         inFromRight.setInterpolator(new AccelerateInterpolator());  
  44.         return inFromRight;  
  45.     }  
  46.   
  47.     /** 
  48.      * 定义从左侧退出的动画效果 
  49.      * @return 
  50.      */  
  51.     protected Animation outToLeftAnimation() {  
  52.         Animation outtoLeft = new TranslateAnimation(  
  53.                 Animation.RELATIVE_TO_PARENT, 0.0f,  
  54.                 Animation.RELATIVE_TO_PARENT, -1.0f,  
  55.                 Animation.RELATIVE_TO_PARENT, 0.0f,  
  56.                 Animation.RELATIVE_TO_PARENT, 0.0f);  
  57.         outtoLeft.setDuration(500);  
  58.         outtoLeft.setInterpolator(new AccelerateInterpolator());  
  59.         return outtoLeft;  
  60.     }  
  61.   
  62.     /** 
  63.      * 定义从左侧进入的动画效果 
  64.      * @return 
  65.      */  
  66.     protected Animation inFromLeftAnimation() {  
  67.         Animation inFromLeft = new TranslateAnimation(  
  68.                 Animation.RELATIVE_TO_PARENT, -1.0f,  
  69.                 Animation.RELATIVE_TO_PARENT, 0.0f,  
  70.                 Animation.RELATIVE_TO_PARENT, 0.0f,  
  71.                 Animation.RELATIVE_TO_PARENT, 0.0f);  
  72.         inFromLeft.setDuration(500);  
  73.         inFromLeft.setInterpolator(new AccelerateInterpolator());  
  74.         return inFromLeft;  
  75.     }  
  76.   
  77.     /** 
  78.      * 定义从右侧退出时的动画效果 
  79.      * @return 
  80.      */  
  81.     protected Animation outToRightAnimation() {  
  82.         Animation outtoRight = new TranslateAnimation(  
  83.                 Animation.RELATIVE_TO_PARENT, 0.0f,  
  84.                 Animation.RELATIVE_TO_PARENT, +1.0f,  
  85.                 Animation.RELATIVE_TO_PARENT, 0.0f,  
  86.                 Animation.RELATIVE_TO_PARENT, 0.0f);  
  87.         outtoRight.setDuration(500);  
  88.         outtoRight.setInterpolator(new AccelerateInterpolator());  
  89.         return outtoRight;  
  90.     }  
  91.   
  92.     public boolean onDown(MotionEvent e) {  
  93.         // TODO Auto-generated method stub  
  94.         return false;  
  95.     }  
  96.   
  97.     /* 
  98.      * 用户按下触摸屏、快速移动后松开即触发这个事件 
  99.      * e1:第1个ACTION_DOWN MotionEvent 
  100.      * e2:最后一个ACTION_MOVE MotionEvent 
  101.      * velocityX:X轴上的移动速度,像素/秒 
  102.      * velocityY:Y轴上的移动速度,像素/秒 
  103.      * 触发条件 : 
  104.      * X轴的坐标位移大于FLING_MIN_DISTANCE,且移动速度大于FLING_MIN_VELOCITY个像素/秒 
  105.      */  
  106.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  107.             float velocityY) {  
  108.         if (e1.getX() - e2.getX() > FLING_MIN_DISTANCE  
  109.                 && Math.abs(velocityX) > FLING_MIN_VELOCITY) {  
  110.             // 当像左侧滑动的时候  
  111.             //设置View进入屏幕时候使用的动画  
  112.             mFlipper.setInAnimation(inFromRightAnimation());  
  113.             //设置View退出屏幕时候使用的动画  
  114.             mFlipper.setOutAnimation(outToLeftAnimation());  
  115.             mFlipper.showNext();  
  116.         } else if (e2.getX() - e1.getX() > FLING_MIN_DISTANCE  
  117.                 && Math.abs(velocityX) > FLING_MIN_VELOCITY) {  
  118.             // 当像右侧滑动的时候  
  119.             mFlipper.setInAnimation(inFromLeftAnimation());  
  120.             mFlipper.setOutAnimation(outToRightAnimation());  
  121.             mFlipper.showPrevious();  
  122.         }  
  123.         return false;  
  124.     }  
  125.     public boolean onTouch(View v, MotionEvent event) {  
  126.         // 将触屏事件交给手势识别类去处理  
  127.         return mGestureDetector.onTouchEvent(event);  
  128.     }  
  129. }  

 

希望对你有帮助,谢谢


原创粉丝点击