2017-07-22 122 views
1

我有兩件事。平滑地改變動畫製作期間的動畫持續時間

首先是一個循環縮放動畫,做一種永久放大/縮小。

第二件事是TimerTask設置這個縮放動畫的持續時間每20秒。

問題是,當發生setDuration()時,動畫中有時會出現某種「跳躍」。

首先,我把這個setDuration()TimerTask,然後我只是試圖把國旗在TimerTaskonAnimationEnd()改變了時間,沒有工作沒有,同樣的問題。在他的代碼下面我使用這個標誌技術。

如果沒有足夠的清晰度,所有這些的目標是對可繪製圓進行「無限」放大/縮小,放大/縮小速度會隨着時間的推移而減少。如上所述,它確實有效,但並不順利。

有沒有辦法順利地做到這一點?

,設置標誌 「changeDurationFlag」

private void setRegularRythmeDecrease() { 

    final Handler handler = new Handler(); 
    Timer timer = new Timer(); 

    TimerTask task = new TimerTask() { 
     @Override 
     public void run() { 
      handler.post(new Runnable() { 
       public void run() { 
        try { 
         if (elapsedTime > sessionLengthInSec) { 
          circle.clearAnimation(); 
         } 
         zoomDuration = zoomDuration + (toDecreaseEveryUpdate/2); 
         changeDurationFlag = true; 


        } catch (Exception e) { 
         e.printStackTrace(); 
        } 
       } 
      }); 
     } 
    }; 
    timer.schedule(task, 0, BREATH_RYTHME_UPDATE_INTERVAL_IN_SECONDS*1000); 
} 

我使用放大和縮小的ScaleAnimation的TimeTask

public Animation scaleAnimation(View v, float startScale, float endScale, long duration) { 
    Animation anim = new ScaleAnimation(
      startScale, endScale, 
      startScale, endScale, 
      Animation.RELATIVE_TO_SELF, 0.5f, 
      Animation.RELATIVE_TO_SELF, 0.5f); 
    anim.setFillAfter(true); 
    anim.setDuration(duration); 
    return anim; 
} 

,其中持續時間設置動畫聽衆

zoomDuration = ZOOM_DURATION_START; 
    animZoomIn = scaleAnimation(circle, 1f, ZOOM_FACTOR,zoomDuration); 
    animZoomOut = scaleAnimation(circle, ZOOM_FACTOR, 1f,zoomDuration); 
    animZoomIn.setAnimationListener(new Animation.AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 
      // If the flag is true (modified in the TimerTask) I set the Duration to decrease the speed 
      // it's where the not smoothly thing happens 
      if(changeDurationFlag) { 
       Log.d("beat ","Set breath to " + String.valueOf(zoomDuration * 2d)); 
       animZoomIn.setDuration(zoomDuration); 
       animZoomOut.setDuration(zoomDuration); 
       changeDurationFlag = false; 
      } 
      circle.startAnimation(animZoomOut); 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 
    }); 
    animZoomOut.setAnimationListener(new Animation.AnimationListener() { 
     @Override 
     public void onAnimationStart(Animation animation) { 

     } 

     @Override 
     public void onAnimationEnd(Animation animation) { 

      circle.startAnimation(animZoomIn); 

      currentDateTime = Calendar.getInstance().getTime(); 
      elapsedTime = currentDateTime.getTime() - startDateTime.getTime(); 

      long elapsedTimeInSeconds = TimeUnit.MILLISECONDS.toSeconds(elapsedTime); 

      beatCount++; 
     } 

     @Override 
     public void onAnimationRepeat(Animation animation) { 

     } 
    }); 

回答

0

我對變量elapsedTimesessionLengthInSec的值更新存在問題。這意味着有時(elapsedTime > sessionLengthInSec)是真的,當它不應該和circle.clearAnimation()發生......所以確保它產生了跳躍!

當然仍可能發生跳躍如果持續時間的變化實在是太多了大,但它是一種正常的,因爲速度會迅速增加。在我的代碼中,情況並非如此,因爲速度增加小於200毫秒,幾乎無法用眼睛看到/

對不起,實際上此代碼完美工作...