RectF 是 Android 中用于表示矩形区域的一个类
首先,确保你已经在项目中导入了 android.graphics.RectF 类。
创建一个 RectF 对象并初始化其属性:
RectF rect = new RectF();
set() 方法设置矩形的左上角、右下角坐标和宽度、高度:rect.set(left, top, right, bottom);
其中,left 和 top 分别是矩形左上角的 x 和 y 坐标,right 和 bottom 分别是矩形右下角的 x 和 y 坐标。
union() 方法合并两个矩形:RectF anotherRect = new RectF(x1, y1, x2, y2);
rect.union(anotherRect);
这将把 anotherRect 的边界合并到 rect 中,形成一个新的包含两个矩形的最小矩形。
contains() 方法检查一个点是否在矩形内:float x = ...;
float y = ...;
boolean isInside = rect.contains(x, y);
equals() 方法比较两个 RectF 对象是否相等:RectF anotherRect = new RectF(x1, y1, x2, y2);
boolean areEqual = rect.equals(anotherRect);
toString() 方法将 RectF 对象转换为字符串:String rectString = rect.toString();
这些是 RectF 的基本用法。你可以根据具体需求进行操作。如果你需要绘制矩形,可以使用 Canvas 类的 drawRect() 方法:
Canvas canvas = ...;
canvas.drawRect(rect, paint);
其中,paint 是一个 Paint 对象,用于设置矩形的颜色、描边等样式。