首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Android Radial / Pie菜单

Android Radial / Pie菜单
EN

Stack Overflow用户
提问于 2010-04-13 11:56:07
回答 2查看 5K关注 0票数 9

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

就像this一样。

谢谢你,杰克

EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-02-17 20:31:00

下面有View类的ondraw()方法来绘制Radial菜单。

代码语言:javascript
复制
    @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坐标以触摸项目菜单

代码语言:javascript
复制
  @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;
}

希望上面的代码能有所帮助..

票数 4
EN

Stack Overflow用户

发布于 2010-10-25 07:37:42

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

1)构建一个表示“菜单”的布局,并将其附加到安卓视图根目录下的"FrameLayout“。在使元素可见之前,通过调整元素的位置,可以在屏幕上移动元素。这种方法有点“老生常谈”,但它应该是有效的。

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

在这两种情况下,请记住,当用户使用trackball/d-pad时,您需要考虑径向菜单的功能。

票数 2
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/2626887

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档