Android TimeAnimator低于API级别16

时间:2015-12-28 18:00:19

标签: android animation android-animation

由于TimeAnimator是ValueAnimator的子类,有没有办法构建ValueAnimator的子类,其执行类似于API级别16的TimeAnimator?或者是否有其他方法在API级别16下使用TimeAnimator?我想要的只是计算无限和正确的时间。

1 个答案:

答案 0 :(得分:0)

我有个主意,而且有效!

public class TimeAnimatorCompat implements  ValueAnimator.AnimatorUpdateListener{
    protected ValueAnimator animator;
    protected long currentPlayTime = 0;
    protected TimeListenerCompat listener;
    private boolean ignoreNextUpdate = false;
    public TimeAnimatorCompat(){
        animator = ValueAnimator.ofInt(0, Integer.MAX_VALUE);
        animator.addUpdateListener(this);
        animator.setDuration(Integer.MAX_VALUE);
    }

    public void setCurrentPlayTime(long t){
        currentPlayTime = t;
    }

    public long getCurrentPlayTime(){
        return currentPlayTime;
    }

    public void start(){
        currentPlayTime = 0;
        animator.start();
    }

    public TimeListenerCompat getListener() {
        return listener;
    }

    public void setTimeListener(TimeListenerCompat listener) {
        this.listener = listener;
    }

    @Override
    public void onAnimationUpdate(ValueAnimator animation) {
        if(ignoreNextUpdate){
            ignoreNextUpdate = false;
            return;
        }
        long deltaTime = animation.getCurrentPlayTime();
        currentPlayTime += deltaTime;
        ignoreNextUpdate = true;
        animation.setCurrentPlayTime(0);
        if(listener != null){
            listener.onTimeUpdate(this, currentPlayTime, deltaTime);
        }
    }

    public void cancel(){
        animator.cancel();
    }

    public void pause(){
        animator.cancel();
    }

    public void resume(){
        animator.start();
    }

    public void end(){
        animator.end();
    }

    public interface TimeListenerCompat{
        void onTimeUpdate(TimeAnimatorCompat animation, long totalTime, long deltaTime);
    }
}