旋转轮与旋转图标

时间:2013-02-26 14:57:08

标签: java android user-interface android-canvas

现在我正在研究旋转轮可能附加的截图。

enter image description here

enter image description here

我找到了来自github https://github.com/R4md4c/AndroidRotaryWheelView的代码 困难在于我无法在车轮旋转期间将图标保持在垂直位置。以轮子的形式旋转的图标虽然最初显示时需要将它们保持在垂直位置(如截图1所示)。

我必须自定义以下代码,我需要更改特定drawable的rect的Bounds。

我发现自己无法在参考中实现精确计算。

任何帮助都将受到高度赞赏。

    @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) {
    }
}

1 个答案:

答案 0 :(得分:0)

使用canvas.rotate你必须确保你给它的x&amp; y值是主轮中心。