What is the best way to convert from a RectF to a Rect in Android?
Asked Answered
U

2

20

I would like to convert from a RectF to a Rect in Android. Currently I have:

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

...

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

Is there a better way?

Untouchability answered 8/5, 2013 at 19:57 Comment(1)
THe best way would be not to need to switch between the two. If you have to, that's the way to do it. If you need more accuracy you may round the values rather than truncating them with a cast,but for most cases that's not needed.Accumulation
U
52

Ah ha -- found the answer:

rectF.round(rect);

-- or --

rectF.roundOut(rect);
Untouchability answered 8/5, 2013 at 20:1 Comment(0)
E
0

You can use extension functions in Kotlin:

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

or

val yourRectF = RectF()
val yourRect = yourRectF.toRect()
Exemplar answered 6/4, 2023 at 3:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.