android图片推拉门动画效果

  1. import android.view.animation.Animation;
  2. import android.view.animation.Transformation;
  3. import android.graphics.Camera;
  4. import android.graphics.Matrix;

  5. public class Rotate3dAnimation extends Animation {
  6.     private final float mFromDegrees;
  7.     private final float mToDegrees;
  8.     private final float mCenterX;
  9.     private final float mCenterY;
  10.     private final float mDepthZ;
  11.     private final boolean mReverse;//翻转标志
  12.     private Camera mCamera;
  13.     /**
  14.      * 初始化方法
  15.      * @param fromDegrees 开始的旋转角度
  16.      * @param toDegrees 结束的旋转角度
  17.      * @param centerX 中心X坐标
  18.      * @param centerY 中心Y坐标
  19.      * @param depthZ  Z轴
  20.      * @param reverse 翻转状态的标识 为true时表示从0—90度翻转,false表示从90-0度翻转
  21.      */
  22.     public Rotate3dAnimation(float fromDegrees, float toDegrees,
  23.             float centerX, float centerY, float depthZ, boolean reverse) {
  24.         mFromDegrees = fromDegrees;
  25.         mToDegrees = toDegrees;
  26.         mCenterX = centerX;
  27.         mCenterY = centerY;
  28.         mDepthZ = depthZ;
  29.         mReverse = reverse;
  30.     }
  31.     
  32.     public void initialize(int width, int height, int parentWidth, int parentHeight) {
  33.         super.initialize(width, height, parentWidth, parentHeight);
  34.         mCamera = new Camera();
  35.     }

  36.     protected void applyTransformation(float interpolatedTime, Transformation t) {
  37.         final float fromDegrees = mFromDegrees;
  38.         float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime);

  39.         final float centerX = mCenterX;
  40.         final float centerY = mCenterY;
  41.         final Camera camera = mCamera;

  42.         final Matrix matrix = t.getMatrix();

  43.         camera.save();
  44.         if (mReverse) {
  45.             //三轴旋转
  46.             camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
  47.         } else {
  48.             camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
  49.         }
  50.         //绕Y轴旋转
  51.         camera.rotateY(degrees);
  52.         camera.getMatrix(matrix);
  53.         camera.restore();

  54.         matrix.preTranslate(-centerX, -centerY);
  55.         matrix.postTranslate(centerX, centerY);
  56.     }
  57. }
如何使用:

  1. import android.app.Activity;
  2. import android.graphics.Color;
  3. import android.opengl.Visibility;
  4. import android.os.Bundle;
  5. import android.util.Log;
  6. import android.view.View;
  7. import android.view.ViewGroup;
  8. import android.view.animation.AccelerateInterpolator;
  9. import android.view.animation.Animation;
  10. import android.view.animation.DecelerateInterpolator;
  11. import android.view.animation.Animation.AnimationListener;
  12. import android.widget.FrameLayout;
  13. import android.widget.ImageView;
  14. import android.widget.LinearLayout;
  15. import android.widget.LinearLayout.LayoutParams;

  16. public class Rotate3dActivity extends Activity {
  17.     private FrameLayout mContainer;
  18.     private ImageView imageView1;
  19.     private ImageView imageView2;
  20.     @Override
  21.     public void onCreate(Bundle savedInstanceState) {
  22.         super.onCreate(savedInstanceState);
  23.         /**
  24.          * 测试用的view
  25.          */
  26.         mContainer = new FrameLayout(this);
  27.         mContainer.setBackgroundColor(Color.BLACK);
  28.         imageView1=new ImageView(this);
  29.         imageView1.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
  30.         imageView1.setId(20);
  31.         imageView1.setImageResource(R.drawable.back);
  32.       //第1个图片的按键监听
  33.         imageView1.setOnClickListener(new View.OnClickListener() {
  34.             
  35.             @Override
  36.             public void onClick(View view) {
  37.                 startRotation3D(0, 90, imageView1,mContainer);
  38.                 
  39.             }
  40.         });
  41.         mContainer.addView(imageView1);
  42.         
  43.         imageView2 =new ImageView(this);
  44.         imageView2.setId(30);
  45.         imageView2.setLayoutParams(new LayoutParams(LayoutParams.FILL_PARENT,LayoutParams.FILL_PARENT));
  46.         imageView2.setImageResource(R.drawable.front);
  47.         //第2个图片的按键监听
  48.         imageView2.setOnClickListener(new View.OnClickListener() {
  49.             
  50.             @Override
  51.             public void onClick(View view) {
  52.                 startRotation3D(0, 90, imageView2,mContainer);
  53.                 
  54.             }
  55.         });
  56.         mContainer.addView(imageView2);
  57.         setContentView(mContainer);

  58.       
  59.     }

  60.     
  61.     public void startRotation3D(float start,float end,final View rotateView,final View containerView){
  62.         //获得中心点
  63.         final float centerX=containerView.getWidth()/2.0f;
  64.         final float centerY =containerView.getHeight() / 2.0f;
  65.         //开始翻转,将当前界面从0°翻转到90°
  66.         Rotate3dAnimation rotation=new Rotate3dAnimation(start, end, centerX, centerY, 200.0f, true);
  67.         rotation.setDuration(500);
  68.         rotation.setInterpolator(new AccelerateInterpolator());
  69.         rotation.setAnimationListener(new AnimationListener() {
  70.             @Override
  71.             public void onAnimationEnd(Animation arg0) {

  72.                 containerView.post(new Runnable() {
  73.                     public void run() {
  74.                         //判断哪个图片需要隐藏
  75.                         Log.e("sC", String.valueOf(rotateView.getId()));
  76.                         if(rotateView.getId() == 20){
  77.                             imageView1.setVisibility(View.GONE);
  78.                             imageView2.setVisibility(View.VISIBLE);
  79.                         }else if (rotateView.getId() == 30) {
  80.                             imageView2.setVisibility(View.GONE);
  81.                             imageView1.setVisibility(View.VISIBLE);
  82.                         }
  83.                         //第二次翻转,将新页面从90°翻转到0°
  84.                         Rotate3dAnimation rotatiomAnimation = new Rotate3dAnimation(-90, 0, centerX, centerY, 200.0f, false);
  85.                         rotatiomAnimation.setDuration(500);
  86.                         rotatiomAnimation.setInterpolator(new DecelerateInterpolator());
  87.                         
  88.                         containerView.startAnimation(rotatiomAnimation);
  89.                     }
  90.                 });
  91.             
  92.             }
  93.             public void onAnimationRepeat(Animation arg0) {
  94.             }

  95.             public void onAnimationStart(Animation arg0) {
  96.             }
  97.         });
  98.         containerView.startAnimation(rotation);
  99.     }
  100. }
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值