Android Pause animation

2019-08-10 10:16发布

I want to stop animation once i click on the button..I mean that I want to pause animating a button when it is clicked and want to show an image in place of that button.I want to do this for api level > 11.I am using value animator for animation.Please help

2条回答
一夜七次
2楼-- · 2019-08-10 10:56

There's no supporting API for pausing/resuming animations in API level 11. The features have been added in API level 19.

However, here's a solution that you could try https://stackoverflow.com/a/7840895/3025732

查看更多
ゆ 、 Hurt°
3楼-- · 2019-08-10 11:09

What I did was create my own interpolatedTime value and ignore the one sent.

If the animation is paused, I keep updating my elapsed time. Then I caluclate my own interpolated time value and use that for whatever animation I am doing.

/**
 * 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;
    }
}
查看更多
登录 后发表回答