暂停和恢复翻译动画

时间:2012-04-04 07:31:21

标签: java android android-animation

我正在使用翻译动画来移动ImageView。我正在使用此代码:

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);

创建一组动画后,我将它们应用于ImageView,如下所示:

image = (ImageView)findViewById(R.id.img);
        image.startAnimation(animSet);

一切正常,但我无法暂停动画并点击按钮继续

我该怎么做?

我尝试了一切,但没有成功。知道怎么做吗?

请帮忙!

2 个答案:

答案 0 :(得分:7)

在搜索了一段时间之后,我发现了这个link并检查这是否适用于翻译动画,经过一些修改后,这也适用于你的动画。!

请参阅以下修改后的代码:

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

我只会更改类名,扩展此类的类名和构造函数。

您可以像:

一样使用它
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);

在开始动画后,你只有呼叫暂停和恢复方法。 感谢Johan与我们分享他的代码。

希望这可以解决您的问题。 :)

答案 1 :(得分:-1)

你也可以这样做:аможноещетак:

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

}

要从XML创建动画,您可以创建自己的AnimationUtils子类,如下所示:длясозданияанимацииизXMLможносделатьсвойDynamicUtils:

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;

    }

}

然后你构建这样的动画:ивоттаксоздаеманимацию:

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

希望这会有所帮助。 Пользуйтесьназдоровье!