在 canvas(Android Studio)上找到形状(路径)的中心
Find the center of a shape (path) on a canvas (Android Studio)
是否可以在 Android 工作室中找到 canvas 上的形状中心?我已经记录了接触点,但似乎无法弄清楚。然而,canvas (0,0) 点确实出现在左上角,但 canvas 上的 paths/shapes 将中心点视为 (0,0)。
有关我正在谈论的示例,请查看所附图片,我正在尝试找到绿点所在的位置。
在此先感谢您的帮助。
IMG
要找到 Path
的中心,请使用方法 computeBounds(bounds,exact),它将第一个参数 RectF bounds
设置为路径的范围。那么只要取左右和上下坐标的均值,就可以得到路径的几何中心了。
// mPath is your path. Must contain more than 1 path point
RectF bounds = new RectF();
mPath.computeBounds(bounds, false); // fills rect with bounds
PointF center = new PointF((bounds.left + bounds.right) / 2,
(bounds.top + bounds.bottom) / 2);
No Need for Mathematical Calculations
RectF bounds = new RectF();
mPath.computeBounds(bounds, false);
float centerX = rectF.centerX();
float centerY = rectF.centerY();
是否可以在 Android 工作室中找到 canvas 上的形状中心?我已经记录了接触点,但似乎无法弄清楚。然而,canvas (0,0) 点确实出现在左上角,但 canvas 上的 paths/shapes 将中心点视为 (0,0)。
有关我正在谈论的示例,请查看所附图片,我正在尝试找到绿点所在的位置。
在此先感谢您的帮助。
IMG
要找到 Path
的中心,请使用方法 computeBounds(bounds,exact),它将第一个参数 RectF bounds
设置为路径的范围。那么只要取左右和上下坐标的均值,就可以得到路径的几何中心了。
// mPath is your path. Must contain more than 1 path point
RectF bounds = new RectF();
mPath.computeBounds(bounds, false); // fills rect with bounds
PointF center = new PointF((bounds.left + bounds.right) / 2,
(bounds.top + bounds.bottom) / 2);
No Need for Mathematical Calculations
RectF bounds = new RectF();
mPath.computeBounds(bounds, false);
float centerX = rectF.centerX();
float centerY = rectF.centerY();