Android 移动小球+CircularReveal页面切换动画

效果图如下

ScreenGif.gif

是在fragment中跳转activity实现的效果,fragment跳fragment,activity跳activity类似~~

实现过程

  • 重写FloatingActionButtononTouchListener()方法,使小球可以移动,并判断边界
  • 点击fab时记录坐标传到下一个页面,在下一个页面展示动画。
  • 点击后退或者重写onBackPressed()方法,执行动画

重写Fab的onTouchListener()

        floatingActionButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View view, MotionEvent ev) {
                switch (ev.getAction()) {
                    case MotionEvent.ACTION_DOWN:
                        downX = ev.getX();
                        downY = ev.getY();
                        isClick = true;
                        break;
                    case MotionEvent.ACTION_MOVE:
                        isClick = false;
                        moveX = ev.getX();
                        moveY = ev.getY();

                        int offsetX = (int) (moveX - downX);
                        int offsetY = (int) (moveY - downY);

                        //这里使用了setTranslation来移动view。。。尝试过layout。不知道为什么fragment切换回来的时候会恢复原位
                        floatingActionButton.setTranslationX(floatingActionButton.getTranslationX() + offsetX);
                        floatingActionButton.setTranslationY(floatingActionButton.getTranslationY() + offsetY);

                        break;
                    case MotionEvent.ACTION_UP:
                        //用来触发点击事件
                        if (isClick) {
                            startAct();
                            return false;
                        }
                        //用来判断移动边界

                        if (floatingActionButton.getX() < 0) {
                            floatingActionButton.setX(0);
                        }
                        if (floatingActionButton.getX() + floatingActionButton.getWidth() > ScreenUtil.getScreenWidth(getContext())) {
                            floatingActionButton.setX(ScreenUtil.getScreenWidth(getContext()) - floatingActionButton.getWidth());
                        }
                        if (floatingActionButton.getY() < titleHeight) {
                            floatingActionButton.setY(0);
                        }
                        if (floatingActionButton.getY() + floatingActionButton.getHeight() + titleHeight >
                                getActivity().findViewById(R.id.activity_main_mainLl).getHeight() - getActivity().findViewById(R.id.fc_rg).getHeight()) {
                            floatingActionButton.setY(getBottomY());
                        }

                        break;
                }
                return true;
            }

            private void startAct() {
                //跳转Activity,传递动画参数
                Intent intent = new Intent(getActivity(), CheckWorkActivity.class);
                intent.putExtra("x", (int) floatingActionButton.getX() + floatingActionButton.getWidth() / 2);
                intent.putExtra("y", (int) floatingActionButton.getY() + floatingActionButton.getHeight() / 2);
                intent.putExtra("start_radius", floatingActionButton.getWidth() / 2);
                intent.putExtra("end_radius", DialogFragment.this.view.getHeight());
                startActivity(intent);
            }
        });

在下一个页面中实现CircleRevel动画

onCrete中调用

    private void initAnimation() {
        //ll为根布局
        final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
        linearLayout.post(new Runnable() {
            @Override
            public void run() {
                if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
                    Animator animator = ViewAnimationUtils.createCircularReveal(
                            linearLayout,// 操作的视图
                            getIntent().getIntExtra("x", 0),  // 动画的中心点X
                            getIntent().getIntExtra("y", 0) + findViewById(R.id.title).getHeight(), // 动画的中心点Y
                            getIntent().getIntExtra("start_radius", 0),    // 动画半径
                            getIntent().getIntExtra("end_radius", 0)           // 动画结束半径
                    );
                    animator.setInterpolator(new AccelerateInterpolator());
                    animator.setDuration(500);
                    animator.start();
                }
            }
        });
    }

点击后退或者触发onBackPressed时候调用

    private void endAnim() {
        final LinearLayout linearLayout = (LinearLayout) findViewById(R.id.ll);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.LOLLIPOP) {
            Animator animator = ViewAnimationUtils.createCircularReveal(
                    linearLayout,// 操作的视图
                    getIntent().getIntExtra("x", 0),
                    getIntent().getIntExtra("y", 0) + findViewById(R.id.title).getHeight(),
                    getIntent().getIntExtra("end_radius", 0),
                    getIntent().getIntExtra("start_radius", 0)

            );
            animator.setInterpolator(new AccelerateInterpolator());
            animator.setDuration(500);
            animator.addListener(new AnimatorListenerAdapter() {
                @Override
                public void onAnimationEnd(Animator animation) {
                    super.onAnimationEnd(animation);
                    finish();
                }
            });
            animator.start();
        }
    }

还有一个重要的地方是修改两个activity的theme

    

你可能感兴趣的:(Android 移动小球+CircularReveal页面切换动画)