2012-06-28 18 views
4

我試圖建立一個動畫,其中這一形象:Android LayoutAnimationController:如何強制第一個孩子在重複之前等待最後一個孩子的動畫完成?

No bars

變爲這一形象:

Bars

凡三個槓淡入像1 ... 2 ... .3 ...然後回到無。此時動畫將重複播放。這裏是我試圖讓工作:

AlphaAnimation anim = (AlphaAnimation) AnimationUtils.loadAnimation(this, R.anim.fade_in); 
    anim.setRepeatMode(Animation.RESTART); 
    anim.setRepeatCount(Animation.INFINITE); 
    anim.setStartOffset(3000); 

    LayoutAnimationController controller = new LayoutAnimationController(anim, 0.5f); 

    rl.setLayoutAnimation(controller); 

    rl.startLayoutAnimation(); 

幾乎作品,但問題是,這些無線條的視圖組在第一胎不等待最後一個子來完成其動畫在它熄滅之前。結果是一個節拍,看起來像1 ... 2 ... 3..1..2.3 ... 1 ... 3 ... 1..2.3,而不是一個不錯的1..2..3。 .1..2..3。我需要找到一種方法讓控制器等待,直到循環完成,然後再次關閉第一個孩子。我一直無法找到任何解決方案,有沒有人有一些輸入?我願意改變我完全這樣做的方式,這個LayoutAnimationController類似乎是迄今爲止最好的方式。

謝謝!

回答

2

當我不得不爲圖像使用動畫時,我準備了框架。我用AnimatioDrawable這個類:

* This class is a hack to simply get an on finish callback for an 
* AnimationDrawable. 
* 
*/ 
public class CustomAnimationDrawable extends AnimationDrawable { 
// We need to keep our own frame count because mCurFrame in AnimationDrawable is private 
private int mCurFrameHack = -1; 

// This is the Runnable that we will call when the animation finishes 
private Runnable mCallback = null; 

/* 
* We override the run method in order to increment the frame count the same 
* way the AnimationDrawable class does. Also, when we are on the last frame we 
* schedule our callback to be called after the duration of the last frame. 
*/ 
@Override 
public void run() { 
super.run(); 

mCurFrameHack += 1; 
if (mCurFrameHack == (getNumberOfFrames() - 1) && mCallback != null) { 
scheduleSelf(mCallback, SystemClock.uptimeMillis() + getDuration(mCurFrameHack)); 
} 
} 

/* 
* We override this method simply to reset the frame count just as is done in 
* AnimationDrawable. 
*/ 
@Override 
public void unscheduleSelf(Runnable what) { 
// TODO Auto-generated method stub 
super.unscheduleSelf(what); 
mCurFrameHack = -1; 
} 

public void setOnFinishCallback(Runnable callback) { 
mCallback = callback; 
} 

public Runnable getOnFinishCallback() { 
return mCallback; 
} 

} 

而在我的代碼,我用它喜歡:

 CustomAnimationDrawable staticAnimation = new CustomAnimationDrawable(); 
    //add frames with resources and duration. step is just a class of mine 
     staticAnimation.addFrame(getResources().getDrawable(step.getStepImageResId()), step.getStepDuration()); 
    staticAnimation.addFrame(getResources().getDrawable(step.getStepImageResId()), step.getStepDuration()); 
    staticAnimation.addFrame(getResources().getDrawable(step.getStepImageResId()), step.getStepDuration()); 
     staticAnimation.setOneShot(false); 

     imgView.setBackgroundDrawable(staticAnimation); 
staticAnimation.start(); 

可能不是最好的解決方案在那裏,但它的工作對我的需要。

相關問題