Search code examples
androidandroid-animationcountdowntimer

activate an animation only once inside an if statement


I have a countdown timer and I used some if statements to mark certain intervals within the countdown. I want to play an animation where I move an TextView from one point to another but I want it to be done once within the interval. I have for example set an interval from 0 to 60000 milliseconds I want the animation to play once during that interval, with the following animation:

    <?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>

Part of my timer code:

     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 is the method I use to call my animation

 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, linearlayoutoutflow, progressbar are supposed to move upwards during the time intervals above but I want the movement to occur only once per interval.

Edit: the problem is that the animation keeps looping. I want it to occur once inside the onTick method which gets called every second. The time intervals I have designated using the if statements I want the animation to occur in them only once.


Solution

  • The easiest way that comes to my mind would be setting a Boolean flag, if true play the animation and set the boolean to false, if false do not play the animation.

    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;
        }
    }
    

    At the end of all processes, or as you say 'the interval' you set the flag to true again in case you want to be able to restart the animation.