mobile/android/base/animation/Rotate3DAnimation.java

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial