这是个图片浏览的例子; 
 
刚在群里一个家伙给提供的思路: 
1.Activity类Rotate.java文件 
Java代码  收藏代码
  1. package cn.com;  
  2.   
  3. import android.app.Activity;  
  4. import android.os.Bundle;  
  5.   
  6. import android.view.GestureDetector;  
  7. import android.view.MotionEvent;  
  8. import android.widget.ImageView;  
  9.   
  10. public class Rotate extends Activity {  
  11.     GestureDetector gesture;  
  12.   
  13.     int mCenterX = 160;  
  14.     int mCenterY = 0;  
  15.   
  16.     ImageView mImageView1;  
  17.     ImageView mImageView2;  
  18.   
  19.     /** Called when the activity is first created. */  
  20.     @Override  
  21.     public void onCreate(Bundle savedInstanceState) {  
  22.         super.onCreate(savedInstanceState);  
  23.         setContentView(R.layout.main);  
  24.   
  25.         FlingGuest sg = new FlingGuest(this);  
  26.         gesture = new GestureDetector(sg);  
  27.   
  28.         mImageView1 = (ImageView) findViewById(R.id.image1);  
  29.         mImageView2 = (ImageView) findViewById(R.id.image2);  
  30.     }  
  31.   
  32.     public void leftMoveHandle() {  
  33.         Rotate3d leftAnimation = new Rotate3d(0, -9000, mCenterX, mCenterY);  
  34.         Rotate3d rightAnimation = new Rotate3d(9000.0f, 0.0f, mCenterX,  
  35.                 mCenterY);  
  36.   
  37.         leftAnimation.setFillAfter(true);  
  38.         leftAnimation.setDuration(1000);  
  39.         rightAnimation.setFillAfter(true);  
  40.         rightAnimation.setDuration(1000);  
  41.   
  42.         mImageView1.startAnimation(leftAnimation);  
  43.         mImageView2.startAnimation(rightAnimation);  
  44.     }  
  45.   
  46.     public void rightMoveHandle() {  
  47.         Rotate3d leftAnimation = new Rotate3d(09000, mCenterX, mCenterY);  
  48.         Rotate3d rightAnimation = new Rotate3d(-9000.0f, 0.0f, mCenterX,  
  49.                 mCenterY);  
  50.   
  51.         leftAnimation.setFillAfter(true);  
  52.         leftAnimation.setDuration(1000);  
  53.         rightAnimation.setFillAfter(true);  
  54.         rightAnimation.setDuration(1000);  
  55.   
  56.         mImageView1.startAnimation(rightAnimation);  
  57.         mImageView2.startAnimation(leftAnimation);  
  58.     }  
  59.   
  60.     // called automatically, any screen action will Triggered it  
  61.     public boolean onTouchEvent(MotionEvent me) {  
  62.         return gesture.onTouchEvent(me);  
  63.     }  
  64. }  


2.手势监听类 
Java代码  收藏代码
  1. package cn.com;  
  2.   
  3. import android.app.Activity;  
  4. import android.util.Log;  
  5. import android.view.MotionEvent;  
  6. import android.view.GestureDetector.OnGestureListener;  
  7.   
  8. public class FlingGuest implements OnGestureListener {  
  9.     Activity activity;  
  10.   
  11.     int VALUE_DISTANCE = 100;  
  12.     int VALUE_SPEED = 20;  
  13.   
  14.     public FlingGuest(Activity a) {  
  15.         activity = a;  
  16.     }  
  17.   
  18.     // 用户轻触触摸屏,由1个MotionEvent ACTION_DOWN触发  
  19.     public boolean onDown(MotionEvent e) {  
  20.         Log.d("TAG""[+++++++++++][onDown]");  
  21.         return true;  
  22.     }  
  23.   
  24.     // e1, the begin of ACTION_DOWN MotionEvent  
  25.     // e2, the end of ACTION_DOWN MotionEvent  
  26.     // velocityX, the motion speed in X  
  27.     // velocityY:the motion speed in y  
  28.     // 用户按下触摸屏、快速移动后松开,由1个MotionEvent ACTION_DOWN,  
  29.     // 多个ACTION_MOVE, 1个ACTION_UP触发  
  30.     // e1:第1个ACTION_DOWN MotionEvent  
  31.     // e2:最后一个ACTION_MOVE MotionEvent  
  32.     // velocityX:X轴上的移动速度,像素/秒  
  33.     // velocityY:Y轴上的移动速度,像素/秒  
  34.     // 触发条件 :  
  35.     // X轴的坐标位移大于VALUE_DISTANCE,且移动速度大于VALUE_SPEED个像素/秒  
  36.     public boolean onFling(MotionEvent e1, MotionEvent e2, float velocityX,  
  37.             float velocityY) {  
  38.         if ((e1.getX() - e2.getX() > VALUE_DISTANCE)  
  39.                 && Math.abs(velocityX) > VALUE_SPEED) {  
  40.             Log.d("TAG""[+++++++++++][onFling][Fling left]");  
  41.             ((Rotate)activity).leftMoveHandle();  
  42.               
  43.         } else if ((e2.getX() - e1.getX() > VALUE_DISTANCE)  
  44.                 && Math.abs(velocityX) > VALUE_SPEED) {  
  45.             Log.d("TAG""[+++++++++++][onDown][Fling right]");  
  46.             ((Rotate)activity).rightMoveHandle();  
  47.         }  
  48.         return true;  
  49.     }  
  50.   
  51.     // 用户长按触摸屏,由多个MotionEvent ACTION_DOWN触发  
  52.     public void onLongPress(MotionEvent e) {  
  53.         Log.d("TAG""[+++++++++++][onLongPress]");  
  54.     }  
  55.   
  56.     // 用户按下触摸屏,并拖动,由1个MotionEvent ACTION_DOWN, 多个ACTION_MOVE触发  
  57.     public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,  
  58.             float distanceY) {  
  59.         Log.d("TAG""[+++++++++++][onScroll]");  
  60.         return true;  
  61.     }  
  62.   
  63.     // 用户轻触触摸屏,尚未松开或拖动,由一个1个MotionEvent ACTION_DOWN触发  
  64.     // 注意和onDown()的区别,强调的是没有松开或者拖动的状态  
  65.     public void onShowPress(MotionEvent e) {  
  66.         Log.d("TAG""[+++++++++++][onShowPress]");  
  67.     }  
  68.   
  69.     // 用户(轻触触摸屏后)松开,由一个MotionEvent ACTION_UP触发  
  70.     public boolean onSingleTapUp(MotionEvent e) {  
  71.         Log.d("TAG""[+++++++++++][onSingleTapUp]");  
  72.         return true;  
  73.     }  
  74.   
  75. }  


3.Rotate3d.java动画辅助类 
Java代码  收藏代码
  1. package cn.com;  
  2.   
  3. import android.graphics.Camera;  
  4. import android.graphics.Matrix;  
  5. import android.view.animation.Animation;  
  6. import android.view.animation.Transformation;  
  7.   
  8. public class Rotate3d extends Animation {  
  9.     private float mFromDegree;  
  10.     private float mToDegree;  
  11.     private float mCenterX;  
  12.     private float mCenterY;  
  13.     private float mLeft;  
  14.     private float mTop;  
  15.     private Camera mCamera;  
  16.     private static final String TAG = "Rotate3d";  
  17.   
  18.     public Rotate3d(float fromDegree, float toDegree, float left, float top,  
  19.             float centerX, float centerY) {  
  20.         this.mFromDegree = fromDegree;  
  21.         this.mToDegree = toDegree;  
  22.         this.mLeft = left;  
  23.         this.mTop = top;  
  24.         this.mCenterX = centerX;  
  25.         this.mCenterY = centerY;  
  26.   
  27.     }  
  28.   
  29.     @Override  
  30.     public void initialize(int width, int height, int parentWidth,  
  31.             int parentHeight) {  
  32.         super.initialize(width, height, parentWidth, parentHeight);  
  33.         mCamera = new Camera();  
  34.     }  
  35.   
  36.     @Override  
  37.     protected void applyTransformation(float interpolatedTime, Transformation t) {  
  38.         final float FromDegree = mFromDegree;  
  39.         float degrees = FromDegree + (mToDegree - mFromDegree)  
  40.                 * interpolatedTime;  
  41.         final float centerX = mCenterX;  
  42.         final float centerY = mCenterY;  
  43.         final Matrix matrix = t.getMatrix();  
  44.   
  45.         if (degrees <= -76.0f) {  
  46.             degrees = -90.0f;  
  47.             mCamera.save();  
  48.             mCamera.rotateY(degrees);  
  49.             mCamera.getMatrix(matrix);  
  50.             mCamera.restore();  
  51.         } else if (degrees >= 76.0f) {  
  52.             degrees = 90.0f;  
  53.             mCamera.save();  
  54.             mCamera.rotateY(degrees);  
  55.             mCamera.getMatrix(matrix);  
  56.             mCamera.restore();  
  57.         } else {  
  58.             mCamera.save();  
  59.             //  
  60.             mCamera.translate(00, centerX);  
  61.             mCamera.rotateY(degrees);  
  62.             mCamera.translate(00, -centerX);  
  63.             mCamera.getMatrix(matrix);  
  64.             mCamera.restore();  
  65.         }  
  66.   
  67.         matrix.preTranslate(-centerX, -centerY);  
  68.         matrix.postTranslate(centerX, centerY);  
  69.     }  
  70. }  


4.main.xml 
Java代码  
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:orientation="vertical" android:layout_width="fill_parent"  
  4.     android:layout_height="wrap_content">  
  5.     <ImageView android:id="@+id/image2" android:layout_width="fill_parent"  
  6.         android:layout_height="fill_parent" android:background="@drawable/two" />  
  7.     <ImageView android:id="@+id/image1" android:layout_width="fill_parent"  
  8.         android:layout_height="fill_parent" android:background="@drawable/one" />  
  9. </RelativeLayout>  

posted on 2011-04-18 08:51  kitea  阅读(322)  评论(0)    收藏  举报