2013-02-26 126 views
1

現在這些天我正在旋轉輪可能附加的屏幕截圖。從github https://github.com/R4md4c/AndroidRotaryWheelView 困難帶旋轉圖標的旋轉輪

enter image description here

enter image description here

我發現代碼是我不能夠保持在垂直位置的圖標車輪的旋轉過程中。圖標以其輪子段的形式進行旋轉雖然最初需要將它們保持在垂直位置(如屏幕截圖1所示)。

我必須自定義以下代碼,我需要更改特定drawable的矩形的邊界。

我發現自己無法實現參考中的精確計算。

任何幫助將不勝感激。

@Override 
public void onDraw(Canvas canvas) { 
// set the height of Wheel childs items 
    canvas.scale(getWidth()/mViewRect.width(), getHeight()/2 
      /mViewRect.width(), xPosition, yPosition); 


    canvas.save(Canvas.MATRIX_SAVE_FLAG); // Saving the canvas and later 
              // restoring it so only this 
              // image will be rotated. 
    canvas.rotate((float) mRotationAngle, xPosition, yPosition); 

    for (int i = 0; i < mWedges.length; i++) { 
     Wedge f = mWedges[i]; 
     mPaint.setColor(SEGMENT_COLOR); 
     mPaint.setStyle(Paint.Style.FILL_AND_STROKE); 
     canvas.drawPath(f, mPaint); 

     Rect rf = iconRect[i]; 

     if ((mMenuEntries.get(i).getIcon() != 0) 
       && (mMenuEntries.get(i).getLabel() != null)) { 

      System.out.println("the canvasd drawn ........"); 

      // This will look for a "new line" and split into multiple lines 
      String menuItemName = mMenuEntries.get(i).getLabel(); 
      String[] stringArray = menuItemName.split("\n"); 

      mPaint.setColor(textColor); 

      mPaint.setStyle(Paint.Style.FILL); 
      // mPaint.setTextSize(textSize); 

      Rect rect = new Rect(); 
      float textHeight = 0; 
      for (int j = 0; j < stringArray.length; j++) { 
       mPaint.getTextBounds(stringArray[j], 0, 
         stringArray[j].length(), rect); 
       textHeight = textHeight + (rect.height() + 3); 
      } 

      Rect rf2 = new Rect(); 
      rf2.set(rf.left, rf.top - ((int) textHeight/2), rf.right, 
        rf.bottom - ((int) textHeight/2)); 

      float textBottom = rf2.bottom; 
      for (int j = 0; j < stringArray.length; j++) { 
       mPaint.getTextBounds(stringArray[j], 0, 
         stringArray[j].length(), rect); 
       float textLeft = rf.centerX() - rect.width()/2; 
       textBottom = textBottom + (rect.height() + 3); 
       mPaint.setTextSize(scalePX(8)); 
       canvas.drawText(stringArray[j], textLeft - rect.left, 
         textBottom - rect.bottom, mPaint); 
      } 

      // canvas.rotate((float)mRotationAngle, 
      // rf.top-((int)textHeight/2), rf.bottom-((int)textHeight/2)); 
      int index = checkSelection(canvas); 
      rf2 = rotaionRf(rf2, mRotationAngle); 


      if (i == index) { 
       // Puts in the Icon 
       Drawable drawable = getResources().getDrawable(
         mMenuEntries.get(i).getIconSelected()); 
       drawable.setBounds(rf2); 
       drawable.draw(canvas); 

      } else { 
       // Puts in the Icon 
       Drawable drawable = getResources().getDrawable(
         mMenuEntries.get(i).getIcon()); 
       drawable.setBounds(rf2); 
       drawable.draw(canvas); 
      } 
      // Icon Only 
     } else if (mMenuEntries.get(i).getIcon() != 0) { 

      System.out.println("the canvasd drawn ELSE........"); 

      // Puts in the Icon 
      Drawable drawable = getResources().getDrawable(
        mMenuEntries.get(i).getIconSelected()); 
      drawable.setBounds(rf); 

      drawable.draw(canvas); 

      // Text Only 
     } else { 
      // Puts in the Text if no Icon 
      mPaint.setColor(this.textColor); 

      /* 
      * if (f != enabled && Wedge2Shown == true) { 
      * mPaint.setAlpha(disabledAlpha); } else { 
      * mPaint.setAlpha(textAlpha); } 
      */ 
      // mPaint.setAlpha(textAlpha); 
      mPaint.setStyle(Paint.Style.FILL); 
      // mPaint.setTextSize(textSize); 

      // This will look for a "new line" and split into multiple lines 
      String menuItemName = mMenuEntries.get(i).getLabel(); 
      String[] stringArray = menuItemName.split("\n"); 

      // gets total height 
      Rect rect = new Rect(); 
      float textHeight = 0; 
      for (int j = 0; j < stringArray.length; j++) { 
       mPaint.getTextBounds(stringArray[j], 0, 
         stringArray[j].length(), rect); 
       textHeight = textHeight + (rect.height() + 3); 
      } 

      float textBottom = rf.centerY() - (textHeight/2); 
      for (int j = 0; j < stringArray.length; j++) { 
       mPaint.getTextBounds(stringArray[j], 0, 
         stringArray[j].length(), rect); 
       float textLeft = rf.centerX() - rect.width()/2; 
       textBottom = textBottom + (rect.height() + 3); 
       canvas.drawText(stringArray[j], textLeft - rect.left, 
         textBottom - rect.bottom, mPaint); 

      } 

      // canvas.drawTextOnPath(text, path, hOffset, vOffset, paint) 
      // canvas.rotate((float)mRotationAngle, xPosition, yPosition); 
      // canvas.drawRect(rf, mPaint); 

     } 
     // canvas.restore(); 
    } 
    // canvas.restore(); 
    canvas.restore(); 

    // System.out.println() 
    canvas.save(); 

    canvas.restore(); 


    mPaint.setShader(mShader); 
    // mPaint.setAlpha(0x66); 
    // Draw the Selection Segment 
    if (mSelectionWedge != null) { 
     canvas.drawPath(mSelectionWedge, mPaint); 
     // canvas.drawRect(mSelectionWedge.getWedgeRegion().getBounds(), 
     // mPaint); 
    } 

    mPaint.setShader(null); 



    int index = checkSelection(canvas); 

    System.out.println("the index=====" + index); 

    if (checkSelection(canvas) != -1) { 
    } 
} 
+0

發佈您的答案在這個賞金qtn ..http://stackoverflow.com/questions/6826982/rotate-images-around-a-circle – 2015-01-08 14:50:45

回答

0

使用canvas.rotate u必須確保X & y值你給它的主輪中心。