【问题标题】:Android Pause animationAndroid 暂停动画
【发布时间】:2013-12-02 14:26:09
【问题描述】:

我想在单击按钮后停止动画..我的意思是我想在单击按钮时暂停按钮的动画并希望显示图像来代替该按钮。我想为 api 级别执行此操作> 11.我正在使用 value animator 进行动画制作。请帮助

【问题讨论】:

    标签: android animation


    【解决方案1】:

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

    不过,您可以尝试以下解决方案 https://stackoverflow.com/a/7840895/3025732

    【讨论】:

    • 实际上我正在使用 Value animator 制作动画,所以提供的链接对我没有用..无论如何谢谢
    【解决方案2】:

    我所做的是创建自己的 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;
        }
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-28
      • 2013-11-25
      • 2012-08-21
      • 2015-03-17
      • 1970-01-01
      • 2022-01-21
      • 2018-07-20
      • 2013-12-01
      • 1970-01-01
      相关资源
      最近更新 更多