ButtonAnimUtil.java 8.7 KB
package com.cnlive.strike.xiaojiaspeaker;

import android.animation.Animator;
import android.animation.AnimatorSet;
import android.animation.ObjectAnimator;
import android.os.Build;
import android.os.Handler;
import android.view.View;
import android.view.ViewAnimationUtils;
import android.view.animation.DecelerateInterpolator;

/**
 * 圆形按钮点击特效
 *
 * @author ShinnyYang
 */
public class ButtonAnimUtil {
    public static final int DEFAULT_ZOOM_DURATION = 50;//默认缩放动画时间间隔
    public static final int SHOW_VIEW_ZOOM_DURATION = 150;//显示控件缩放动画时间间隔
    public static final int HIDE_VIEW_ZOOM_DURATION = 100;//隐藏控件缩放动画时间间隔

    //设置控件放大动画
    public static void setViewZoomIn(View view) {
        try {
            AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1, 1.2f);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1, 1.2f);
            animatorSetsuofang.setDuration(DEFAULT_ZOOM_DURATION);
            animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
            animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
            animatorSetsuofang.start();
        } catch (Exception e) {
        }
    }

    //设置控件缩小动画
    public static void setViewZoomOut(View view) {
        try {
            AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1.2f, 1);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1.2f, 1);
            animatorSetsuofang.setDuration(DEFAULT_ZOOM_DURATION);
            animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
            animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
            animatorSetsuofang.start();
        } catch (Exception e) {
        }
    }

    //设置控件缩小动画
    public static void setViewDefault(View view) {
        try {
            AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1f, 1);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1f, 1);
            animatorSetsuofang.setDuration(DEFAULT_ZOOM_DURATION);
            animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
            animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
            animatorSetsuofang.start();
        } catch (Exception e) {
        }
    }

    //设置控件显示放大动画
    public static void setViewShowZoomIn(View view) {
        try {
            AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0, 1.2f);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0, 1.2f);
            animatorSetsuofang.setDuration(SHOW_VIEW_ZOOM_DURATION);
            animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
            animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
            animatorSetsuofang.start();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (view == null) {
                        return;
                    }
                    AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
                    ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1.2f, 1);
                    ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1.2f, 1);
                    animatorSetsuofang.setDuration(SHOW_VIEW_ZOOM_DURATION);
                    animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
                    animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
                    animatorSetsuofang.start();
                }
            }, SHOW_VIEW_ZOOM_DURATION);
        } catch (Exception e) {
        }
    }

    //设置控件隐藏缩小动画
    public static void setViewHideZoomOut(View view) {
        try {
            AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 1, 0);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 1, 0);
            animatorSetsuofang.setDuration(HIDE_VIEW_ZOOM_DURATION);
            animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
            animatorSetsuofang.play(scaleX).with(scaleY);//两个动画同时开始
            animatorSetsuofang.start();
        } catch (Exception e) {
        }
    }

    //设置时间显示放大动画
    public static void setTimeShowZoomIn(View view) {
        try {
            ObjectAnimator scaleX = ObjectAnimator.ofFloat(view, "scaleX", 0, 1.4f);
            ObjectAnimator scaleY = ObjectAnimator.ofFloat(view, "scaleY", 0, 1.4f);
            ObjectAnimator scaleX1 = ObjectAnimator.ofFloat(view, "scaleX", 1.4f, 1);
            ObjectAnimator scaleY1 = ObjectAnimator.ofFloat(view, "scaleY", 1.4f, 1);
            ObjectAnimator scaleX2 = ObjectAnimator.ofFloat(view, "scaleX", 1, 0);
            ObjectAnimator scaleY2 = ObjectAnimator.ofFloat(view, "scaleY", 1, 0);
            AnimatorSet animatorSetsuofang = new AnimatorSet();//组合动画
            animatorSetsuofang.setDuration(100);
            animatorSetsuofang.setInterpolator(new DecelerateInterpolator());
            animatorSetsuofang.play(scaleX).with(scaleY);
            animatorSetsuofang.start();
            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (view == null) {
                        return;
                    }
                    AnimatorSet animatorSetsuofang2 = new AnimatorSet();//组合动画
                    animatorSetsuofang2.setDuration(100);
                    animatorSetsuofang2.setInterpolator(new DecelerateInterpolator());
                    animatorSetsuofang2.play(scaleX1).with(scaleY1);
                    animatorSetsuofang2.start();
                }
            }, 100);

            new Handler().postDelayed(new Runnable() {
                @Override
                public void run() {
                    if (view == null) {
                        return;
                    }
                    AnimatorSet animatorSetsuofang3 = new AnimatorSet();//组合动画
                    animatorSetsuofang3.setDuration(10);
                    animatorSetsuofang3.setInterpolator(new DecelerateInterpolator());
                    animatorSetsuofang3.play(scaleX2).with(scaleY2);
                    animatorSetsuofang3.start();
                }
            }, 300);
        } catch (Exception e) {
        }
    }


    /**
     * 设置控件展开特效
     *
     * @param view
     * @param closeFlag
     */
    public static void startAnimation(View view, boolean closeFlag) {
        try {
            //因为CircularReveal动画是api21之后才有的,所以加个判断语句,免得崩溃
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                int cicular_R = view.getHeight() / 2 > view.getWidth() / 2 ? view.getHeight() / 2 : view.getWidth() / 2;
                Animator animator = null;
                if (!closeFlag) {
                    animator = ViewAnimationUtils.createCircularReveal(view, (int) view.getWidth() / 2, (int) view.getHeight() / 2, 0, cicular_R);
                } else {
                    animator = ViewAnimationUtils.createCircularReveal(view, (int) view.getWidth() / 2, (int) view.getHeight() / 2, cicular_R, 0);
                }
                animator.addListener(new Animator.AnimatorListener() {
                    @Override
                    public void onAnimationStart(Animator animator) {

                    }

                    @Override
                    public void onAnimationEnd(Animator animator) {
                        if (closeFlag) {
                            view.setVisibility(View.GONE);
                        }
                    }

                    @Override
                    public void onAnimationCancel(Animator animator) {

                    }

                    @Override
                    public void onAnimationRepeat(Animator animator) {

                    }
                });
                animator.setDuration(350);
                animator.start();
            } else {
                if (closeFlag) {
                    view.setVisibility(View.GONE);
                }
            }

        } catch (Exception e) {
        }
    }

}