3

これは質問ではなく、私が遭遇した問題とそれをどのように解決したかを他の人と共有するようなものです.
基本的に、ユーザーのクリックに応じて追加の子を作成する ViewAnimator を作成しようとしていました。
次のビューをアニメーション化した後にクリーンアップするには、

outAnimation.setAnimationListener(listener);

および AnimationListener で

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

さて、上記のアプローチの問題は、onAnimationEnd の直後に NullPointerException がスローされることです。基本的に、これは ViewAnimator がまだアニメーション化されている子ビューを使用して描画していることを意味します。私はそれを削除したので、そこには null があります。私は調査を行いましたが、基本的に、これは既知のバグのようです。参照: onAnimationEnd で Android アニメーションが終了していない

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

<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

2 に答える 2

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
          }
        });
      }
于 2012-04-02T21:57:50.713 に答える
2

私はそれを解決しました。inAnimation と outAnimation があります。それで:

@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;

    }


}
于 2012-07-18T10:03:23.087 に答える