问题
How do you find the center coordinates of a Rectangle drawn in the canvas? The only items I have is the left, right, bottom, and top values.
Rect newRect = new Rect(left, top, right, bottom);
canvas.drawRect(newRect, paint);
回答1:
Just use Rect methods centerX() and centerY()
Rect newRect = new Rect(left, top, right, bottom);
canvas.drawRect(newRect, paint);
int xCenter = newRect.centerX();
int yCenter = newRect.centerY();
Or if you need a float value for better precision, use exactCenterX() and exactCenterY()
float xCenter = newRect.exactCenterX();
float yCenter = newRect.exactCenterY();
回答2:
center = 0.5(left+right), 0.5(bottom+top)
来源:https://stackoverflow.com/questions/20573124/find-center-of-rectangle