1

我在 xml 中定义了一个按钮,我正在向它添加动画(Card Flip 3D),它在 google nexus one 中工作正常,但在 Kindle 上无法正常工作。

你可以在这里看到一个演示演示卡翻转动画这是我试图在 ANDROID 中实现的。

这是我的代码:

public class CardFlipAnimation extends Animation {
    private Camera camera;

    private View fromView;

    private View toView;

    private float centerX;

    private float centerY;

    private boolean forward = true;

    private boolean visibilitySwapped;

    /**
     * Creates a 3D flip animation between two views. If forward is true, its
     * assumed that view1 is "visible" and view2 is "gone" before the animation
     * starts. At the end of the animation, view1 will be "gone" and view2 will
     * be "visible". If forward is false, the reverse is assumed.
     * 
     * @param fromView First view in the transition.
     * @param toView Second view in the transition.
     * @param centerX The center of the views in the x-axis.
     * @param centerY The center of the views in the y-axis.
     * @param forward The direction of the animation.
     */
    public CardFlipAnimation(View fromView, View toView, int centerX, int centerY) {
        this.fromView = fromView;
        this.toView = toView;
        this.centerX = centerX;
        this.centerY = centerY;

        setDuration(500);
        setFillAfter(true);
        setInterpolator(new AccelerateDecelerateInterpolator());
    }

    public void reverse() {
        forward = false;
        View temp = toView;
        toView = fromView;
        fromView = temp;
    }

    @Override
    public void initialize(int width, int height, int parentWidth, int parentHeight) {
        super.initialize(width, height, parentWidth, parentHeight);
        camera = new Camera();
    }

    @Override
    protected void applyTransformation(float interpolatedTime, Transformation t) {
        // Angle around the y-axis of the rotation at the given time. It is
        // calculated both in radians and in the equivalent degrees.
        final double radians = Math.PI * interpolatedTime;
        float degrees = (float) (180.0 * radians / Math.PI);

        // Once we reach the midpoint in the animation, we need to hide the
        // source view and show the destination view. We also need to change
        // the angle by 180 degrees so that the destination does not come in
        // flipped around. This is the main problem with SDK sample, it does not
        // do this.
        if (interpolatedTime >= 0.5f) {
            degrees -= 180.f;

            if (!visibilitySwapped) {
                fromView.setVisibility(View.GONE);
                toView.setVisibility(View.VISIBLE);

                visibilitySwapped = true;
            }
        }

        if (forward)
            degrees = -degrees;

        final Matrix matrix = t.getMatrix();

        camera.save();
        camera.translate(0.0f, 0.0f, (float) (150.0 * Math.sin(radians)));
        camera.rotateY(degrees);
        camera.getMatrix(matrix);
        camera.restore();

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

在我的活动中,我这样做:

CardFlipAnimation animator = new CardFlipAnimation(button, button,
                    button.getWidth() / 2, Button.getHeight() / 2);
            if (button.getVisibility() == View.GONE) {
                animator.reverse();
            }
            layout.startAnimation(animator); // Relative layout with the button

代码有什么问题,它在某些设备中运行良好,在某些设备中无法运行。它给了我 3d 效果吗?

编辑

这个链接解决了我的问题 http://www.inter-fuser.com/2009/08/android-animations-3d-flip.html

感谢罗宾汉

4

0 回答 0