为什么AnimationSet没有正确执行动画组合?

时间:2012-02-21 12:27:25

标签: android animation

我使用AnimationSet在动画序列方面遇到了大麻烦。我想做以下动画:

从0增加到100%然后从A移动到B,同时从1.0移动到0.5到0.5和0.5到1.0。

我对这个序列有多个问题,但两个专业是:

  • 尽管我正确地计算了每个动画的起始偏移和持续时间,但移动动画结束并且我仍然处于alpha 0.5,所以它在位置B处再次增加到1.0。

  • 有时,alpha动画不会达到1.0,看起来它会停在0.9或者达到1.0,当重新绘制屏幕并显示为0.9时。

据我所知,AnimatorSet按顺序评估动画,但是使用startOffset可以并行运行它们。

这是我的一段代码:

public void moveAndDrawPart( ImageView partToMove, Button buttonPressed )
   {
      // Save current data for listener.
      lastPartToMove    = partToMove;
      lastButtonPressed = buttonPressed;

      // Animate part movement.
      animatePart(partToMove, buttonPressed);
   }

   // Animate part movement from original position to the final position.
   private void animatePart(ImageView part, Button destination)
   {
      // Make part visible.
      part.setVisibility(ImageView.VISIBLE);
      part.bringToFront();

      AnimationSet animator = new AnimationSet(false);
      animator.setFillAfter(false);

      // Make part appears by scaling up.
      ScaleAnimation scaleUp = new ScaleAnimation((float)0.0, (float)1.0, (float)0.0, (float)1.0, ScaleAnimation.RELATIVE_TO_SELF, (float)0.5, ScaleAnimation.RELATIVE_TO_SELF, (float)0.5);
      scaleUp.setInterpolator(new DecelerateInterpolator());
      scaleUp.setFillAfter(true);
      scaleUp.setStartOffset(0);
      scaleUp.setDuration(500);      

      // Move from origin to final position.
      TranslateAnimation move = new TranslateAnimation(0, destination.getX()-part.getX(), 0, destination.getY()-part.getY());
      move.setInterpolator(new DecelerateInterpolator());
      move.setFillAfter(true);
      move.setStartOffset(scaleUp.getDuration());
      move.setDuration(600);

      // Make part semi-transparent.
      AlphaAnimation alpha = new AlphaAnimation((float)1.0, (float)0.5);
      alpha.setInterpolator(new LinearInterpolator());
      alpha.setFillAfter(true);
      alpha.setRepeatCount(1);
      alpha.setRepeatMode(AlphaAnimation.REVERSE);
      alpha.setStartOffset(scaleUp.getDuration());
      alpha.setDuration(300);

      animator.addAnimation(scaleUp);
      animator.addAnimation(move);
      animator.addAnimation(alpha);
      animator.setAnimationListener(this);

      part.startAnimation(animator);
   }

   // Reposition part at final position to redraw it properly.
   private void repositionPart(ImageView part, Button destination)
   {
      // Clear part animation to bring back drawn position to original.
      part.clearAnimation();

      part.setX(destination.getX());
      part.setY(destination.getY());
   }

   public void onAnimationStart(Animation animation)
   {
      // This variable is used to workaround a glitch: clearAnimation() 
      // causes onAnimationEnd() to be called twice.
      animationDone = false;
   }

   public void onAnimationEnd(Animation animation)
   {
      // This variable is used to workaround a glitch: clearAnimation() 
      // causes onAnimationEnd() to be called twice.
      if(animationDone == false)
      {
         animationDone = true;

         if( (lastPartToMove != null) && (lastButtonPressed != null) )
         {
            // Reposition the part.
            repositionPart(lastPartToMove, lastButtonPressed);
         }
         else
         {
            Toast.makeText(myAct, "Major error occured. Last references are not set!", Toast.LENGTH_LONG).show();
         }
      }
   }

你能告诉我为什么AnimationSet没有做我期待的事情吗?

顺便说一下,我正在用Honeycomb 3.2模拟器试验这个问题。

1 个答案:

答案 0 :(得分:0)

创建动画或动画集后,必须将其加载到View类,如Layout。 那么您可以像这样设置该布局:

 TranslateAnimation trans = new TranslateAnimation(0, 0, 0, height/2);
        trans.setDuration(1000);


        myAnimationSet.addAnimation(trans);

        final RelativeLayout splash_relative = (RelativeLayout) findViewById(R.id.relativeLayout_splash);
        splash_relative.startAnimation(myAnimationSet);

它有效。我希望能有所帮助。