【问题标题】:Smoothly change animation duration during animating在动画期间平滑更改动画持续时间
【发布时间】:2017-07-23 15:57:42
【问题描述】:

我有两件事。

第一个是循环缩放动画,执行一种永久放大/缩小。

第二件事是TimerTask,它设置此缩放动画的持续时间每 20 秒。

问题是,当setDuration() 出现时,有时动画中会出现一种“跳跃”。

首先我将这个setDuration() 放在TimerTask 中,然后我只是尝试在TimerTask 中放置一个标志并更改onAnimationEnd() 中的持续时间,它也不起作用,同样的问题。在下面的代码中,我使用了这个标志技术。

如果还不够清楚,所有这些的目标是“无限”放大/缩小可绘制的圆圈,放大/缩小速度会随着时间的推移而降低。它确实有效,但并不顺利,如上所述。

有没有办法顺利做到这一点?

设置标志“changeDurationFlag”的 TimeTask

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

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

        }
    });

【问题讨论】:

    标签: android animation zooming timertask


    【解决方案1】:

    变量elapsedTimesessionLengthInSec 的值更新出现问题。这意味着有时(elapsedTime > sessionLengthInSec) 在不应该的情况下为真,而circle.clearAnimation() 发生了......所以肯定它产生了跳跃!

    当然,如果持续时间变化太大,跳跃​​仍然会发生,但这是正常的,因为速度会迅速增加。在我的代码中并非如此,因为速度增加不到 200 毫秒,所以肉眼几乎看不到/

    抱歉,实际上这段代码运行良好......

    【讨论】:

      猜你喜欢
      • 2013-01-18
      • 1970-01-01
      • 2021-10-19
      • 1970-01-01
      • 1970-01-01
      • 2018-06-15
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多