Advertisement
Nesswit

Android FilpShowAnimation

Apr 24th, 2013
570
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 3.55 KB | None | 0 0
  1. /*
  2.  * Copyright (C) 2007 Google Inc.
  3.  
  4.  *
  5.  * Licensed under the Apache License, Version 2.0 (the "License");
  6.  * you may not use this file except in compliance with the License.
  7.  * You may obtain a copy of the License at
  8.  *
  9.  *      http://www.apache.org/licenses/LICENSE-2.0
  10.  *
  11.  * Unless required by applicable law or agreed to in writing, software
  12.  * distributed under the License is distributed on an "AS IS" BASIS,
  13.  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  14.  * See the License for the specific language governing permissions and
  15.  * limitations under the License.
  16.  */
  17.  
  18. import android.view.animation.Animation;
  19. import android.view.animation.Transformation;
  20. import android.graphics.Camera;
  21. import android.graphics.Matrix;
  22.  
  23. /**
  24.  * An animation that rotates the view on the X axis between two specified
  25.  * angles. This animation also adds a translation on the Z axis (depth) to
  26.  * improve the effect.
  27.  */
  28. public class FilpShowAnimation extends Animation {
  29.     private final float mFromDegrees;
  30.     private final float mToDegrees;
  31.     private final float mCenterX;
  32.     private final float mCenterY;
  33.     private final float mDepthZ;
  34.     private final boolean mReverse;
  35.     private Camera mCamera;
  36.  
  37.     /**
  38.      * Creates a new 3D rotation on the X axis. The rotation is defined by its
  39.      * start angle and its end angle. Both angles are in degrees. The rotation
  40.      * is performed around a center point on the 2D space, definied by a pair of
  41.      * X and Y coordinates, called centerX and centerY. When the animation
  42.      * starts, a translation on the Z axis (depth) is performed. The length of
  43.      * the translation can be specified, as well as whether the translation
  44.      * should be reversed in time.
  45.      *
  46.      * @param fromDegrees
  47.      *            the start angle of the 3D rotation
  48.      * @param toDegrees
  49.      *            the end angle of the 3D rotation
  50.      * @param centerX
  51.      *            the X center of the 3D rotation
  52.      * @param centerY
  53.      *            the Y center of the 3D rotation
  54.      * @param reverse
  55.      *            true if the translation should be reversed, false otherwise
  56.      */
  57.     public FilpShowAnimation(float fromDegrees, float toDegrees, float centerX,
  58.             float centerY, float depthZ, boolean reverse) {
  59.         mFromDegrees = fromDegrees;
  60.         mToDegrees = toDegrees;
  61.         mCenterX = centerX;
  62.         mCenterY = centerY;
  63.         mDepthZ = depthZ;
  64.         mReverse = reverse;
  65.     }
  66.  
  67.     @Override
  68.     public void initialize(int width, int height, int parentWidth,
  69.             int parentHeight) {
  70.         super.initialize(width, height, parentWidth, parentHeight);
  71.         mCamera = new Camera();
  72.     }
  73.  
  74.     @Override
  75.     protected void applyTransformation(float interpolatedTime, Transformation t) {
  76.         final float fromDegrees = mFromDegrees;
  77.         float degrees = fromDegrees
  78.                 + ((mToDegrees - fromDegrees) * interpolatedTime);
  79.  
  80.         final float centerX = mCenterX;
  81.         final float centerY = mCenterY;
  82.         final Camera camera = mCamera;
  83.  
  84.         final Matrix matrix = t.getMatrix();
  85.  
  86.         camera.save();
  87.         if (mReverse) {
  88.             camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime);
  89.         } else {
  90.             camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime));
  91.         }
  92.         camera.rotateY(degrees);
  93.         camera.getMatrix(matrix);
  94.         camera.restore();
  95.  
  96.         matrix.preTranslate(-centerX, -centerY);
  97.         matrix.postTranslate(centerX, centerY);
  98.  
  99.     }
  100. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement