2014-12-25 170 views
4

我想要水平旋轉ImageView的動畫(圍繞X軸)。我做了順時針和逆時針動畫。這裏是我用過的代碼...垂直旋轉ImageView

<set xmlns:android="http://schemas.android.com/apk/res/android"> 
    <rotate 
     android:fromDegrees="0" 
     android:toDegrees="180" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:duration="300" 
     android:fillAfter="true" 
     android:fillEnabled="true" /> 
</set> 

我也想連續旋轉它,它應該在每次翻轉後暫停一會兒。

回答

6

嘗試使用以下與相應的Java邏輯XML代碼,實現與AnimationListener

您的活動

animation1.xml

<?xml version="1.0" encoding="utf-8"?> 
    <scale xmlns:android="http://schemas.android.com/apk/res/android" 
    android:duration="250" 
    android:fromXScale="1.0" 
    android:fromYScale="1.0" 
    android:pivotX="50%" 
    android:pivotY="50%" 
    android:toXScale="0.0" 
    android:toYScale="1.0" /> 

animation2.xml

<?xml version="1.0" encoding="utf-8"?> 
    <scale xmlns:android="http://schemas.android.com/apk/res/android" 
     android:duration="250" 
     android:fromXScale="0.0" 
     android:fromYScale="1.0" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     android:toXScale="1.0" 
     android:toYScale="1.0" /> 

爪哇邏輯

private Animation animation1, animation2; 
animation1 = AnimationUtils.loadAnimation(this, R.drawable.to_middle); 
animation1.setAnimationListener(this); 
animation2 = AnimationUtils.loadAnimation(this, R.drawable.from_middle); 
animation2.setAnimationListener(this); 

    if (flag = true) { 
     flipLayout.clearAnimation(); 
     flipLayout.setAnimation(animation1); 
     flipLayout.startAnimation(animation1); 
     flag = false; 
    } else { 
     flipLayout.clearAnimation(); 
     flipLayout.setAnimation(animation2); 
     flipLayout.startAnimation(animation2); 
     flag = true; 
    } 


@Override 
public void onAnimationStart(Animation animation) { 
    if (animation == animation1) { 
     data = true; 

    } else { 
     if (id == true) { 
      tv_calculation.setVisibility(View.GONE); 
      id = false; 
     } else { 
      tv_calculation.setVisibility(View.VISIBLE); 
      id = true; 
     } 
     data = false; 
    } 
} 

@Override 
public void onAnimationEnd(Animation animation) { 

    if (animation == animation1) { 

     flipLayout.clearAnimation(); 
     flipLayout.setAnimation(animation2); 
     flipLayout.startAnimation(animation2); 

    } else { 

     flipLayout.clearAnimation(); 
     flipLayout.setAnimation(animation1); 
     flipLayout.startAnimation(animation1); 
    } 

} 

FlipLayout是ImageView的。

+0

Thanx @ajit它的工作... – Sachin

4

使用對象動畫,它支持API級別11及以上。

here is sample

如果你想工作這個動畫在波紋管

(API等級11)的水平也用這種library

+0

我會嘗試這個代碼.. – Sachin