19

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?

superdave
  • 1,928
  • 1
  • 17
  • 35
  • 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. – Gabe Sechan May 08 '13 at 19:59

2 Answers2

51

Ah ha -- found the answer:

rectF.round(rect);

-- or --

rectF.roundOut(rect);
superdave
  • 1,928
  • 1
  • 17
  • 35
0

You can use extension functions in Kotlin:

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

or

val yourRectF = RectF()
val yourRect = yourRectF.toRect()
charman
  • 123
  • 6