2013-03-28 75 views
3

我想製作一個順序動畫,其中兩個按鈕收縮直到它們消失,然後再次增長到原始大小。 當我運行它的按鈕地消失,沒有任何動畫Android縮小和增長順序動畫

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
    android:interpolator="@android:anim/accelerate_decelerate_interpolator" 
    android:fillAfter="true"> 

    <scale 
     android:fromXScale="1" 
     android:toXScale="0" 
     android:fromYScale="1" 
     android:toYScale="0" 
     android:duration="400" 
     android:pivotX="50%" 
     android:pivotY="50%" 
     /> 

    <scale 
     android:startOffset="700" 
     android:fromXScale="0" 
     android:toXScale="1" 
     android:fromYScale="0" 
     android:toYScale="1" 
     android:duration="400" 
     android:pivotX="50%" 
     android:pivotY="50%" 
    /> 

</set> 

Animation sgAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shrink_grow); 
btPrimary.startAnimation(sgAnimation); 
btSecondary.startAnimation(sgAnimation); 
+0

當你離開的第二個'scale'動畫會發生什麼?按鈕縮小了嗎? – Axarydax

+0

是的,兩個動畫單獨工作 – JoeyCK

回答

10

下面是一個放大/縮小動畫的作品(不休)。 設置動畫repeatcount當然不會工作,但你可以自己添加一個計數器來阻止它。

(順便說一句, 「l」 是要動畫視圖)

final ScaleAnimation growAnim = new ScaleAnimation(1.0f, 1.15f, 1.0f, 1.15f); 
    final ScaleAnimation shrinkAnim = new ScaleAnimation(1.15f, 1.0f, 1.15f, 1.0f); 

    growAnim.setDuration(2000); 
    shrinkAnim.setDuration(2000); 

    l.setAnimation(growAnim); 
    growAnim.start(); 

    growAnim.setAnimationListener(new AnimationListener() 
    { 
     @Override 
     public void onAnimationStart(Animation animation){} 

     @Override 
     public void onAnimationRepeat(Animation animation){} 

     @Override 
     public void onAnimationEnd(Animation animation) 
     { 
      l.setAnimation(shrinkAnim); 
      shrinkAnim.start(); 
     } 
    }); 
    shrinkAnim.setAnimationListener(new AnimationListener() 
    { 
     @Override 
     public void onAnimationStart(Animation animation){} 

     @Override 
     public void onAnimationRepeat(Animation animation){} 

     @Override 
     public void onAnimationEnd(Animation animation) 
     { 
      l.setAnimation(growAnim); 
      growAnim.start(); 
     } 
    });  
3

採用Android:REPEATMODE = 「反向」 的android:repeatCount = 「無限」,那麼它會重複我用無限的時間它在我的補間動畫中工作

+0

重複不是問題。他在問如何順序運行它們。 – RichieHH

0

使用下面的代碼,這是工作代碼。在anim文件夾中複製這個shrink_grow.xml。

<?xml version="1.0" encoding="utf-8"?> 
<set xmlns:android="http://schemas.android.com/apk/res/android" 
android:fillAfter="false" 
android:fillBefore="false" 
android:shareInterpolator="true" > 

<scale 
    android:duration="1000" 
    android:fillAfter="true" 
    android:fillBefore="false" 
    android:fillEnabled="true" 
    android:fromXScale="1.0" 
    android:fromYScale="1.0" 
    android:toXScale="0.0" 
    android:toYScale="0.0" /> 
<scale 
    android:duration="1000" 
    android:fillAfter="true" 
    android:fillBefore="false" 
    android:fillEnabled="true" 
    android:fromXScale="0.0" 
    android:fromYScale="0.0" 
    android:startOffset="1000" 
    android:toXScale="1.0" 
    android:toYScale="1.0" /> 

</set> 

代碼:

Animation sgAnimation = AnimationUtils.loadAnimation(getApplicationContext(), R.anim.shrink_grow); 
btPrimary.startAnimation(sgAnimation); 
btSecondary.startAnimation(sgAnimation); 
+0

如何評論和解釋爲什麼它應該與連續動畫一起使用? – RichieHH

+1

我用問題中發佈的代碼玩了一下,並做了一些小的修改,那些可以看到差異,差異是1.在fromXScale等中使用float值2.填充fillBefore的值。我不知道爲什麼這不起作用的原因以及它爲什麼起作用。 – raju

1
 final ScaleAnimation growAnim = new ScaleAnimation(1.0f, 1.5f, 1.0f, 1.5f, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F); 
    final ScaleAnimation shrinkAnim = new ScaleAnimation(1.5f, 1.0f, 1.5f, 1.0f, Animation.RELATIVE_TO_SELF, 0.5F, Animation.RELATIVE_TO_SELF, 0.5F); 

    growAnim.setDuration(2000); 
    shrinkAnim.setDuration(2000); 

    view.setAnimation(growAnim); 
    growAnim.start(); 

    growAnim.setAnimationListener(new Animation.AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      view.setAnimation(shrinkAnim); 
      shrinkAnim.start(); 
     } 
    }); 
    shrinkAnim.setAnimationListener(new Animation.AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 
     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      view.setAnimation(growAnim); 
      growAnim.start(); 
     } 
    });