如何在android中进行对角翻转动画?

时间:2016-03-07 06:03:19

标签: android matrix android-animation

<scale
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:fromXScale="1.0"
    android:fromYScale="1.0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toXScale="1.0"
    android:toYScale="0">
</scale>

<rotate
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:duration="5000"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="45">
</rotate>

使用这些我无法进行对角线翻转。有没有办法做这个翻转。

我尝试过matrix.setskew,但我不知道如何使用矩阵。

2 个答案:

答案 0 :(得分:0)

尝试使用Object animator。如果您想阅读有关对象动画师的信息,请查看以下链接。 http://developer.android.com/reference/android/animation/ObjectAnimator.html

对于水平翻转,使用下面的代码在资源中的animator目录下创建一个xml文件。

<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
    android:duration="900"
    android:propertyName="rotationY"
    android:valueFrom="0"
    android:valueTo="90"
    android:id="@+id/anim1">
</objectAnimator>

<objectAnimator
    android:duration="900"
    android:propertyName="rotationY"
    android:valueFrom="90"
    android:valueTo="0" >
</objectAnimator>

您可以使用以下代码在任何视图上调用此动画。 R.animator.flip 是您的xml的名称。

AnimatorSet mAnimation = (AnimatorSet) AnimatorInflater.loadAnimator(context, R.animator.flip);

mAnimation.addListener(new Animator.AnimatorListener() {
    @Override
    public void onAnimationStart(Animator animation) {
       //what you want to do on animation start
    }

    @Override
    public void onAnimationEnd(Animator animation) {
      //what you want to do on animation ended
    }

    @Override
    public void onAnimationCancel(Animator animation) {
        //what you want to do on animation canceled
    }

    @Override
    public void onAnimationRepeat(Animator animation) {
        //what you want to do on animation repeated
    }
});

这使您可以控制自己想要做的事情。

使用

开始动画
mAnimation.setTarget(<the view you want to target>);
mAnimation.start(); 

监听器是可选的,如果您不需要对动画进行任何控制,只是希望它执行您的xml,那么您只能创建xml文件并使用上面两行。

答案 1 :(得分:0)

我也挣扎了一段时间。通过指定4x4变换矩阵并为每个帧计算它很容易,但遗憾的是Android中只有3x3矩阵对象,它不支持3D旋转和平移。我翻转两条对角线的解决方案:

public class RightDiagonalFlipAnimation extends Animation {
        private float fromDegree;
        private float toDegree;
        private float densityDpi;

        private float width;
        private Camera camera;

        public RightDiagonalFlipAnimation(float fromDegree, float toDegree, float densityDpi) {
            this.fromDegree = fromDegree;
            this.toDegree = toDegree;
            this.densityDpi = densityDpi;
        }

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

            float cameraDistance = width * 25;
            camera.setLocation(0, 0, -cameraDistance/densityDpi);
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            float xDegrees = fromDegree + ((toDegree - fromDegree) * interpolatedTime);
            final Matrix matrix = t.getMatrix();

            camera.save();
            camera.rotateX(-xDegrees);
            camera.getMatrix(matrix);
            camera.restore();

            matrix.preTranslate(-this.width/(float) Math.sqrt(2), -this.width/(float) Math.sqrt(2));
            matrix.postTranslate(this.width/(float)Math.sqrt(2), this.width/(float)Math.sqrt(2));
            matrix.postRotate(-45);
            matrix.preRotate(45);
        }
    }

public class LeftDiagonalFlipAnimation extends Animation {
        private float fromDegree;
        private float toDegree;
        private float densityDpi;

        private float width;
        private Camera camera;

        public LeftDiagonalFlipAnimation(float fromDegree, float toDegree, float densityDpi) {
            this.fromDegree = fromDegree;
            this.toDegree = toDegree;
            this.densityDpi = densityDpi;
        }

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

            float cameraDistance = width * 25;
            camera.setLocation(0, 0, -cameraDistance/densityDpi);
        }

        @Override
        protected void applyTransformation(float interpolatedTime, Transformation t) {
            float xDegrees = fromDegree + ((toDegree - fromDegree) * interpolatedTime);
            final Matrix matrix = t.getMatrix();

            camera.save();
            camera.rotateX(xDegrees);
            camera.getMatrix(matrix);
            camera.restore();

            matrix.postRotate(45);
            matrix.preRotate(-45);
        }
    }