Jaffar Raza
Jaffar Raza

Reputation: 1

Start and then pause animation in android

I have a circular layout and there are "n" buttons in this layout. I start the animation on that layout when the activity starts.

When I click on any button the animation should stop and a dialog appear with the message, "You have clicked this 'XYZ' button".

The code I am using:

    animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
    animation.setFillEnabled(true);
    animation.setFillAfter(true);
    findViewById(R.id.circle_layout).startAnimation(animation);

and the animation XML:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="15000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="360" >

Upvotes: 0

Views: 2943

Answers (3)

wseme
wseme

Reputation: 835

There is no pause as posted. But you can mimic it.

This answer worked for me: How do I pause frame animation using AnimationDrawable?

public class PausableAlphaAnimation extends AlphaAnimation {

    private long mElapsedAtPause=0;
    private boolean mPaused=false;

    public PausableAlphaAnimation(float fromAlpha, float toAlpha) {
        super(fromAlpha, toAlpha);
    }

    @Override
    public boolean getTransformation(long currentTime, Transformation outTransformation) { 
        if(mPaused && mElapsedAtPause==0) {
            mElapsedAtPause=currentTime-getStartTime();
        }
        if(mPaused)
            setStartTime(currentTime-mElapsedAtPause);
        return super.getTransformation(currentTime, outTransformation);
    }

    public void pause() {
        mElapsedAtPause=0;
        mPaused=true;
    }

    public void resume() {
        mPaused=false;
    }
}

Please note: this does not technically 'pause' the animation because it keeps continuously calling the transformation. But can keeps a persistent transformation that 'mimics' the same functionality.

I tried this with a RotateAnimation and worked just fine. But it will not lower the CPU/framerate when it is 'paused' as it does when you cancel the animation.

Upvotes: 1

Jschools
Jschools

Reputation: 2767

There is no great way to pause an animation mid-cycle.

You could subclass RotateAnimation and intercept the currentTime value in getTransformation and feed it the same time while you want your animation to be paused.

If you can afford to only support HC+, then you should look into using property animations instead of View animations.

Upvotes: 0

D&#39;yer Mak&#39;er
D&#39;yer Mak&#39;er

Reputation: 1632

There is no pause in animation in android. I checked many questions relating to the same on StackOverflow but no luck. You can still give a try to pausing the Activity itself using this link which may help. The link states the following:

Pause Your Activity

When the system calls onPause() for your activity, it technically means your activity is still partially visible, but most often is an indication that the user is leaving the activity and it will soon enter the Stopped state. You should usually use the onPause() callback to:

  1. Stop animations or other ongoing actions that could consume CPU.
  2. Commit unsaved changes, but only if users expect such changes to be permanently saved when they leave (such as a draft email).
  3. Release system resources, such as broadcast receivers, handles to sensors (like GPS), or any resources that may affect battery life while your activity is paused and the user does not need them.

Upvotes: 1

Related Questions