Pause and Resume Translate Animation

后端 未结 2 1240
花落未央
花落未央 2020-12-18 14:44

I am using Translate Animation for moving an ImageView. I am using this code:

TranslateAnimation set1 = new TranslateAnimation(-4, 10, -110, 0);         


        
相关标签:
2条回答
  • 2020-12-18 15:17

    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. :)

    0 讨论(0)
  • 2020-12-18 15:33

    You can also do like this: а можно еще так:

    public class MyTranslateAnimation extends TranslateAnimation {
    
        private long mTimePause, mTimeTotal;
        private boolean mPause;
    
    
        public MyTranslateAnimation(Context context, AttributeSet attrs) {
    
            super(context, attrs);
    
        }
    
        @Override
        public boolean getTransformation(long currentTime, Transformation outTransformation) {
            updateTime(currentTime);
            return super.getTransformation(mTimeTotal - mTimePause, outTransformation);
        }
    
        private void updateTime(long currentTime) {
            long dt = currentTime - mTimeTotal;
            mTimeTotal += dt;
            if (mPause) {
                mTimePause += dt;
            }
        }
    
        public void pause() {
            mPause = true;
        }
    
        public void resume() {
            mPause = false;
        }
    
    }
    

    To create an animation from an XML, you can create your own AnimationUtils subclass, like this: для создания анимации из XML можно сделать свой AnimationUtils:

    public class MyAnimationUtils {
    
        public static Animation loadAnimation(Context context, int id) throws Resources.NotFoundException {
    
            XmlResourceParser parser = null;
            try {
                parser = context.getResources().getAnimation(id);
                return createAnimationFromXml(context, parser);
            } catch (XmlPullParserException ex) {
                Resources.NotFoundException rnf = new Resources.NotFoundException("Can't load animation resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(ex);
                throw rnf;
            } catch (IOException ex) {
                Resources.NotFoundException rnf = new Resources.NotFoundException("Can't load animation resource ID #0x" + Integer.toHexString(id));
                rnf.initCause(ex);
                throw rnf;
            } finally {
                if (parser != null) parser.close();
            }
    
        }
    
        private static Animation createAnimationFromXml(Context c, XmlPullParser parser) throws XmlPullParserException, IOException {
    
            return createAnimationFromXml(c, parser, null, Xml.asAttributeSet(parser));
    
        }
    
        private static Animation createAnimationFromXml(Context c, XmlPullParser parser, AnimationSet parent, AttributeSet attrs) throws XmlPullParserException, IOException {
    
            Animation anim = null;
    
            // Make sure we are on a start tag.
            int type;
            int depth = parser.getDepth();
    
            while (((type=parser.next()) != XmlPullParser.END_TAG || parser.getDepth() > depth)
                    && type != XmlPullParser.END_DOCUMENT) {
    
                if (type != XmlPullParser.START_TAG) {
                    continue;
                }
    
                String  name = parser.getName();
    
                if (name.equals("set")) {
                    anim = new AnimationSet(c, attrs);
                    createAnimationFromXml(c, parser, (AnimationSet)anim, attrs);
                } else if (name.equals("alpha")) {
                    anim = new AlphaAnimation(c, attrs);
                } else if (name.equals("scale")) {
                    anim = new ScaleAnimation(c, attrs);
                }  else if (name.equals("rotate")) {
                    anim = new RotateAnimation(c, attrs);
                }  else if (name.equals("translate")) {
                    //anim = new TranslateAnimation(c, attrs);
                    anim = new MyTranslateAnimation(c, attrs); // отредактировали только эту строчку, остальное взяли как было
                } else {
                    throw new RuntimeException("Unknown animation name: " + parser.getName());
                }
    
                if (parent != null) {
                    parent.addAnimation(anim);
                }
            }
    
            return anim;
    
        }
    
    }
    

    And then you build the animation like this: и вот так создаем анимацию:

    MyTranslateAnimation cloud1 = (MyTranslateAnimation) MyAnimationUtils.loadAnimation(this, R.anim.main_cloud1);
    

    Hope this helps. Пользуйтесь на здоровье!

    0 讨论(0)
提交回复
热议问题