找到矩形中心

时间:2013-12-13 18:06:58

标签: android android-canvas

如何找到画布中绘制的矩形的中心坐标?我唯一的项目是left,right,bottom和top值。

Rect newRect = new Rect(left, top, right, bottom);
canvas.drawRect(newRect, paint);

2 个答案:

答案 0 :(得分:3)

center = 0.5(左+右),0.5(下+上)

答案 1 :(得分:3)

只需使用Rect方法centerX()centerY()

Rect newRect = new Rect(left, top, right, bottom);
canvas.drawRect(newRect, paint);
int xCenter = newRect.centerX();
int yCenter = newRect.centerY();

或者,如果您需要浮点值以获得更好的精确度,请使用exactCenterX()exactCenterY()

float xCenter = newRect.exactCenterX();
float yCenter = newRect.exactCenterY();