Android编程:扇形展开的悬浮菜单按钮CircularFloatingActionMenu实例

Android编程:扇形展开的悬浮菜单按钮CircularFloatingActionMenu实例


本文博客链接:http://blog.csdn.net/jdh99,作者:jdh,转载请注明.


环境:

主机:WIN10

开发环境:Android Studio 2.2 Preview 3


说明:

用第三方库CircularFloatingActionMenu实现悬浮按钮扇形展开


效果图:




源码:

private void initFloatingActionsMenu(View view) {
        // 添加 右下角的白色+号按钮
        final ImageView fabIcon = new ImageView(getContext());
        fabIcon.setImageDrawable(getResources().getDrawable(R.drawable.ic_fab, null));
        final FloatingActionButton fabButton = new FloatingActionButton.Builder(getActivity())
                .setContentView(fabIcon)
                .setPosition(FloatingActionButton.POSITION_BOTTOM_LEFT)
                .build();

        SubActionButton.Builder rLSubBuilder = new SubActionButton.Builder(getActivity());

        ImageView imageViewQuit = new ImageView(getContext());
        ImageView imageViewTool = new ImageView(getContext());
        ImageView imageViewPalette = new ImageView(getContext());
        ImageView imageViewCamera = new ImageView(getContext());
        imageViewQuit.setImageDrawable(getResources().getDrawable(R.drawable.ic_call_end_black_48dp, null));
        imageViewTool.setImageDrawable(getResources().getDrawable(R.drawable.ic_settings_applications_black_48dp, null));
        imageViewPalette.setImageDrawable(getResources().getDrawable(R.drawable.ic_color_lens_black_48dp, null));
        imageViewCamera.setImageDrawable(getResources().getDrawable(R.drawable.ic_camera_alt_black_48dp, null));

        SubActionButton buttonQuit = rLSubBuilder.setContentView(imageViewQuit).build();
        SubActionButton buttonPalette = rLSubBuilder.setContentView(imageViewPalette).build();
        SubActionButton buttonTool = rLSubBuilder.setContentView(imageViewTool).build();
        SubActionButton buttonCamera = rLSubBuilder.setContentView(imageViewCamera).build();

        // Build the menu with default options: light theme, 90 degrees, 72dp
        // radius.
        // Set 4 default SubActionButtons
        // FloatingActionMenu通过attachTo(fabButton)附着到FloatingActionButton
        final FloatingActionMenu buttonToolMenu = new FloatingActionMenu.Builder(getActivity())
                .addSubActionView(buttonPalette)
                .addSubActionView(buttonCamera)
                .addSubActionView(buttonTool)
                .addSubActionView(buttonQuit)
                .setStartAngle(0)
                .setEndAngle(-90)
                .attachTo(fabButton)
                .build();

        // Listen menu open and close events to animate the button content view
        buttonToolMenu.setStateChangeListener(new FloatingActionMenu.MenuStateChangeListener() {
            @Override
            public void onMenuOpened(FloatingActionMenu menu) {
                // 增加按钮中的+号图标顺时针旋转45度
                // Rotate the icon of fabButton 45 degrees clockwise
                fabIcon.setRotation(0);
                PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, 45);
                ObjectAnimator animation = ObjectAnimator.ofPropertyValuesHolder(fabIcon, pvhR);
                animation.start();
            }

            @Override
            public void onMenuClosed(FloatingActionMenu menu) {
                // 增加按钮中的+号图标逆时针旋转45度
                // Rotate the icon of fabButton 45 degrees
                // counter-clockwise
                fabIcon.setRotation(45);
                PropertyValuesHolder pvhR = PropertyValuesHolder.ofFloat(View.ROTATION, 0);
                ObjectAnimator animation = ObjectAnimator.ofPropertyValuesHolder(fabIcon, pvhR);
                animation.start();
            }
        });

        RxView.clicks(buttonQuit)
                .throttleFirst(1, TimeUnit.SECONDS)
                .compose(this.bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(v -> {
                    Voip.getInstance().hangUpCall(callId);
                    finishActivity();
                });

        RxView.clicks(buttonPalette)
                .throttleFirst(1, TimeUnit.SECONDS)
                .compose(this.bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(v -> {
                    buttonToolMenu.close(true);
//                    buttonToolMenu.collapse();
                    dialogPalette.show();
                });

        RxView.clicks(buttonCamera)
                .throttleFirst(1, TimeUnit.SECONDS)
                .compose(this.bindUntilEvent(FragmentEvent.DESTROY))
                .subscribe(v -> {
                    buttonToolMenu.close(true);
//                    buttonToolMenu.collapse();
                    dialogSelectImage.show();
                });
    }


参考链接:



  • 1
    点赞
  • 17
    收藏
    觉得还不错? 一键收藏
  • 4
    评论
要实现点击扇形展开效果菜单,需要使用CSS中的transform和transition属性。以下是一种实现方式: HTML结构: ```html <div class="menu"> <div class="item item1"></div> <div class="item item2"></div> <div class="item item3"></div> <div class="item item4"></div> <div class="item item5"></div> <div class="center"></div> </div> ``` CSS样式: ```css .menu { position: relative; width: 200px; height: 200px; } .item { position: absolute; width: 50px; height: 50px; background-color: #ccc; border-radius: 50%; transition: transform 0.5s ease; } .item1 { top: -25px; left: 75px; } .item2 { top: 35px; left: 35px; } .item3 { top: 75px; left: -25px; } .item4 { top: 35px; left: -85px; } .item5 { top: -25px; left: -45px; } .center { position: absolute; top: 50%; left: 50%; width: 20px; height: 20px; background-color: #333; border-radius: 50%; transform: translate(-50%, -50%); cursor: pointer; } ``` JavaScript代码: ```javascript var menu = document.querySelector('.menu'); var items = document.querySelectorAll('.item'); var center = document.querySelector('.center'); var isOpen = false; center.addEventListener('click', function() { if (isOpen) { // 关闭菜单 menu.classList.remove('open'); items.forEach(function(item, index) { item.style.transform = 'rotate(0deg)'; }); } else { // 打开菜单 menu.classList.add('open'); items.forEach(function(item, index) { item.style.transform = 'rotate(' + (index * 72) + 'deg)'; }); } isOpen = !isOpen; }); ``` 在上面的代码中,我们通过JavaScript监听中心圆的点击事件,并根据菜单是否打开来决定是展开还是收起菜单。当菜单打开时,我们给菜单容器添加open类名,并将每个菜单项按照一定角度旋转,从而形成扇形展开效果。当菜单关闭时,我们将open类名移除,并将每个菜单项旋转回初始状态。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值