Android系统API Demos提供了伪3D旋转的例子,实现原理都是通过Camera API来进行处理.
先附上源码:
动画类Rotate3dAnimation:
/** * An animation that rotates the view on the Y axis between two specified angles. * This animation also adds a translation on the Z axis (depth) to improve the effect. */ public class Rotate3dAnimation extends Animation { private final float mFromDegrees; private final float mToDegrees; private final float mCenterX; private final float mCenterY; private final float mDepthZ; private final boolean mReverse; private Camera mCamera; /** * Creates a new 3D rotation on the Y axis. The rotation is defined by its * start angle and its end angle. Both angles are in degrees. The rotation * is performed around a center point on the 2D space, definied by a pair * of X and Y coordinates, called centerX and centerY. When the animation * starts, a translation on the Z axis (depth) is performed. The length * of the translation can be specified, as well as whether the translation * should be reversed in time. * * @param fromDegrees the start angle of the 3D rotation * @param toDegrees the end angle of the 3D rotation * @param centerX the X center of the 3D rotation * @param centerY the Y center of the 3D rotation * @param reverse true if the translation should be reversed, false otherwise */ public Rotate3dAnimation(float fromDegrees, float toDegrees, float centerX, float centerY, float depthZ, boolean reverse) { mFromDegrees = fromDegrees; mToDegrees = toDegrees; mCenterX = centerX; mCenterY = centerY; mDepthZ = depthZ; mReverse = reverse; } @Override public void initialize(int width, int height, int parentWidth, int parentHeight) { super.initialize(width, height, parentWidth, parentHeight); mCamera = new Camera(); } @Override protected void applyTransformation(float interpolatedTime, Transformation t) { final float fromDegrees = mFromDegrees; float degrees = fromDegrees + ((mToDegrees - fromDegrees) * interpolatedTime); final float centerX = mCenterX; final float centerY = mCenterY; final Camera camera = mCamera; final Matrix matrix = t.getMatrix(); camera.save(); if (mReverse) { camera.translate(0.0f, 0.0f, mDepthZ * interpolatedTime); } else { camera.translate(0.0f, 0.0f, mDepthZ * (1.0f - interpolatedTime)); } camera.rotateY(degrees); camera.getMatrix(matrix); camera.restore(); matrix.preTranslate(-centerX, -centerY); matrix.postTranslate(centerX, centerY); } /** * 自己加上去的工具方法 */ public void matrix() { final Camera camera = mCamera; camera.translate(0.0f, 0.0f,2*mCenterY); camera.rotateY(180f); camera.translate(0.0f, 0.0f,2*mCenterY); } }
然后是调用处理部分:
/** * Setup a new 3D rotation on the container view. * * @param position the item that was clicked to show a picture, or -1 to show the list * @param start the start angle at which the rotation must begin * @param end the end angle of the rotation */ private void applyRotation(int position, float start, float end) { // Find the center of the container final float centerX = mContainer.getWidth() / 2.0f; final float centerY = mContainer.getHeight() / 2.0f; // Create a new 3D rotation with the supplied parameter // The animation listener is used to trigger the next animation final Rotate3dAnimation rotation = new Rotate3dAnimation(start, end, centerX, centerY, 310.0f, true); rotation.setDuration(500); rotation.setFillAfter(true); rotation.setInterpolator(new AccelerateInterpolator()); rotation.setAnimationListener(new DisplayNextView(position,rotation)); mContainer.startAnimation(rotation); } /** * This class listens for the end of the first half of the animation. * It then posts a new action that effectively swaps the views when the container * is rotated 90 degrees and thus invisible. */ private final class DisplayNextView implements Animation.AnimationListener { private final int mPosition; private Rotate3dAnimation mAnimation; private DisplayNextView(int position,Rotate3dAnimation rotate3dAnimation) { mPosition = position; mAnimation = rotate3dAnimation; } public void onAnimationStart(Animation animation) { } public void onAnimationEnd(Animation animation) { mAnimation.matrix(); mContainer.post(new SwapViews(mPosition)); } public void onAnimationRepeat(Animation animation) { } } /** * This class is responsible for swapping the views and start the second * half of the animation. */ private final class SwapViews implements Runnable { private final int mPosition; public SwapViews(int position) { mPosition = position; } public void run() { final float centerX = mContainer.getWidth() / 2.0f; final float centerY = mContainer.getHeight() / 2.0f; if (!isShowDay) { day_listview.setVisibility(View.GONE); month_listview.setVisibility(View.VISIBLE); month_listview.requestFocus(); } else { day_listview.setVisibility(View.VISIBLE); month_listview.setVisibility(View.GONE); day_listview.requestFocus(); } final Rotate3dAnimation rotation = new Rotate3dAnimation(270, 360, centerX, centerY, 310.0f, false); rotation.setDuration(1000); rotation.setFillAfter(true); rotation.setInterpolator(new DecelerateInterpolator()); mContainer.startAnimation(rotation); } }
可以清楚得看到 它调用的时候都是直接传入起始的角度和结束的角度以及旋转的中心坐标。
这种写法如果翻转的是一张图片是没有任何问题的,假设翻转的两个都是View的话,你就会发现翻转后的View有问题了。
所以我们必须在mContainer翻转到90的时候,对它进行一个翻转180操作。
这样就是OK....
public void matrix() { final Camera camera = mCamera; camera.translate(0.0f, 0.0f,2*mCenterY); camera.rotateY(180f); camera.translate(0.0f, 0.0f,2*mCenterY); }