2010-04-13 17 views

回答

7

下面有視圖類的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,在觸摸事件ŸCordinates觸摸項目菜單

@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; 
} 

enter image description here

希望以上代碼有幫助..

+0

非常令人印象深刻的工作!我迫不及待想要嘗試一下! – 2014-02-18 14:15:35

2

有沒有在API沒有內置這樣的菜單,但是,至少還有兩種方法可以做到這一點

1)建立一個佈局代表你的「菜單」和其附加到您的android視圖根目錄下的「FrameLayout」。通過在元素可見之前調整元素的位置,可以將其移動到碎石上。這種方法有點「黑客」,但它應該起作用。

2)構建一個完全自定義的組件,包括您自己的繪圖方法和onTouch事件,並將其附加到您的視圖。這種方法比較複雜(你需要實現所有的繪圖方法),但也有些更一般。

在這兩種情況下,請記住,您需要考慮用戶使用軌跡球/ d-pad時徑向菜單的功能。

+0

這就是我所設想的,但我是hopin g自定義組件已經存在。謝謝(你的)信息。 – 2010-10-25 03:22:23

+0

你能告訴我如何移動刷卡上的徑向? – AndroidDev 2014-10-17 16:50:50