14

Whats the best way to convert a Rect variable to a RectF one? I am unable to cast it.

RectF rect = (RectF) currentRect; //produces an error
industrychanger
  • 563
  • 1
  • 9
  • 21
  • Possible duplicate of [What is the best way to convert from a RectF to a Rect in Android?](http://stackoverflow.com/questions/16449145/what-is-the-best-way-to-convert-from-a-rectf-to-a-rect-in-android) – Louis CAD Jun 07 '16 at 12:22

2 Answers2

22
RectF rect = new RectF(currentRect);
dymmeh
  • 22,247
  • 5
  • 53
  • 60
5

As "new" shouldn't be done in onDraw,

// As class fields,
Rect rect = new Rect();
RectF rectF = new RectF();

...
onDraw(Canvas canvas) {
    ...
    rectF.set(rect);
    ...
}
Ebrahim Byagowi
  • 10,338
  • 4
  • 70
  • 81