0

I have a circular layout and there are "n" buttons in this layout. I start the animation on that layout when the activity starts.

When I click on any button the animation should stop and a dialog appear with the message, "You have clicked this 'XYZ' button".

The code I am using:

    animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
    animation.setFillEnabled(true);
    animation.setFillAfter(true);
    findViewById(R.id.circle_layout).startAnimation(animation);

and the animation XML:

<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:duration="15000"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="infinite"
android:toDegrees="360" >

4

3 回答 3

1

There is no pause as posted. But you can mimic it.

This answer worked for me: How do I pause frame animation using AnimationDrawable?

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

Please note: this does not technically 'pause' the animation because it keeps continuously calling the transformation. But can keeps a persistent transformation that 'mimics' the same functionality.

I tried this with a RotateAnimation and worked just fine. But it will not lower the CPU/framerate when it is 'paused' as it does when you cancel the animation.

于 2014-04-03T21:32:28.420 回答
1

android中没有pause动画。我在 StackOverflow 上检查了许多与此相关的问题,但没有运气。您仍然可以尝试使用此链接Activity暂停自身,这可能会有所帮助。该链接指出以下内容:

暂停您的活动

当系统为您的 Activity 调用 onPause() 时,从技术上讲,这意味着您的 Activity 仍然部分可见,但通常表明用户正在离开该 Activity,并且它很快就会进入“已停止”状态。您通常应该使用 onPause() 回调来:

  1. 停止可能消耗 CPU 的动画或其他正在进行的操作。
  2. 提交未保存的更改,但前提是用户希望在他们离开时永久保存此类更改(例如草稿电子邮件)。
  3. 释放系统资源,例如广播接收器、传感器句柄(如 GPS),或在您的活动暂停且用户不需要它们时可能影响电池寿命的任何资源。
于 2013-04-19T06:00:35.710 回答
0

没有很好的方法可以在周期中暂停动画。

您可以继承 RotateAnimation 并拦截 currentTime 值,getTransformation并在您希望动画暂停时同时输入它。

如果你负担得起只支持 HC+,那么你应该考虑使用属性动画而不是视图动画。

于 2013-04-19T06:21:54.667 回答