动画

2020-12-28  本文已影响0人  HOLLE_karry
import android.animation.Animator.AnimatorListener;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.animation.ValueAnimator;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.LinearInterpolator;
import android.view.animation.TranslateAnimation;

public class AnimationUtil {
    /**
     * 播放放大动画
     * @param view
     */
    public static AnimatorSet startScaleToBigAnimation(final View view, float rate, AnimatorListener animatorListener) {
        if (null != view.getAnimation()) {

            view.getAnimation().cancel();
        }
        view.clearAnimation();
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, rate);
        oa.setDuration(240);
        oa.setInterpolator(new DecelerateInterpolator());

        ObjectAnimator oa1 = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, rate);
        oa1.setDuration(240);
        oa1.setInterpolator(new DecelerateInterpolator());
        set.playTogether(oa, oa1);
        if (null != animatorListener) {
            set.addListener(animatorListener);
        }
        set.start();
        return set;
    }

    public static AnimatorSet startScaleToBigAnimation(final View view, float rate, AnimatorListener animatorListener, long duration) {
        if (null != view.getAnimation()) {
            view.getAnimation().cancel();
        }
        view.clearAnimation();
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "scaleY", 1.0f, rate);
        oa.setDuration(duration);
        oa.setInterpolator(new DecelerateInterpolator());
        ObjectAnimator oa1 = ObjectAnimator.ofFloat(view, "scaleX", 1.0f, rate);
        oa1.setDuration(duration);
        oa1.setInterpolator(new DecelerateInterpolator());
        set.playTogether(oa, oa1);
        if (null != animatorListener) {
            set.addListener(animatorListener);
        }
        set.start();
        return set;
    }

    /**
     * 播放缩小动画
     * @param view
     */
    public static AnimatorSet startScaleToSmallAnimation(View view, float rate, AnimatorListener animatorListener) {
        if (null != view.getAnimation()) {
            view.getAnimation().cancel();
        }
        view.clearAnimation();
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "scaleY", rate, 1.0f);
        oa.setDuration(140);
        oa.setInterpolator(new DecelerateInterpolator());
        ObjectAnimator oa1 = ObjectAnimator.ofFloat(view, "scaleX", rate, 1.0f);
        oa1.setDuration(140);
        oa1.setInterpolator(new DecelerateInterpolator());
        set.playTogether(oa, oa1);
        set.start();
        return set;
    }

    /**
     * 播放缩小动画
     * @param view
     */
    public static AnimatorSet startScaleToSmallAnimation(View view, float rate, int time, AnimatorListener animatorListener) {
        if (null != view.getAnimation()) {
            view.getAnimation().cancel();
        }
        view.clearAnimation();
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "scaleY", rate, 0.9f,1.1f,1.0f);
        oa.setDuration(time);
        oa.setInterpolator(new LinearInterpolator());
        ObjectAnimator oa1 = ObjectAnimator.ofFloat(view, "scaleX", rate, 0.9f,1.1f,1.0f);
        oa1.setDuration(time);
        oa1.setInterpolator(new LinearInterpolator());
        set.playTogether(oa, oa1);
        set.start();
        return set;
    }

    public static AnimatorSet startScaleToSmallAnimation(View view, float rate, AnimatorListener animatorListener, long duration) {
        if (null != view.getAnimation()) {
            view.getAnimation().cancel();
        }
        view.clearAnimation();
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "scaleY", rate, 1.0f);
        oa.setDuration(duration);
        oa.setInterpolator(new DecelerateInterpolator());
        ObjectAnimator oa1 = ObjectAnimator.ofFloat(view, "scaleX", rate, 1.0f);
        oa1.setDuration(duration);
        oa1.setInterpolator(new DecelerateInterpolator());
        set.playTogether(oa, oa1);
        set.start();
        return set;
    }

    public static void startRotateAnimation(View v, int duration) {
        ObjectAnimator oa = ObjectAnimator.ofFloat(v, "rotation", 0f, 360f);
        oa.setDuration(duration);
        oa.setInterpolator(new LinearInterpolator());
        oa.setRepeatCount(ValueAnimator.INFINITE);
        oa.start();
    }

    public static void setAnimator(View v, int duration, AnimatorListener listener) {
        final ObjectAnimator animator = ObjectAnimator.ofFloat(v, "rotation", 0f, 360f);
        LinearInterpolator lin = new LinearInterpolator();
        animator.setInterpolator(lin);
        animator.addListener(listener);
        animator.setDuration(duration);
        animator.start();
    }

    public static void clearAnimation(View v) {
        if (null == v) {
            return;
        }

        if (null != v.getAnimation()) {
            v.getAnimation().cancel();
            v.clearAnimation();
        }
    }

    /**
     * 向上移动动画
     * @param view
     * @return
     */
    public static AnimatorSet startTranlateUpAnimation(final View view, final float start, final float end, final int duration, int delayTime) {
        if (null != view.getAnimation()) {
            view.getAnimation().cancel();
        }
        view.clearAnimation();
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "translationY", start, end);
        oa.setDuration(duration);
        oa.setInterpolator(new DecelerateInterpolator());
        set.play(oa);
        set.start();
        set.setStartDelay(delayTime);
        return set;
    }

    /**
     * 向下移动动画
     * @param view
     * @return
     */
    public static AnimatorSet startTranlateDownAnimation(final View view, final float start, final float end, final int duration, int delayTime) {
        if (null != view.getAnimation()) {
            view.getAnimation().cancel();
        }
        view.clearAnimation();
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "translationY", start, end);
        oa.setDuration(duration);
        oa.setInterpolator(new DecelerateInterpolator());
        set.play(oa);
        set.setStartDelay(delayTime);
        set.start();
        return set;
    }

    /**
     * X轴移动动画
     */
    public static AnimatorSet startTranlateRightAnimation(final View view, final float start, final float end, final int duration) {
        if (null != view.getAnimation()) {
            view.getAnimation().cancel();
        }
        view.clearAnimation();
        AnimatorSet set = new AnimatorSet();
        ObjectAnimator oa = ObjectAnimator.ofFloat(view, "translationX", start, end);
        oa.setDuration(duration);
        oa.setInterpolator(new LinearInterpolator());
        set.play(oa);
        set.start();
        return set;
    }

    public static void upAnimation(final View view, final float y, final int duration) {
        TranslateAnimation animShow = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, y,
                Animation.RELATIVE_TO_SELF, 0);
        animShow.setDuration(duration);
        view.startAnimation(animShow);
    }

    public static TranslateAnimation downAnimation(final View view, final float y, final int duration) {
        TranslateAnimation animHide = new TranslateAnimation(
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, 0,
                Animation.RELATIVE_TO_SELF, y);
        animHide.setDuration(duration);
        view.startAnimation(animHide);
        return animHide;
    }
}

上一篇下一篇

猜你喜欢

热点阅读