如何暂停动画一段时间? [英] How to pause animation for sometime?

查看:221
本文介绍了如何暂停动画一段时间?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

能否有人请告诉我如何暂停适用于动画android.In以下动画从屏幕底部的按钮移动到screen.I中心已经这样做了迄今为止使用低于code.However我想按钮,等待一段时间达到其最终位置(在屏幕的中心)之后。
以下是我的code。

 <?XML版本=1.0编码=UTF-8&GT?;
<设置的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android>
    <翻译
    机器人:fromXDelta =400%
    机器人:toXDelta = - 200%
    安卓的repeatCount =无限
    机器人:时间=2000
    机器人:fillAfter =真
    />
< /集>

在Java的:

 动画= AnimationUtils.loadAnimation(这一点,R.anim.alpha);
a.reset();
image.clearAnimation();
image.startAnimation(一);


解决方案

您需要做的就是继承你想使用,然后添加方法暂停和恢复动画的动画类型。下面是AlphaAnimation一个例子:

 公共类PausableAlphaAnimation扩展AlphaAnimation {    私人长期mElapsedAtPause = 0;
    私人布尔mPaused = FALSE;    公共PausableAlphaAnimation(浮动fromAlpha,浮toAlpha){
        超(fromAlpha,toAlpha);
    }    @覆盖
    公共布尔getTransformation(长currentTime的,转型outTransformation){
        如果(mPaused&安培;&安培; mElapsedAtPause == 0){
            mElapsedAtPause = currentTime的-getStartTime();
        }
        如果(mPaused)
            setStartTime(currentTime的-mElapsedAtPause);
        返回super.getTransformation(currentTime的,outTransformation);
    }    公共无效暂停(){
        mElapsedAtPause = 0;
        mPaused = TRUE;
    }    公共无效简历(){
        mPaused = FALSE;
    }
}

这将让您的增加而开始时间在动画暂停时,有效​​地将其与整理,并保持它的状态,当你暂停它。

希望可以帮助你。

Can somebody please tell me how to apply a pause to animation in android.In the following animation a button moves from the bottom of the screen to the center of the screen.I have done this so far using the below code.However i want the button to wait for some time after it has reached its final position(At the center of the screen). Following is my code.

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
    android:fromXDelta="400%"
    android:toXDelta="-200%"
    android:repeatCount="infinite"
    android:duration="2000"
    android:fillAfter="true"
    />
</set>

In java:

Animation a = AnimationUtils.loadAnimation(this, R.anim.alpha);
a.reset();
image.clearAnimation();
image.startAnimation(a);

解决方案

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 helps you.

这篇关于如何暂停动画一段时间?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆