2012-04-02 3 views
3

これは疑問ではありません。他の人と出会ったときの問題や解決方法などです。
基本的には、ViewAnimatorを作成しようとしていましたが、ViewAnimatorはユーザーのクリックに応じて追加の子を作成します。私はViewAnimatorのOnDrawは、onAnimationEndで子を削除した場合にNullPointerExceptionをスローします。

outAnimation.setAnimationListener(listener); 

を入れて、AnimationListener

public void onAnimationEnd(Animation animation) { 
    viewAnimator.removeView(out); 
} 

では、上記のアプローチの問題は、すぐにonAnimationEnd後、それはNullPointerExceptionがスローされ、中に次のビューをアニメ化した後にクリーンアップする
。基本的には、ViewAnimatorは引き続き描画するためにアニメーション化されている子ビューを使用しています。私はそれを削除して以来、そこにnullがあります。 私は研究をしましたが、基本的にこれは既知のバグです。参照:Android Animation is not finished on onAnimationEnd

これを解決するには、レイアウトを変更しました。

<ViewAnimator 
    android:id="@+id/animator" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent"> 

    <FrameLayout 
     android:id="@+id/container1" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent"> 
    </FrameLayout> 

    <FrameLayout 
     android:id="@+id/container2" 
     android:layout_width="fill_parent" 
     android:layout_height="fill_parent" > 
    </FrameLayout> 
</ViewAnimator> 

とonAnimationEnd無事container.removeAllViews()を呼び出すことができます。中に新しいビューをアニメーション化するために、私は隠されたコンテナと

container.addView(newView); 
animator.setDisplayedChild(animator.indexOfChild(container)); 

を選択し、私はあなたのコメントやアドバイスを参照させていただきます。

答えて

4

私はこの問題に遭遇するとアニメーションが実際に行われるまで待機するビューのpostメソッドを使用しました:

 public void onAnimationEnd(Animation animation) { 
     //Wait until the container has finished rendering to remove the items. 
     view.post(new Runnable() { 
      @Override 
      public void run() { 
      //remove view here 
      } 
     }); 
     } 
+0

私は私の場合、それはいくつかの奇妙な理由で再びinAnimationをトリガー.' 'postDelayed(Runnableをタスク、delayMillisをint型)、同様のアプローチを試してみました。 –

1

私はそれを解決しました。私はinAnimationとoutAnimationを持っています。 SO:

@Override 
public void onAnimationEnd(Animation animation) { 

    if(animationsFinished < 2) animationsFinished++; 
    else{ 

     this.setInAnimation(null); // Skipping this will cause trouble 
     this.setOutAnimation(null); // Skipping this will cause trouble 

     flipper.post(new Runnable(){ 

      @Override 
      public void run() { 
       flipper.removeView(previous); 
      } 

     }); 

     animationsFinished = 0; 

    } 


}