内容如题: 
1.SlideExample.java文件 
Java代码  
  1. package com.example;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5. import android.view.GestureDetector;  
  6. import android.view.MotionEvent;  
  7. import android.view.ViewGroup;  
  8.   
  9. public class SlideExample extends Activity {  
  10.   
  11.     public ViewGroup container1, container2;  
  12.   
  13.     private GestureDetector gestureDetector;  
  14.   
  15.     /** Called when the activity is first created. */  
  16.     @Override  
  17.     public void onCreate(Bundle savedInstanceState) {  
  18.         super.onCreate(savedInstanceState);  
  19.         setContentView(R.layout.main);  
  20.   
  21.         // 监听屏幕动作事件  
  22.         SampleGuest gestureListener = new SampleGuest(this);  
  23.         gestureDetector = new GestureDetector(gestureListener);  
  24.   
  25.         container1 = (ViewGroup) findViewById(R.id.container1);  
  26.         container2 = (ViewGroup) findViewById(R.id.container2);  
  27.     }  
  28.   
  29.     // called automatically, any screen action will Triggered it  
  30.     public boolean onTouchEvent(MotionEvent event) {  
  31.         if (gestureDetector.onTouchEvent(event))  
  32.             return true;  
  33.         else  
  34.             return false;  
  35.     }  
  36.   
  37. }  

2.SampleGuest.java文件 
Java代码  
  1. package com.example;  
  2.   
  3. import android.util.Log;  
  4. import android.view.MotionEvent;  
  5. import android.view.View;  
  6. import android.view.GestureDetector.OnGestureListener;  
  7. import android.view.animation.AnimationUtils;  
  8.   
  9. public class SampleGuest implements OnGestureListener {  
  10.   
  11.     SlideExample se;  
  12.   
  13.     private static final int SWIPE_MAX_OFF_PATH = 100;  
  14.   
  15.     private static final int SWIPE_MIN_DISTANCE = 100;  
  16.   
  17.     private static final int SWIPE_THRESHOLD_VELOCITY = 100;  
  18.   
  19.     public SampleGuest(SlideExample se) {  
  20.         this.se = se;  
  21.     }  
  22.   
  23.     // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发  
  24.     public boolean onDown(MotionEvent e) {  
  25.         Log.d("TAG""[onDown]");  
  26.         return true;  
  27.     }  
  28.   
  29.     // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN,  
  30.     // 多个ACTION_MOVE, 1个ACTION_UP触发  
  31.     // e1:第1个ACTION_DOWN MotionEvent  
  32.     // e2:最后一个ACTION_MOVE MotionEvent  
  33.     // velocityX:X轴上的移动速度,像素/秒  
  34.     // velocityY:Y轴上的移动速度,像素/秒  
  35.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  36.             float velocityY) {  
  37.         if (Math.abs(e1.getY() - e2.getY()) > SWIPE_MAX_OFF_PATH)  
  38.             return false;  
  39.   
  40.         if ((e1.getX() - e2.getX()) > SWIPE_MIN_DISTANCE  
  41.                 && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {  
  42.             se.container1.setAnimation(AnimationUtils.loadAnimation(se,  
  43.                     R.anim.push_left_out));  
  44.             se.container2.setVisibility(View.VISIBLE);  
  45.             se.container2.setAnimation(AnimationUtils.loadAnimation(se,  
  46.                     R.anim.push_right_in));  
  47.             se.container1.setVisibility(View.GONE);  
  48.   
  49.         } else if ((e2.getX() - e1.getX()) > SWIPE_MIN_DISTANCE  
  50.                 && Math.abs(velocityX) > SWIPE_THRESHOLD_VELOCITY) {  
  51.             se.container1.setVisibility(View.VISIBLE);  
  52.             se.container1.setAnimation(AnimationUtils.loadAnimation(se,  
  53.                     R.anim.push_left_in));  
  54.             se.container2.setAnimation(AnimationUtils.loadAnimation(se,  
  55.                     R.anim.push_right_out));  
  56.             se.container2.setVisibility(View.GONE);  
  57.         }  
  58.         return true;  
  59.     }  
  60.   
  61.     // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发  
  62.     public void onLongPress(MotionEvent e) {  
  63.         Log.d("TAG""[onLongPress]");  
  64.     }  
  65.   
  66.     // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发  
  67.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  68.             float distanceY) {  
  69.         Log.d("TAG""[onScroll]");  
  70.         return true;  
  71.     }  
  72.   
  73.     // 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发  
  74.     // 注意和onDown()的区别,强调的是没有松开或者拖动的状态  
  75.     public void onShowPress(MotionEvent e) {  
  76.         Log.d("TAG""[onShowPress]");  
  77.   
  78.     }  
  79.   
  80.     // 用户(轻触触摸屏后)松开,由一个1个MotionEvent ACTION_UP触发  
  81.     public boolean onSingleTapUp(MotionEvent e) {  
  82.         Log.d("TAG""[onSingleTapUp]");  
  83.         return true;  
  84.     }  
  85.   
  86. }  

3.main.xml文件 
Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:layout_width="fill_parent" android:layout_height="fill_parent"  
  4.     android:id="@+id/container">  
  5.   
  6.     <RelativeLayout android:layout_width="fill_parent"  
  7.         android:id="@+id/container1" android:layout_height="fill_parent"  
  8.         android:visibility="visible">  
  9.         <ImageView android:layout_width="fill_parent"  
  10.             android:layout_height="fill_parent" android:background="@drawable/one" />  
  11.     </RelativeLayout>  
  12.   
  13.     <RelativeLayout android:layout_width="fill_parent"  
  14.         android:background="@drawable/gray" android:id="@+id/container2"  
  15.         android:visibility="gone" android:layout_height="fill_parent">  
  16.         <ImageView android:layout_width="fill_parent"  
  17.             android:layout_height="fill_parent" android:background="@drawable/three" />  
  18.     </RelativeLayout>  
  19.   
  20. </RelativeLayout>  

4.程序所用到的anim中的几个xml文件内容 
push_left_in.xml 
Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <translate android:fromXDelta="-100%p"   
  4.                android:toXDelta="0"   
  5.                android:duration="1000"/>  
  6.     <alpha android:fromAlpha="0.0" android:toAlpha="1.0" android:duration="100" />  
  7. </set>  

push_left_out.xml 
Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <translate android:fromXDelta="0" android:toXDelta="-100%p"  
  4.         android:duration="1000" />  
  5.     <alpha android:fromAlpha="1.0" android:toAlpha="1.0"  
  6.         android:duration="400" />  
  7. </set>  

push_right_in.xml 
Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <translate android:fromXDelta="100%p"   
  4.                android:toXDelta="0"   
  5.                android:duration="1000"/>  
  6.     <alpha android:fromAlpha="1.0" android:toAlpha="1.0" android:duration="400" />  
  7. </set>  

push_right_out.xml 
Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <translate android:fromXDelta="0"   
  4.                android:toXDelta="100%p"   
  5.                android:duration="1000"/>  
  6.     <alpha android:fromAlpha="1.0" android:toAlpha="1.0" android:duration="400" />  
  7. </set>  


6.至于程序中所用到的图片,大家可以自己找几张,修改名称即可
posted on 2011-04-15 15:41  kitea  阅读(1833)  评论(2)    收藏  举报