Android animation does not officially provide the pause resume method.

Source: Internet
Author: User

This article mainly comes from: http://stackoverflow.com/questions/5828423/android-animation-pause-and-play-problem

Http://stackoverflow.com/questions/10006736/pause-and-resume-translate-animation

Android animation has been introduced in many articles.

Two instructions on Android Animation:

1. The animation we see is that the background resource of the control is floating, and the actual position of the control is not changed (you can verify it by adding a listener ).

2. The official website does not provide direct pause () and resume () excuses, but there are two existing excuses: setstarttime () and getstarttime (). Let's start from here.

The following describes a method:

Package COM. ideal. study; import android. view. animation. transformation; import android. view. animation. pause, resumepublic class translateanim extends {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; @ overridepublic Boolean gettransformation (long currenttime, transformation outtransformation) {If (mpaused & melapsedatpause = 0) {melapsedatpause = currenttime-getstarttime () ;}if (mpaused) setstarttime (currenttime-melapsedatpause); return. gettransformation (currenttime, outtransformation);} public void pause () {melapsedatpause = 0; mpaused = true;} public void resume () {mpaused = false ;}}

To call the pause () and resume () methods, you only need to create a new translateanim class.

The following is 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;    }}

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.