2016-03-07 2 views
0
<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, но я не знаю, как работать с матрицей.

+0

что-то не так? Я пробовал ваш код, и он работает – Raghunandan

+0

Его простой флип, но мне нужен диагональный флип ... –

ответ

0

Попробуйте объект аниматора. Если вы хотите прочитать об объектном аниматоре, посмотрите на следующую ссылку. http://developer.android.com/reference/android/animation/ObjectAnimator.html

И для горизонтального флип создайте файл 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 и использовать над двумя строками.

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); 
     } 
    } 
Смежные вопросы