3

我正在使用这个 Rotate3dAnimation 类来创建一个翻转硬币动画,它也是移动和缩放的。但我只能将它与一个图像视图一起使用。只需在该图像视图上使用 startAnimation() 方法。

但我想要做的是,使用硬币的两个面,这样它看起来就像一枚真正的硬币,有两个不同的面正在翻转。有人可以帮助我如何做到这一点吗?

谢谢

    package com.example.movingcoin;



    import android.view.animation.Animation;
    import android.view.animation.Transformation;
    import android.graphics.Camera;
    import android.graphics.Matrix;

    /**
     * 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.rotateX(degrees);

        camera.getMatrix(matrix);
        camera.restore();

        matrix.preTranslate(-centerX, -centerY);
        matrix.postTranslate(centerX, centerY);
    }
}
4

2 回答 2

4

几天前遇到了同样的问题,在 FlipAnimator 类中找到了解决方案,您可以在这里找到:FlipAnimatorClass

实际上,这很容易:您只需将硬币的两侧传递给 FlipAnimator。我认为,这门课很容易理解,而且它实际上在执行 g00dy 在上面的评论中建议的内容。

于 2013-06-21T08:55:00.837 回答
0

诀窍是旋转你的视图两次!从正常位置到中间位置后,更改您的视图(例如更改硬币的图像),然后将其从中间位置旋转回正常位置。

您应该在第一个动画的 AnimationListener 中的 onAnimationEnd 中进行视图的所有更改并从中间开始旋转到正常!

像这样:

firstAnimation.setAnimationListener(new Animation.AnimationListener() {
                    @Override
                    public void onAnimationStart(Animation animation) {

                    }

                    @Override
                    public void onAnimationEnd(Animation animation) {
                        findViewById(R.id.conceptsLay).setVisibility(View.GONE);
                        findViewById(R.id.factBaseLay).setVisibility(View.VISIBLE);
                        secondAnimation.startAnimation();
                    }

                    @Override
                    public void onAnimationRepeat(Animation animation) {

                    }
                });

在上面的代码中,我首先将conceptsLay 旋转到中间,它基本上是不可见的,然后让它GONE 并让其他示例查看VISIBILE 并从中间开始它的动画正常!所以用户看到的是视图被翻转了!需要说的是,首先你将它从 0 旋转到 90,然后在 secondAnimation 中,你将它旋转 -90 到 0 !

为了让它更流畅,我还添加了一些 alpha 动画!希望它会有所帮助

于 2016-01-22T20:50:14.807 回答