2016-03-07 24 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> 

使用這些我不能做對角翻轉。有沒有辦法做這個翻轉。如何在android中做對角翻轉動畫?

我試過matrix.setskew但我不知道如何在矩陣上工作。

+0

最新錯了?我試過你的代碼,它的工作 – Raghunandan

+0

它是一個簡單的翻轉,但我需要一個對角翻轉... –

回答

0

嘗試使用對象動畫器。如果您想了解關於對象動畫師的內容,請查看以下鏈接。 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文件,並使用動畫上面兩行。

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); 
     } 
    }