在Android中,从RectF转换为Rect的最佳方法是什么?

19

我想在Android中将RectF转换为Rect。目前我的代码如下:

RectF rectF = new RectF();
rectF.set( ... some values ... );

...

Rect rect = new Rect((int)rectF.left,
                     (int)rectF.top,
                     (int)rectF.right,
                     (int)rectF.bottom));

有更好的方法吗?


最好的方法是不需要在两者之间切换。如果必须这样做,那么这就是做法。如果您需要更高的精度,可以四舍五入值而不是使用强制转换截断它们,但对于大多数情况来说,这并不是必需的。 - Gabe Sechan
2个回答

51

啊哈——找到答案了:

rectF.round(rect);

-- or --

rectF.roundOut(rect);

0

在 Kotlin 中,您可以使用扩展函数:

val myRect = Rect()
val myRectF = myRect.toRectF()

或者

val yourRectF = RectF()
val yourRect = yourRectF.toRect()

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接