2017-07-22 124 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毫秒,几乎无法用眼睛看到/

对不起,实际上此代码完美工作...