2010-10-21 72 views
2
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,然后出现错误。为什么?安卓removeview错误时动画

+0

什么错误? – 2010-10-21 07:01:56

+0

10-21 08:17:54.605:错误/ AndroidRuntime(10380):java.lang.NullPointerException – lacas 2010-10-21 07:13:58

回答

6

这是一个奇怪的android问题,您不能更改animationEnd中的视图层次结构。

所有你需要做的就是推迟你的电话是这样的:

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

+1这是非常有用的信息,它的工作原理。你有更详细的解释为什么在UI线程中运行的'onAnimationEnd'不能用来改变视图层次结构,或者至少你如何设法找到解决方法? – kiruwka 2014-01-11 20:58:05