Pause and Resume Translate Animation

android, android-animation, java

Solution

After searching for a time i found this link and check is this working for Translate Animation or not and after some modification this is working for your animation too.!

See modified code below:

public class TranslateAnim extends TranslateAnimation{

    public TranslateAnim(float fromXDelta, float toXDelta, float fromYDelta,
            float toYDelta) {
        super(fromXDelta, toXDelta, fromYDelta, toYDelta);
        // TODO Auto-generated constructor stub
    }

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

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

I'll only change class name, extends class name and constructor of this class.

you can use it like:

TranslateAnim set1, set2, set3, set4; // objects of TranslateAnim Class
    
set1 = new TranslateAnim(-4, 10, -110, 0); // initialize all objects like this way
    
animSet.addAnimation(set1); // add all animation objests in your animation set as you do before
            
animSet.setFillAfter(true);

and after start your animation you have only call pause and resume methods. Thanks to Johan for share his code with us.

Hope this solve your problem. :)

Problem

I am using Translate Animation for moving an `ImageView`. I am using this code: ``` TranslateAnimation set1 = new TranslateAnimation(-4, 10, -110, 0); set1.setDuration(3000); TranslateAnimation set2 = new TranslateAnimation(10, -3, 0, 115); set2.setDuration(3000); set2.setStartOffset(2200); TranslateAnimation set3 = new TranslateAnimation(-3, -20, 0, -100); set3.setDuration(3000); set3.setStartOffset(4500); TranslateAnimation set4 = new TranslateAnimation(0, 13, 0, -120); set4.setDuration(3000); set4.setStartOffset(6500); animSet.addAnimation(set1); animSet.addAnimation(set2); animSet.addAnimation(set3); animSet.addAnimation(set4); animSet.setFillAfter(true); ``` After creating a set of animations, I apply them on the `ImageView` like this: ``` image = (ImageView)findViewById(R.id.img); image.startAnimation(animSet); ``` Everything is working fine, but I cannot pause the animation and resume on button click. How can I do that? I tried everything, but didn't succeed. Any idea how to do this? Please help!

Original source

Related problems