我正在尝试为 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);
}