1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/base/animation/Rotate3DAnimation.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,97 @@ 1.4 +/* 1.5 + * Copyright (C) 2007 The Android Open Source Project 1.6 + * 1.7 + * Licensed under the Apache License, Version 2.0 (the "License"); 1.8 + * you may not use this file except in compliance with the License. 1.9 + * You may obtain a copy of the License at 1.10 + * 1.11 + * http://www.apache.org/licenses/LICENSE-2.0 1.12 + * 1.13 + * Unless required by applicable law or agreed to in writing, software 1.14 + * distributed under the License is distributed on an "AS IS" BASIS, 1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 1.16 + * See the License for the specific language governing permissions and 1.17 + * limitations under the License. 1.18 + */ 1.19 + 1.20 +package org.mozilla.gecko.animation; 1.21 + 1.22 +import android.view.animation.Animation; 1.23 +import android.view.animation.Transformation; 1.24 + 1.25 +import android.graphics.Camera; 1.26 +import android.graphics.Matrix; 1.27 + 1.28 +/** 1.29 + * An animation that rotates the view on the Y axis between two specified angles. 1.30 + * This animation also adds a translation on the Z axis (depth) to improve the effect. 1.31 + */ 1.32 +public class Rotate3DAnimation extends Animation { 1.33 + private final float mFromDegrees; 1.34 + private final float mToDegrees; 1.35 + 1.36 + private final float mCenterX; 1.37 + private final float mCenterY; 1.38 + 1.39 + private final float mDepthZ; 1.40 + private final boolean mReverse; 1.41 + private Camera mCamera; 1.42 + 1.43 + private int mWidth = 1; 1.44 + private int mHeight = 1; 1.45 + 1.46 + /** 1.47 + * Creates a new 3D rotation on the Y axis. The rotation is defined by its 1.48 + * start angle and its end angle. Both angles are in degrees. The rotation 1.49 + * is performed around a center point on the 2D space, definied by a pair 1.50 + * of X and Y coordinates, called centerX and centerY. When the animation 1.51 + * starts, a translation on the Z axis (depth) is performed. The length 1.52 + * of the translation can be specified, as well as whether the translation 1.53 + * should be reversed in time. 1.54 + * 1.55 + * @param fromDegrees the start angle of the 3D rotation 1.56 + * @param toDegrees the end angle of the 3D rotation 1.57 + * @param centerX the X center of the 3D rotation 1.58 + * @param centerY the Y center of the 3D rotation 1.59 + * @param reverse true if the translation should be reversed, false otherwise 1.60 + */ 1.61 + public Rotate3DAnimation(float fromDegrees, float toDegrees, 1.62 + float centerX, float centerY, float depthZ, boolean reverse) { 1.63 + mFromDegrees = fromDegrees; 1.64 + mToDegrees = toDegrees; 1.65 + mCenterX = centerX; 1.66 + mCenterY = centerY; 1.67 + mDepthZ = depthZ; 1.68 + mReverse = reverse; 1.69 + } 1.70 + 1.71 + @Override 1.72 + public void initialize(int width, int height, int parentWidth, int parentHeight) { 1.73 + super.initialize(width, height, parentWidth, parentHeight); 1.74 + mCamera = new Camera(); 1.75 + mWidth = width; 1.76 + mHeight = height; 1.77 + } 1.78 + 1.79 + @Override 1.80 + protected void applyTransformation(float interpolatedTime, Transformation t) { 1.81 + final float fromDegrees = mFromDegrees; 1.82 + float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); 1.83 + 1.84 + final Camera camera = mCamera; 1.85 + final Matrix matrix = t.getMatrix(); 1.86 + 1.87 + camera.save(); 1.88 + if (mReverse) { 1.89 + camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime); 1.90 + } else { 1.91 + camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime)); 1.92 + } 1.93 + camera.rotateX(degrees); 1.94 + camera.getMatrix(matrix); 1.95 + camera.restore(); 1.96 + 1.97 + matrix.preTranslate(-mCenterX * mWidth, -mCenterY * mHeight); 1.98 + matrix.postTranslate(mCenterX * mWidth, mCenterY * mHeight); 1.99 + } 1.100 +}