开发者

Android removeview error when animating

开发者 https://www.devze.com 2023-01-21 11:02 出处:网络
private void kartyafeleanim(String idx1, String idx2) { Animation anim1=AnimationUtils.loadAnimation(mycontext, R.anim.scalable_anim);
    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(myc开发者_Python百科ontext);
                        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) {}   


}

in the animation end, i calling removeview, and then the error occure. Why?


It's a weird problem of android, you can't change the view hierarchy in animationEnd.

All you have to do is to postpone your call like this :

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

精彩评论

暂无评论...
验证码 换一张
取 消