Android暂停动画

时间:2013-12-02 14:26:10

标签: android animation

我想在点击按钮后停止动画。我的意思是我想在点击按钮时暂停动画按钮并想要显示图像来代替该按钮。我想为api级别执行此操作>我正在使用价值动画制作动画。请帮忙

2 个答案:

答案 0 :(得分:2)

在API级别11中没有用于暂停/恢复动画的支持API。这些功能已在API级别19中添加。

但是,这是一个可以尝试https://stackoverflow.com/a/7840895/3025732

的解决方案

答案 1 :(得分:0)

我所做的是创建自己的interpolatedTime值并忽略发送的值。

如果动画暂停,我会不断更新已用时间。然后我计算我自己的插值时间值,并将其用于我正在做的任何动画。

/**
 * The animator for the LinearProgressBar.
 */
class LinearProgressBarAnimator extends Animation
{
    private LinearProgressBar _bar;

    /**
     * The desired fill percent when done animating.
     */
    private float _newFillPercent;

    /**
     * The current fill percent.
     */
    private float _oldFillPercent;

    /**
     * Keeps track of if this object is in a paused state.
     */
    private boolean _paused;

    /**
     * TimeConstants trackers to fire _onSecondTick
     * at consistent 1 second intervals.
     */
    private long _accumulatedTickTime;
    private long _lastAccumulatedTime;

    /**
     * Creates a linear progress bar animator with the reference bar and desired fill percent.
     * Call Animate() in order to make it animate the ProgressBar.
     * @param bar
     * @param newFillPercent
     */
    public LinearProgressBarAnimator(LinearProgressBar bar, float newFillPercent)
    {
        super();

        _newFillPercent      = newFillPercent;
        _oldFillPercent      = bar.getFillPercent();
        _bar                 = bar;
        _paused              = false;
        _accumulatedTickTime = 0;
        _lastAccumulatedTime = TimeConstants.NOT_SET;
    }

    /**
     * Applies the interpolated change in fill percent.
     * @param interpolatedTime
     * @param transformation
     */
    @Override
    protected void applyTransformation(float interpolatedTime, Transformation transformation)
    {
        if (_lastAccumulatedTime == TimeConstants.NOT_SET || _paused)
        {
            _lastAccumulatedTime = System.currentTimeMillis();
            return;
        }

        _accumulatedTickTime += System.currentTimeMillis() - _lastAccumulatedTime;

        // This handles pausing, the passed interpolatedTime is ignored
        float adjustedInterpolatedTimed = (float)_accumulatedTickTime / (float)getDuration();
        float newTimeAdjustedFillPercent = _oldFillPercent + ((_newFillPercent - _oldFillPercent) * adjustedInterpolatedTimed);
        _bar.setFillPercent(newTimeAdjustedFillPercent);
        _bar.requestLayout();

        _lastAccumulatedTime = System.currentTimeMillis();
    }

    public void pause()
    {
        _paused = true;
    }

    public void play()
    {
        _paused = false;
    }
}