1

现在这些天我正在研究可能附有屏幕截图的旋转轮。

在此处输入图像描述

在此处输入图像描述

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

我必须自定义以下代码,我需要更改特定可绘制矩形的边界。

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

任何帮助将不胜感激。

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

1 回答 1

0

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

于 2013-02-26T15:15:53.203 回答