首页 > 解决方案 > 在 if 语句中只激活一次动画

问题描述

我有一个倒计时计时器,我使用了一些 if 语句来标记倒计时中的某些间隔。我想播放一个动画,我将 TextView 从一个点移动到另一个点,但我希望它在间隔内完成一次。例如,我设置了一个从 0 到 60000 毫秒的时间间隔,我希望动画在该时间间隔内播放一次,并带有以下动画:

    <?xml version="1.0" encoding="utf-8"?>
<set
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/linear_interpolator"
    android:fillAfter="true">
    <translate
        android:fromYDelta="0%"
        android:toYDelta="-50%"
        android:duration="800" />
</set>

我的计时器代码的一部分:

     private void startTimer() {
        Log.println(Log.ASSERT, "CHECK","Entered startTimer() method");
        millisInFuture = mTimeLeftInMillis;
        mCountDownTimer = new CountDownTimer(mTimeLeftInMillis, 1000) {
            @Override
            public void onTick(long millisUntilFinished) {
                mTimeLeftInMillis = millisUntilFinished;
                updateCountDownText();
                final long millisPassed = millisInFuture - mTimeLeftInMillis;
                progress = (int) (millisPassed * 100 / millisInFuture);
                pb.setProgress(progress);
                pb2.setProgress(0);
                pb3.setProgress(0);


/////////////////////////////////////////////////////////////////////////////////////////////////////////////////
                //Key: 60 sec
                if (millisInFuture == 480000) {
                    if (millisPassed <= 60000 || (millisPassed > 180000 && millisPassed <= 240000) || (millisPassed > 300000 && millisPassed <= 360000 || (millisPassed > 420000 && millisPassed <= 480000))) {
//                        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.fade_in);
//                        stepupimage.setAnimation(animation);
                        setflowrate();
                        Log.println(Log.ASSERT,"CHECK","Check that the first if statement of key 60 is entered");
                        statusIfUp();
                        time_of_stage = (millisInFuture - millisPassed) % 60000;
                        progress2 = (int) (time_of_stage*100 / 60000);
                        Log.println(Log.VERBOSE,"CHECK","TIME OF STAGE = "+time_of_stage);
                        Log.println(Log.VERBOSE,"CHECK","progress2= "+progress2);
                        pb2.setProgress(progress2);
                        updateStageUpCount();

                        upArrowAnimation();

setflowrate是我用来调用动画的方法

 private void setflowrate()
{
    flow.setText(String.valueOf(num) + " GPM");
    Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.move_up);
    flow.setAnimation(animation);
    linearlayoutofflow.setAnimation(animation);
    progressBar.setAnimation(animation);

}

flow、linearlayoutflow、progressbar 应该在上面的时间间隔内向上移动,但我希望每个间隔只发生一次移动。

编辑:问题是动画一直在循环。我希望它在每秒调用一次的 onTick 方法中发生一次。我使用 if 语句指定的时间间隔我希望动画只在其中发生一次。

标签: androidandroid-animationcountdowntimer

解决方案


我想到的最简单的方法是设置一个布尔标志,如果为真则播放动画并将布尔值设置为假,如果为假则不播放动画。

private boolean animate = true;

private void setflowrate() {
    if(animate) {
        flow.setText(String.valueOf(num) + " GPM");
        Animation animation = AnimationUtils.loadAnimation(tool1mode1.this, R.anim.move_up);
        flow.setAnimation(animation);
        linearlayoutofflow.setAnimation(animation);
        progressBar.setAnimation(animation);
        animate = false;
    }
}

在所有进程结束时,或者正如您所说的“间隔”,您再次将标志设置为 true,以防您希望能够重新启动动画。


推荐阅读