美文网首页Android开发部落Android技术知识Android知识
Android 移动小球+CircularReveal页面切换动

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

作者: 超神的菠萝 | 来源:发表于2017-05-24 11:34 被阅读420次

    效果图如下

    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

        <style name="AppThemeCircleRevel" parent="Theme.AppCompat.Light.NoActionBar">
            <!-- Customize your theme here. -->
            <item name="colorPrimary">@color/colorPrimary</item>
            <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
            <item name="colorAccent">@color/blue</item>
    
            <item name="android:windowAnimationStyle">@null</item>
            <item name="android:windowBackground">@android:color/transparent</item>
            <item name="android:windowIsTranslucent">true</item>
            <item name="android:colorBackgroundCacheHint">@null</item>
        </style>
    

    相关文章

      网友评论

      • 我一定会学会:兄弟,请问有没有demo
        超神的菠萝:不好意思。。这个效果是写在同事项目上的,他已经离职很久了。。。代码在他那边。。。:sob:

      本文标题:Android 移动小球+CircularReveal页面切换动

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