2012-02-21 69 views
1

我在使用AnimationSet的動畫序列中遇到了很大的麻煩。我想要做下面的動畫:爲什麼AnimationSet不能正確執行動畫組合?

從0到100%放大,然後從A移動到B,同時alpha從1.0到0.5和0.5到1.0。

我有這個順序多問題,但是這兩個專業是:

  • 儘管我計算起始偏移量和每個動畫的適當時間,此舉動畫結束,我仍然在alpha 0.5,所以它增加回到1.0一次在位置B.

  • 有時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仿真器來試驗這個問題。

+0

關於在動畫結束時有時未達到1.0的alpha值。我在一個設備上測試了它,並獲得了相同的結果,直到我在應用程序中激活硬件加速。從現在起,阿爾法似乎總是達到1.0。但是,我仍然在仿真器上看到奇怪的行爲。 – DavidH 2012-02-23 01:01:55

+0

有沒有人有機會嘗試我的代碼,看看它是否有問題? – DavidH 2012-02-24 12:04:09

回答

0

創建動畫或動畫集後,您必須將其加載到View類,如佈局。 那麼你可以動態顯示佈局是這樣的:

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

和它的作品。我希望這是幫助。

相關問題