悬浮按钮打开姿势

作者: Ad大成 | 来源:发表于2019-10-08 14:58 被阅读0次

    打开动画工具类代码

    
    /**
     * 对 ViewAnimationUtils.createCircularReveal() 方法的封装.
     * <p/>
     * Created on 16/7/20.
     * GitHub: https://github.com/XunMengWinter
     *
     * @author ice
     */
    public class CircularAnimUtil {
    
        public static final long PERFECT_MILLS = 618;
        public static final int MINI_RADIUS = 0;
    
        /**
         * 向四周伸张,直到完成显示。
         */
        @SuppressLint("NewApi")
        public static void show(View myView, float startRadius, long durationMills) {
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
                myView.setVisibility(View.VISIBLE);
                return;
            }
    
            int cx = (myView.getLeft() + myView.getRight()) / 2;
            int cy = (myView.getTop() + myView.getBottom()) / 2;
    
            int w = myView.getWidth();
            int h = myView.getHeight();
    
            // 勾股定理 & 进一法
            int finalRadius = (int) Math.sqrt(w * w + h * h) + 1;
    
            Animator anim =
                    ViewAnimationUtils.createCircularReveal(myView, cx, cy, startRadius, finalRadius);
            myView.setVisibility(View.VISIBLE);
            anim.setDuration(durationMills);
            anim.start();
        }
    
        /**
         * 由满向中间收缩,直到隐藏。
         */
        @SuppressLint("NewApi")
        public static void hide(final View myView, float endRadius, long durationMills) {
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
                myView.setVisibility(View.INVISIBLE);
                return;
            }
    
            int cx = (myView.getLeft() + myView.getRight()) / 2;
            int cy = (myView.getTop() + myView.getBottom()) / 2;
            int w = myView.getWidth();
            int h = myView.getHeight();
    
            // 勾股定理 & 进一法
            int initialRadius = (int) Math.sqrt(w * w + h * h) + 1;
    
            Animator anim =
                    ViewAnimationUtils.createCircularReveal(myView, cx, cy, initialRadius, endRadius);
            anim.setDuration(durationMills);
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    myView.setVisibility(View.INVISIBLE);
                }
            });
    
            anim.start();
        }
    
        /**
         * 从指定View开始向四周伸张(伸张颜色或图片为colorOrImageRes), 然后进入另一个Activity,
         * 返回至 @thisActivity 后显示收缩动画。
         */
        @SuppressLint("NewApi")
        public static void startActivityForResult(
                final Activity thisActivity, final Intent intent, final Integer requestCode, final Bundle bundle,
                final View triggerView, int colorOrImageRes, long durationMills) {
    
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
                if(requestCode != null){
                    thisActivity.startActivityForResult(intent,requestCode);
                }else{
                    thisActivity.startActivity(intent);
                }
                return;
            }
    
            int[] location = new int[2];
            triggerView.getLocationInWindow(location);
            final int cx = location[0] + triggerView.getWidth() / 2;
            final int cy = location[1] + triggerView.getHeight() / 2;
            final ImageView view = new ImageView(thisActivity);
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setImageResource(colorOrImageRes);
            final ViewGroup decorView = (ViewGroup) thisActivity.getWindow().getDecorView();
            int w = decorView.getWidth();
            int h = decorView.getHeight();
            decorView.addView(view, w, h);
    
            // 计算中心点至view边界的最大距离
            int maxW = Math.max(cx, w - cx);
            int maxH = Math.max(cy, h - cy);
            final int finalRadius = (int) Math.sqrt(maxW * maxW + maxH * maxH) + 1;
            Animator
                    anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
            int maxRadius = (int) Math.sqrt(w * w + h * h) + 1;
            // 若使用默认时长,则需要根据水波扩散的距离来计算实际时间
            if (durationMills == PERFECT_MILLS) {
                // 算出实际边距与最大边距的比率
                double rate = 1d * finalRadius / maxRadius;
                // 水波扩散的距离与扩散时间成正比
                durationMills = (long) (PERFECT_MILLS * rate);
            }
            final long finalDuration = durationMills;
            anim.setDuration(finalDuration);
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
    
                    if (requestCode == null)
                        thisActivity.startActivity(intent);
                    else if (bundle == null)
                        thisActivity.startActivityForResult(intent, requestCode);
                    else
                        thisActivity.startActivityForResult(intent, requestCode, bundle);
    
                    // 默认渐隐过渡动画.
                    thisActivity.overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    
                    // 默认显示返回至当前Activity的动画.
                    triggerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Animator anim =
                                    ViewAnimationUtils.createCircularReveal(view, cx, cy, finalRadius, 0);
                            anim.setDuration(finalDuration);
                            anim.addListener(new AnimatorListenerAdapter() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    super.onAnimationEnd(animation);
                                    try {
                                        decorView.removeView(view);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                            anim.start();
                        }
                    }, 1000);
    
                }
            });
            anim.start();
        }
    
    
        @SuppressLint("NewApi")
        public static void fragmentStartActivityForResult(
                final Fragment thisActivity, final Intent intent, final Integer requestCode, final Bundle bundle,
                final View triggerView, int colorOrImageRes, long durationMills) {
    
            if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
                if(requestCode != null){
                    thisActivity.startActivityForResult(intent,requestCode);
                }else{
                    thisActivity.startActivity(intent);
                }
                return;
            }
    
            int[] location = new int[2];
            triggerView.getLocationInWindow(location);
            final int cx = location[0] + triggerView.getWidth() / 2;
            final int cy = location[1] + triggerView.getHeight() / 2;
            final ImageView view = new ImageView(thisActivity.getContext());
            view.setScaleType(ImageView.ScaleType.CENTER_CROP);
            view.setImageResource(colorOrImageRes);
            final ViewGroup decorView = (ViewGroup) thisActivity.getActivity().getWindow().getDecorView();
            int w = decorView.getWidth();
            int h = decorView.getHeight();
            decorView.addView(view, w, h);
    
            // 计算中心点至view边界的最大距离
            int maxW = Math.max(cx, w - cx);
            int maxH = Math.max(cy, h - cy);
            final int finalRadius = (int) Math.sqrt(maxW * maxW + maxH * maxH) + 1;
            Animator
                    anim = ViewAnimationUtils.createCircularReveal(view, cx, cy, 0, finalRadius);
            int maxRadius = (int) Math.sqrt(w * w + h * h) + 1;
            // 若使用默认时长,则需要根据水波扩散的距离来计算实际时间
            if (durationMills == PERFECT_MILLS) {
                // 算出实际边距与最大边距的比率
                double rate = 1d * finalRadius / maxRadius;
                // 水波扩散的距离与扩散时间成正比
                durationMills = (long) (PERFECT_MILLS * rate);
            }
            final long finalDuration = durationMills;
            anim.setDuration(finalDuration);
            anim.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
    
                    if (requestCode == null)
                        thisActivity.startActivity(intent);
                    else if (bundle == null)
                        thisActivity.startActivityForResult(intent, requestCode);
                    else
                        thisActivity.startActivityForResult(intent, requestCode, bundle);
    
                    // 默认渐隐过渡动画.
                    thisActivity.getActivity().overridePendingTransition(android.R.anim.fade_in, android.R.anim.fade_out);
    
                    // 默认显示返回至当前Activity的动画.
                    triggerView.postDelayed(new Runnable() {
                        @Override
                        public void run() {
                            Animator anim =
                                    ViewAnimationUtils.createCircularReveal(view, cx, cy, finalRadius, 0);
                            anim.setDuration(finalDuration);
                            anim.addListener(new AnimatorListenerAdapter() {
                                @Override
                                public void onAnimationEnd(Animator animation) {
                                    super.onAnimationEnd(animation);
                                    try {
                                        decorView.removeView(view);
                                    } catch (Exception e) {
                                        e.printStackTrace();
                                    }
                                }
                            });
                            anim.start();
                        }
                    }, 1000);
    
                }
            });
            anim.start();
        }
    
    
        /*下面的方法全是重载,用简化上面方法的构建*/
    
    
        public static void startActivityForResult(
                Activity thisActivity, Intent intent, Integer requestCode, View triggerView, int colorOrImageRes) {
            startActivityForResult(thisActivity, intent, requestCode, null, triggerView, colorOrImageRes, PERFECT_MILLS);
        }
    
        public static void fragmentStartActivityForResult(
                Fragment thisActivity, Intent intent, Integer requestCode, View triggerView, int colorOrImageRes) {
            fragmentStartActivityForResult(thisActivity, intent, requestCode, null, triggerView, colorOrImageRes, PERFECT_MILLS);
        }
    
    
        public static void startActivity(
                Activity thisActivity, Intent intent, View triggerView, int colorOrImageRes, long durationMills) {
            startActivityForResult(thisActivity, intent, null, null, triggerView, colorOrImageRes, durationMills);
        }
    
        public static void startActivity(
                Activity thisActivity, Intent intent, View triggerView, int colorOrImageRes) {
            startActivity(thisActivity, intent, triggerView, colorOrImageRes, PERFECT_MILLS);
        }
    
        public static void startActivity(Activity thisActivity, Class<?> targetClass, View triggerView, int colorOrImageRes) {
            startActivity(thisActivity, new Intent(thisActivity, targetClass), triggerView, colorOrImageRes, PERFECT_MILLS);
        }
    
        public static void show(View myView) {
            show(myView, MINI_RADIUS, PERFECT_MILLS);
        }
    
        public static void hide(View myView) {
            hide(myView, MINI_RADIUS, PERFECT_MILLS);
        }
    
    

    启动方式代码

     fab.setOnClickListener(new View.OnClickListener() {
                @Override
                public void onClick(View v) {
    //                Intent intent = new Intent(getContext(), CalendarActivity.class);
    //                startActivityForResult(intent,2);
                    Snackbar.make(v, "Replace with your own action", Snackbar.LENGTH_LONG)
                            .setAction("Action", null).show();
                    Intent it = new Intent();
                    it.setClass(getActivity(), CalendarActivity.class);
                    CircularAnimUtil.fragmentStartActivityForResult(fragment,it,2,fab,R.color.colorAccent);
                }
            });
    

    相关文章

      网友评论

        本文标题:悬浮按钮打开姿势

        本文链接:https://www.haomeiwen.com/subject/sateyctx.html