package com.jld.vod.ui.animator;

import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.content.Context;
import android.os.Build;
import android.view.animation.LinearInterpolator;
import android.widget.ImageView;
import android.widget.RelativeLayout;

/**
 * 播放动画
 */

public final class HomeRatateImage {
    private final ValueAnimator rotateAnim;
    private boolean isSpin = false;

    public HomeRatateImage(Context context, final ImageView view) {
        rotateAnim = ObjectAnimator.ofFloat(0, 90);
        rotateAnim.setDuration(2 * 1000);
        rotateAnim.setRepeatMode(ValueAnimator.RESTART);
        rotateAnim.setRepeatCount(0);
        rotateAnim.setInterpolator(new LinearInterpolator());

        view.setRotation(0.0f);
        rotateAnim.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                view.setRotation(value);
            }
        });
    }

    public void startSpin() {

        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {

            if (rotateAnim.isPaused()) {
                rotateAnim.resume();
            } else {
                rotateAnim.start();
            }
        } else {
            rotateAnim.start();
        }

        isSpin = true;
    }

    public void stopSpin() {
        rotateAnim.reverse();
        if (rotateAnim.isRunning()) {
//            rotateAnim.resume();
//            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.KITKAT) {
//                rotateAnim.pause();
//            } else {
//                rotateAnim.cancel();
//            }
            isSpin = false;
        }
    }

    public boolean isSpin() {
        return isSpin;
    }

}
