How can i pause and restart an animations in Android?
-
Can anyone please tell me how to pause and restart an animations in Android. Animation XML is: <?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false"> <translate android:fromXDelta="500%" android:toXDelta="0%" android:fromYDelta="0%" android:toYDelta="0%" android:duration="10000" android:fillAfter="true" android:repeatCount="infinite"/> </set> And the code is: Animation am=AnimationUtils.loadAnimation(this, R.anim.note); music1.startAnimation(am);
-
Answer:
There's no supporting API for pausing/resuming animations in API level 11. The features have been added in API level 19. What you need to do is subclass the animation type you'd like to use and then add methods for pausing and resuming the animation. Here is an example for AlphaAnimation: 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; } } This will keep increasing your starttime while the animation is paused, effectively keeping it from finishing and keeping it's state where it was when you paused. Hope it helps someone.
athulya k at Stack Overflow Visit the source
Other answers
and after start your animation you have only call pause and resume methods. 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; }
Shivansh Saxena
Related Q & A:
- How can I implement idle timeout in android?Best solution by Stack Overflow
- How can I access my localhost from my Android device?Best solution by Stack Overflow
- How can I put element beside another? (Android?Best solution by stackoverflow.com
- How can I capture the raw audio stream on an android device?Best solution by stackoverflow.com
- How can I restart a yahoo account?Best solution by Yahoo! Answers
Just Added Q & A:
- How many active mobile subscribers are there in China?Best solution by Quora
- How to find the right vacation?Best solution by bookit.com
- How To Make Your Own Primer?Best solution by thekrazycouponlady.com
- How do you get the domain & range?Best solution by ChaCha
- How do you open pop up blockers?Best solution by Yahoo! Answers
For every problem there is a solution! Proved by Solucija.
-
Got an issue and looking for advice?
-
Ask Solucija to search every corner of the Web for help.
-
Get workable solutions and helpful tips in a moment.
Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.