1

我正在尝试为 android 设备修改操纵杆组件。目前该组件能够跟踪您手指的 x 和 y 位置并在那里绘制一个小圆圈。现在我要做的是从中心到手指的 x 和 y 位置绘制一个矩形或圆弧。

据我所知,矩形只接受顶部、左侧、右侧和底部的值,这仅允许我水平或垂直绘制矩形。如果有人知道另一种画法,那就太棒了。

我已经设法使用 canvas.drawLine 得到一个直矩形但是我现在想使用 drawArc 函数使矩形弯曲,但这需要 RectF 的参数。

我目前使用的代码是:

@Override
protected void onDraw(Canvas canvas) {

    int centerX = (getWidth())/2;
    int centerY = (getHeight())/2;
    Paint p = new Paint();
    p.setColor(buttonColor);

    RadialGradient gradient = new RadialGradient( centerX, centerY, joystickRadius,
    backgroundColor,circleColor, android.graphics.Shader.TileMode.CLAMP);

    //draw an outer circle
    p.setDither(true);
    p.setShader(gradient);
    canvas.drawCircle( (int) centerX, (int) centerY, joystickRadius, p);


    p.setShader(null);
    p.setColor(buttonColor);
    p.setStyle(Paint.Style.FILL);

    //draw the joystick thumbpad
    canvas.drawCircle(x, y, buttonRadius, p);

    // draw a line from the centre of outer circle to the center of the 
    // thumbpad. I want this to be a curved rectangle instead.
    canvas.drawLine( centerX, centerY, x, y, p);
}
4

1 回答 1

1

我假设您正在尝试从中心到接触点绘制一个矩形?

看看 Canvas.save/restore。一些伪代码:

canvas.save()

canvas.rotate(angleOfRectangle)

canvas.drawRectangle(size)

canvas.restore()

这个想法是保存当前画布状态,旋转整个画布,绘制矩形,然后恢复画布状态以“忘记”旋转”。

[编辑]

感谢马特的更正。

希望能帮助到你...

于 2012-06-06T07:29:05.197 回答