2011-06-16 96 views
1

我有一个活动,其中几个视图动画。当包含少数图像的RelativeLayout被点击时,动画被触发。它成立的onCreate()这样的:OnClickListener不会触发,直到onResume()

mContainer.setOnClickListener(new ContainerClicked()); 

而且ContainerClicked()类看起来是这样的:

private class ContainerClicked implements OnClickListener { 

    @Override 
    public void onClick(View arg0) { 
     Log.i(TAG, "TEST!"); 
     mSomeButton.setOnClickListener(null); 
     mSomeButton.setEnabled(false); 

     mAnotherButton.setOnClickListener(null); 
     mAnotherButton.setEnabled(false); 

     mYetAnotherButton.setOnClickListener(null); 
     mYetAnotherButton.setEnabled(false); 

     mContainer.setOnClickListener(null); 
     if (mLogo.getVisibility() == View.VISIBLE) 
      new AnimateToParty().execute(); 
     else 
      new AnimateFromParty().execute(); 
    } 
} 

这工作,和动画我只是想如何它的一切。

AnimateToParty()看起来是这样的:

private class AnimateToParty extends AsyncTask<Void, Integer, Void> { 

    @Override 
    protected void onProgressUpdate(final Integer... values) { 
     final View theView = findViewById(values[0]); 
     if (!(values[0] == mBackgroundImage.getId() || values[0] == mContainer 
       .getId())) { 
      final Animation animation = AnimationUtils.loadAnimation(
        DashboardActivity.this, values[1]); 
      animation.setAnimationListener(new AnimationListener() { 

       @Override 
       public void onAnimationStart(Animation arg0) { 
       } 

       @Override 
       public void onAnimationRepeat(Animation arg0) { 
       } 

       @Override 
       public void onAnimationEnd(Animation arg0) { 
        theView.setVisibility(View.INVISIBLE); 
       } 
      }); 
      theView.startAnimation(animation); 
     } else if (values[0] == mBackgroundImage.getId()) { 
      TransitionDrawable transition = (TransitionDrawable) theView 
        .getBackground(); 
      transition.startTransition(getResources().getInteger(
        android.R.integer.config_mediumAnimTime)); 
     } else { 
      TranslateAnimation animation = new TranslateAnimation(0, 0, 0, 
        TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 
          -60, getResources().getDisplayMetrics())); 
      animation.setAnimationListener(new AnimationListener() { 

       @Override 
       public void onAnimationStart(Animation arg0) { 
       } 

       @Override 
       public void onAnimationRepeat(Animation arg0) { 
       } 

       @Override 
       public void onAnimationEnd(Animation arg0) { 
        LayoutParams lp = (LayoutParams) mContainer 
          .getLayoutParams(); 
        lp.bottomMargin = 0; 
        RelativeLayout parent = (RelativeLayout) mContainer 
          .getParent(); 
        mSomeButton.setVisibility(View.GONE); 
        mAnotherButton.setVisibility(View.GONE); 
        mYetAnotherButton.setVisibility(View.GONE); 
        mLogo.setVisibility(View.GONE); 
        // mContainer.setLayoutParams(lp); 
        // This caused a visible flicker, so I went with the solution below. 
        parent.removeView(mContainer); 
        parent.addView(mContainer, lp); 
       } 
      }); 
      animation.setDuration(1000); 
      mContainer.startAnimation(animation); 
     } 
    } 

    @Override 
    protected Void doInBackground(Void... arg0) { 
     try { 
      publishProgress(mContainer.getId()); 
      publishProgress(mLogo.getId(), R.anim.slide_out_up); 
      Thread.sleep(100); 
      publishProgress(mSomeButton.getId(), R.anim.slide_out_left); 
      Thread.sleep(110); 
      publishProgress(mAnotherButton.getId(), R.anim.slide_out_left); 
      Thread.sleep(120); 
      publishProgress(mYetAnotherButton.getId(), R.anim.slide_out_left); 
      Thread.sleep(130); 
      publishProgress(mBackgroundImage.getId()); 
      Thread.sleep(550); 
     } catch (InterruptedException e) { 
      e.printStackTrace(); 
     } 
     return null; 
    } 

    @Override 
    protected void onPostExecute(Void result) { 
     mContainer.setOnClickListener(new LighterClicked()); 
    } 
} 

类以上动画一些按钮和标志闪开,然后揭示了容器。为了保持它,我改变它的layoutparams。只要改变它们就会产生一个可见的闪烁,在容器跳出60帧之前,它会停下来大约一帧(动画似乎没有完全结束),然后再安定到我想要的位置。我读了某处将其从父母中删除并重新放入,这不会产生相同的闪烁。但是,现在onclicklistener重新绑定不起作用!

我已经尝试了关于一切,我刚才注意到,每一次点击是没有“经过”大火如果我按home键,然后再次打开该应用程序。

有没有更好的方式来避免“忽悠”?一个更好的方法来做动画? (在动画视图中我很新)为什么点击事件在我重新打开应用程序时触发?

回答

4

为避免你看到的闪烁,你必须调用clearAnimation()onAnimationEnd

@Override 
public void onAnimationEnd(Animation arg0) { 
    theView.clearAnimation() 
    ... 
} 
+0

真棒开始!我明天会试试,我会盲目地进行编码。如果它有效,它可能会为我解决所有问题。 :) – AmITheRWord 2011-06-16 20:37:05

+0

是的。完善。 :) – AmITheRWord 2011-06-17 09:17:34

相关问题