【问题标题】:Android Radial / Pie MenuAndroid 径向 / 饼图菜单
【发布时间】:2010-04-13 03:56:06
【问题描述】:

我希望在我正在编写的游戏中创建一个径向菜单。是否包含一个类或 API 来协助这个或开源解决方案?

类似this

谢谢你, 杰克

【问题讨论】:

    标签: android user-interface


    【解决方案1】:

    下面有 View 类的 ondraw() 方法来绘制径向菜单..

        @Override
        protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        mRadialMenuPaint.setShadowLayer(mShadowRadius, 0.0f, 0.0f, mShadowColor);  
        //Draw the menu if the menu is to be displayed.
        if(isMenuVisible) {
            canvas.drawArc(mMenuRect, mStartAngle, 180, true, mRadialMenuPaint);
            //See if there is any item in the collection
            if(mMenuItems.size() > 0) {
                float mStart = mStartAngle;
                //Get the sweep angles based on the number of menu items
                float mSweep = 180/mMenuItems.size();
                for(SemiCircularRadialMenuItem item : mMenuItems.values()) {
                    mRadialMenuPaint.setColor(item.getBackgroundColor());
                    item.setMenuPath(mMenuCenterButtonRect, mMenuRect, mStart, mSweep, mRadius, mViewAnchorPoints);
                    canvas.drawPath(item.getMenuPath(), mRadialMenuPaint);
                    if(isShowMenuText) {
                        mRadialMenuPaint.setShadowLayer(mShadowRadius, 0.0f, 0.0f, Color.TRANSPARENT);  
                        mRadialMenuPaint.setColor(item.getTextColor());
                        canvas.drawTextOnPath(item.getText(), item.getMenuPath(), 5, textSize, mRadialMenuPaint);
                        mRadialMenuPaint.setShadowLayer(mShadowRadius, 0.0f, 0.0f, mShadowColor);
                    }
                    item.getIcon().draw(canvas);
                    mStart += mSweep;
                }
                mRadialMenuPaint.setStyle(Style.FILL);
            }
        }
        //Draw the center menu toggle piece
        mRadialMenuPaint.setColor(centerRadialColor);
        canvas.drawArc(mMenuCenterButtonRect, mStartAngle, 180, true, mRadialMenuPaint);
        mRadialMenuPaint.setShadowLayer(mShadowRadius, 0.0f, 0.0f, Color.TRANSPARENT);  
        //Draw the center text
        drawCenterText(canvas, mRadialMenuPaint);
    
    }
    

    并且还管理触摸事件中的 X、Y 坐标以触摸项目菜单

      @Override
      public boolean onTouchEvent(MotionEvent event) {
        int x = (int) event.getX();
        int y = (int) event.getY();
    
        switch (event.getAction()) {
        case MotionEvent.ACTION_DOWN:
            if(mMenuCenterButtonRect.contains(x, y-15)) {
                centerRadialColor = RadialMenuColors.HOLO_LIGHT_BLUE;
                isMenuTogglePressed = true;
                invalidate();
            }
            else if(isMenuVisible) {
                if(mMenuItems.size() > 0) {
                    for(SemiCircularRadialMenuItem item : mMenuItems.values()) {
                        if(mMenuRect.contains((int) x+20, (int) y))
                            if(item.getBounds().contains((int) x+20, (int) y)) {
                                System.out.println("get x...> " + x);
                                System.out.println("get y...> " + y);
                                isMenuItemPressed = true;
                                mPressedMenuItemID = item.getMenuID();
                                break;
                            }
                    }
                    mMenuItems.get(mPressedMenuItemID).setBackgroundColor(mMenuItems.get(mPressedMenuItemID).getMenuSelectedColor());
                    invalidate();
                }
            }
            break;
        case MotionEvent.ACTION_UP:
            if(isMenuTogglePressed) {
                centerRadialColor = Color.WHITE;
                if(isMenuVisible) {
                    isMenuVisible = false;
                    centerMenuText = openMenuText;
                } else {
                    isMenuVisible = true;
                    centerMenuText = closeMenuText;
                }
                isMenuTogglePressed = false;
                invalidate();
            }
    
            if(isMenuItemPressed) {
                if(mMenuItems.get(mPressedMenuItemID).getCallback() != null) {
                    mMenuItems.get(mPressedMenuItemID).getCallback().onMenuItemPressed();
                }
                mMenuItems.get(mPressedMenuItemID)
                    .setBackgroundColor(mMenuItems.get(mPressedMenuItemID).getMenuNormalColor());
                isMenuItemPressed = false;
                invalidate();
            }
            break;
        }
    
        return true;
    }
    

    希望以上代码对您有所帮助..

    【讨论】:

      【解决方案2】:

      没有针对此类菜单的内置 API,但是,至少有两种方法可以做到这一点

      1) 构建一个代表您的“菜单”的布局并将其附加到您的 android 视图根目录的“FrameLayout”。通过在使元素可见之前调整元素的位置,您可以在碎石上移动它。这种方法有点“hacky”,但应该可以。

      2) 构建一个完全自定义的组件,包括您自己的绘图方法和 onTouch 事件,并将其附加到您的视图。这个方法有点复杂(你需要实现所有的绘图方法),但也更通用。

      无论哪种情况,请记住,当用户使用轨迹球/方向键时,您需要考虑径向菜单的功能。

      【讨论】:

      • 这是我的假设,但我希望自定义组件已经存在。感谢您的信息。
      猜你喜欢
      • 1970-01-01
      • 2012-09-07
      • 1970-01-01
      • 1970-01-01
      • 2016-06-16
      • 1970-01-01
      • 2015-05-20
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多