在动画制作过程中平滑地更改动画持续时间

时间:2017-07-22 19:14:37

标签: android animation zoom timertask

我有两件事。

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

第二件事是TimerTask,每20秒设置一次这个比例动画的持续时间。

问题在于,有时会出现“跳跃”等问题。在setDuration()出现的动画中。

首先我将此setDuration()放入TimerTask,然后我只是尝试在TimerTask中添加一个标记并更改onAnimationEnd()中的持续时间,它没有&#39不工作,同样的问题。在下面的代码中,我使用了这个标志技术。

如果它还不够清楚,那么所有这一切的目标就是拥有一个无限的"放大/缩小可绘制圆圈,放大/缩小速度随时间减小。它确实有效,但如上所述,它并不顺畅。

有没有办法顺利完成这项工作?

设置标志的TimeTask" 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

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

        }
    });

1 个答案:

答案 0 :(得分:0)

我对变量elapsedTimesessionLengthInSec的值更新有疑问。这意味着有时(elapsedTime > sessionLengthInSec)是真的,当它不应该发生circle.clearAnimation()时......确定它会产生跳跃!

如果持续时间变化太大,肯定会发生跳跃,但这种情况很正常,因为速度会迅速增加。在我的代码中并非如此,因为速度增加小于200毫秒,所以用眼睛

几乎看不到

对不起,实际上这段代码完美无缺......

相关问题