DialogFragment 出场动画

DialogFragment 其实就是继承fragment,用Fragment的事物进行管理
首先在onCreate中指定DialogFragment 所对应的样式

setStyle(DialogFragment.STYLE_NO_FRAME, R.style.DialogStyle);

    

然后在onCreateView 中返回的是我们将要展示的布局

 rootView = View.inflate(getContext(), R.layout.dialog_fragment,null);

        final ViewTreeObserver viewTreeObserver = rootView.getViewTreeObserver();
        boolean alive = viewTreeObserver.isAlive();
 //        当一个视图树将要绘制时,所要调用的回调函数的接口类
 // 增加监听是为了,加入DialogFragment的出场动画
            viewTreeObserver.addOnPreDrawListener(new ViewTreeObserver.OnPreDrawListener() {
                @Override
                public boolean onPreDraw() {
//                    this 就表示的这个接口的匿名实现类
                    rootView.getViewTreeObserver().removeOnPreDrawListener(this);
                    System.out.println("MyDialogFragment onPreDraw 33333333333");
                    if (circleAnimal != null){
                        circleAnimal.show(rootView);
                    }
                    return true;
                }
            });

在onstart方法中设置DialogFragment的宽高,然后设置初始化动画对象

   @Override
    public void onStart() {
        super.onStart();
        System.out.println("MyDialogFragment onStart 44444444444444");
        ViewGroup.LayoutParams params = getDialog().getWindow().getAttributes();
        params.width = WindowManager.LayoutParams.MATCH_PARENT;
        params.height = WindowManager.LayoutParams.MATCH_PARENT;
        getDialog().getWindow().setAttributes((WindowManager.LayoutParams) params);

        //取消过渡动画 , 使DialogSearch的出现更加平滑
//        getDialog().getWindow().setWindowAnimations(R.style.DialogEmptyAnimation);
//      初始化动画对象,并且设置回调监听
        circleAnimal = new CircleAnimal();
        circleAnimal.setAnimListener(this);
    }

动画类

public class CircleAnimal {
    //duration 运行时间
    private static final long DURATION = 200;
    //动画监听
    private AnimListener mListener = null;
    private Animator anim;

    public interface AnimListener {
        //退出动画完成
        void onHideAnimationEnd();
        //出场动画完成
        void onShowAnimationEnd();
    }
    private void actionOtherVisible(boolean isShow,final View animView){
        int[] location = new int[2];
//        获取view在当前窗口内的绝对坐标
        animView.getLocationInWindow(location);
        System.out.println("location :" + location[0]);// X 0
        System.out.println("location :" + location[1]);// y 0
//计算中心位置
        int avX = location[0] + animView.getWidth() / 2;
        int avY = location[1] + animView.getHeight() / 2;
        System.out.println("location avX" + avX +" avY " +avY);
//这个是获取view在当前屏幕的高度,包含通知栏的高度也就是状态栏的高度
//        int[] locaton2 = new int[2];
//        animView.getLocationOnScreen(locaton2);
//        System.out.println("locaton2 :" + locaton2[0]);// x 0
//        System.out.println("locaton2 :" + locaton2[1]);// y 90


        float startRadius;
        float endRadius;
        // Math.sqrt 这个函数是用来计算平方根的
        float maxRadius = (float) Math.sqrt((double) (avX * avX + avY * avY));
        System.out.println("maxRadius :" + maxRadius);

        if (isShow) {
            startRadius = 0f;
            endRadius = maxRadius;
        } else {
            startRadius = maxRadius;
            endRadius = 0f;
        }
//        快速实现圆形动画缩放的api,5.0之后才引入  Reveal 显示,展示的意思
        anim = ViewAnimationUtils.createCircularReveal(animView,0,0,startRadius, 2160f);
        animView.setVisibility(View.VISIBLE);
        anim.setDuration(500);
        anim.setInterpolator(new DecelerateInterpolator());

        anim.start();
        anim.addListener(new AnimatorListenerAdapter() {
            @Override
            public void onAnimationEnd(Animator animation) {
//                animView.setVisibility(View.VISIBLE);
            }
        });
    }
    public void show(View rootView){
        actionOtherVisible(true,rootView);
    }
    public void hide(View rootView) {
        actionOtherVisible(false,rootView);
    }
    
//    设置对动画的监听
    public void setAnimListener(AnimListener listener) {
        mListener = listener;
    }
}

下面这个是玩Android 的动画类

public class CircularRevealAnim {
//duration 运行时间
    private static final long DURATION = 200;
//动画监听
    private AnimListener mListener = null;
    private Animator anim;

    public interface AnimListener {

        void onHideAnimationEnd();
//动画展示完成
        void onShowAnimationEnd();
    }

    @SuppressLint("NewApi")
    private void actionOtherVisible(Boolean isShow, View triggerView, View animView) {
//        triggerView 是所要获取焦点的EditTextView
//        Lollipop是21
        if (android.os.Build.VERSION.SDK_INT < android.os.Build.VERSION_CODES.LOLLIPOP) {
            if (isShow) {
//                animView 是整个rootView,
                animView.setVisibility(View.VISIBLE);
                if (mListener != null) {
                    anim.cancel();
                    anim = null;
                    mListener.onShowAnimationEnd();
                }
            } else {
                animView.setVisibility(View.GONE);
                if (mListener != null) {
                    anim.cancel();
                    anim = null;
                    mListener.onHideAnimationEnd();
                }
            }
            return;
        }

        /**
         * 计算 triggerView 的中心位置
         */
        int[] tvLocation = {0, 0};
        triggerView.getLocationInWindow(tvLocation);
//156--------12
        System.out.println("tvLocation :" + tvLocation[0] +"--------" +tvLocation[1]);
// 屏幕宽度的的0.8 倍
        int tvX = (int) (tvLocation[0] + animView.getWidth() * 0.8);
//        edittext 高度的一般
        int tvY = tvLocation[1] + triggerView.getHeight() / 2;
//        1002-----tvY84
        System.out.println("tvX :" + tvX +"-----tvY" +tvY);


        /**
         * 计算 animView 的中心位置
         */
        int[] avLocation = {0, 0};
        animView.getLocationInWindow(avLocation);
//        0--------0
        System.out.println("avLocation :" + avLocation[0] +"--------" +avLocation[1]);
        int avX = avLocation[0] + animView.getWidth() / 2;
        int avY = avLocation[1] + animView.getHeight() / 2;
//        : avX :529  avY :987
        System.out.println("avX :"+ avX + "  avY :"+ avY);
        int rippleW;
        if (tvX < avX) { //1002 < 529
             rippleW = animView.getWidth() - tvX;
        } else {
//            1002 - 0
             rippleW = tvX - avLocation[0];
        }
//      84
        int rippleH;
        if (tvY < avY) {//84 < 987
            rippleH = animView.getHeight() - tvY;
        } else {
            rippleH = tvY - avLocation[1];//84 -
        }
//        Math.sqrt(9)  返回一个正值的平方根 为3     1002*1002 +
        float maxRadius = (float) Math.sqrt((double) (rippleW * rippleW + rippleH * rippleH));
        float startRadius;
        float endRadius;

        if (isShow) {
            startRadius = 0f;
            endRadius = maxRadius;
        } else {
            startRadius = maxRadius;
            endRadius = 0f;
        }
//         tvX :1002 ;tvY :84  endRadius :2139.183
        System.out.println("tvX :" + tvX +" ;tvY :" +tvY + "  endRadius :" + endRadius);
        anim = ViewAnimationUtils.createCircularReveal(animView, tvX, tvY, startRadius, endRadius);
        animView.setVisibility(View.VISIBLE);
        anim.setDuration(DURATION);
        anim.setInterpolator(new DecelerateInterpolator());

        anim.addListener(new Animator.AnimatorListener() {
            @Override
            public void onAnimationStart(Animator animation) {

            }

            @Override
            public void onAnimationEnd(Animator animation) {
                if (isShow) {
                    animView.setVisibility(View.VISIBLE);
                    if (mListener != null) {
                        mListener.onShowAnimationEnd();
                    }
                } else {
                    animView.setVisibility(View.GONE);
                    if (mListener != null) {
                        mListener.onHideAnimationEnd();
                    }
                }
            }

            @Override
            public void onAnimationCancel(Animator animation) {

            }

            @Override
            public void onAnimationRepeat(Animator animation) {

            }
        });

        anim.start();
    }

    public void show(View triggerView, View showView) {
        actionOtherVisible(true, triggerView, showView);
    }

    public void hide(View triggerView, View hideView) {
        actionOtherVisible(false, triggerView, hideView);
    }

    public void setAnimListener(AnimListener listener) {
        mListener = listener;
    }
}

你可能感兴趣的:(UI篇章)