我試圖讓ValueAnimator
重複一次。我用SeekBar
在ListView
中使用它。由於某種原因,ValueAnimator
將完成,再次觸發onAnimationEnd()
,但當到達結尾時,onAnimationEnd()
永遠不會被第二次調用。ValueAnimator只重複一次
@Override
public View getContentView(int position, View convertView, ViewGroup parent) {
...
setupTimerBar(t);
...
}
private AnimatorListenerAdapter generateNewAnimatorListenerAdapter(final TylersContainer t) {
return new AnimatorListenerAdapter() {
@Override
public void onAnimationEnd(Animator animation) {
setupTimerBar(t);
}
};
}
private void setupTimerBar(TylersContainer t)
{
View view = t.getView();
BusTime arrivalTime = t.getBusTime();
int minutes = BusTime.differenceInMiuntes(arrivalTime, BusTime.now());
long milliseconds = minutes * 60 * 1000;
final TimerBar seekBar = (TimerBar) view.findViewById(R.id.SeekBar);
int progress = Utility.setProgress(arrivalTime, seekBar.getMax());
long duration = Utility.setAnimationDuration(progress);
seekBar.setProgress(progress);
seekBar.setAnimationDuration(duration);
seekBar.setAnimationStartDelay(milliseconds);
seekBar.setAnimatorListenerAdapter(generateNewAnimatorListenerAdapter(t));
}
的seekBar
對象實際上是包含SeekBar
和ValueAnimator
的自定義對象,這裏的相關位:
//Constructor
public TimerBar(Context context) {
super(context);
startTime = Calendar.getInstance();
valueAnimator = ValueAnimator.ofInt(0, getMax());
//Override the update to set this object progress to the animation's value
valueAnimator.addUpdateListener(new AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
int animProgress = (Integer) animation.getAnimatedValue();
setProgress(animProgress);
}
});
}
//Specify the start time by passing a long, representing the delay in milliseconds
public void setAnimationStartDelay(long milliseconds){
//Set the delay (if need be) and start the counter
if(milliseconds > 0)
valueAnimator.setStartDelay(milliseconds);
valueAnimator.setIntValues(this.getProgress(), this.getMax());
valueAnimator.start();
}
//Set the duration of the animation
public void setAnimationDuration(long duration){
valueAnimator.setDuration(duration);
}
public void setAnimatorListenerAdapter(AnimatorListenerAdapter ala){
valueAnimator.addListener(ala);
}
我想不通爲什麼不會重複兩次以上。
我試過使用Repeat
屬性,並將其設置爲INIFINITI
但這也沒有幫助。
編輯:要清楚,我想獲得的是無限重複本身具有不同的持續時間,每次的動畫。
一些問題:什麼是'TylersContainer'?爲什麼每次執行'setupTimerBar'時添加一個新的監聽器,並且不會刪除任何?爲什麼在'setAnimationStartDelay'之後調用'setAnimatorListenerAdapter'?你是如何驗證'onAnimationEnd'只被調用一次的? – dst
'TylersContainer'是一個持有視圖和下一班車何時到達的時間的對象(顯示在該視圖中)。我每次都添加了一個新的監聽器,因爲這是我在我發現的例子中如何完成的,我也不知道你必須刪除監聽器,我認爲他們在完成後才被刪除。 'setAnimationListenerAdapter'和'setAnimationStartDelay'的順序是微不足道的。我驗證了'onAnimationEnd'只用一些打印語句被調用來記錄貓。 「 – Tyler
」setAnimationListenerAdapter和setAnimationStartDelay的順序很簡單。「我問,因爲我不確定是否有可能在添加偵聽器之前調用onAnimationEnd。通常,您可以嘗試使用android的調試功能逐步執行代碼的執行。也許這會產生結果? – dst