动画时出现Android removeview错误

时间:2010-10-21 06:21:07

标签: android animation

    private void kartyafeleanim(String idx1, String idx2) {
    Animation anim1=AnimationUtils.loadAnimation(mycontext, R.anim.scalable_anim);
    Animation anim2=AnimationUtils.loadAnimation(mycontext, R.anim.scalable_anim);

    try {
        for (int i=0; i<6; i++) {
            LinearLayout LL = (LinearLayout) myLinearLayout.getChildAt(i);

            for (int j=0; j<LL.getChildCount(); j++) {
                if (LL.getChildAt(j).getTag().toString().equals(idx1) || LL.getChildAt(j).getTag().toString().equals(idx2)) {

                    final View v = LL.getChildAt(j);

                    int rtop=0;
                    if (v.getTag().toString().equals(idx1)) rtop=110+(53*((int)((Integer.parseInt(idx1)-100)/6)));
                    if (v.getTag().toString().equals(idx2)) rtop=110+(53*((int)((Integer.parseInt(idx2)-100)/6)));
                    int left=v.getLeft();
                    int top =v.getTop()+rtop-30;

                    FrameLayout.LayoutParams lp =new FrameLayout.LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT, Gravity.LEFT);
                    lp.setMargins(left, top, 0, 0);

                    if (v.getTag().toString().equals(idx1)) {
                        final ImageView img1 =new ImageView(mycontext);
                        img1.setBackgroundResource(R.drawable.match);

                        img1.setLayoutParams(lp);
                        myLinearLayoutAll.addView(img1);

                        img1.setVisibility(View.VISIBLE);
                        img1.startAnimation(anim1);

                        anim1.setAnimationListener(new AnimationListener() {
                            @Override
                            public void onAnimationEnd(Animation animation) {
                                //img1.setVisibility(View.INVISIBLE);
                                myLinearLayoutAll.removeView(img1);
                            }
                            @Override
                            public void onAnimationRepeat(Animation animation) {}
                            @Override
                            public void onAnimationStart(Animation animation) { }

                        });
                    }
                    else if (v.getTag().toString().equals(idx2)) {
                        final ImageView img2 =new ImageView(mycontext);
                        img2.setBackgroundResource(R.drawable.match);

                        img2.setLayoutParams(lp);
                        myLinearLayoutAll.addView(img2);

                        img2.setVisibility(View.VISIBLE);
                        img2.startAnimation(anim2);

                        anim2.setAnimationListener(new AnimationListener() {
                            @Override
                            public void onAnimationEnd(Animation animation) {
                                //img2.setVisibility(View.INVISIBLE);
                                myLinearLayoutAll.removeView(img2);
                            }
                            @Override
                            public void onAnimationRepeat(Animation animation) {}
                            @Override
                            public void onAnimationStart(Animation animation) { }

                        });
                    }
                }
            }
        }

    } catch (Exception ex) {}   


}

在动画结束时,我调用removeview,然后发生错误。为什么?

1 个答案:

答案 0 :(得分:6)

这是android的一个奇怪问题,你无法在animationEnd中更改视图层次结构。

您所要做的就是推迟拨打电话:

@Override
public void onAnimationEnd(Animation animation) {
    post(new Runnable() {
        public void run() {
            myLinearLayoutAll.removeView(img2);
        }
    });
}