当动画的android removeview错误 [英] Android removeview error when animating

查看:613
本文介绍了当动画的android removeview错误的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

    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,然后错误occure。为什么?

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

推荐答案

这是Android的一个奇怪的问题,你不能改变animationEnd视图层次。

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);
        }
    });
}

这篇关于当动画的android removeview错误的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆