0

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

BSMP
  • 4,596
  • 8
  • 33
  • 44
Jaffar Raza
  • 1
  • 1
  • 1
  • What does your code to stop the animation look like, and which part of it is giving you trouble? – Simon MᶜKenzie Apr 19 '13 at 05:56
  • The problem is: when i click on any button the animation resets to its orignal position and all vies comes to its orignal position. and clicked occurs with respect to its orignal position of the views. – Jaffar Raza Apr 19 '13 at 06:12

3 Answers3

1

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.
D'yer Mak'er
  • 1,632
  • 5
  • 24
  • 47
1

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.

Community
  • 1
  • 1
wseme
  • 805
  • 6
  • 14
0

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.

Jschools
  • 2,698
  • 1
  • 17
  • 18