0

I am making a custom view and I am overriding its onDraw method. In the onDraw method I am scaling a Bitmap, with the createBitmap function. This works perfectly fine. However, Android Studio gives a warning when doing this, namely: avoid object allocations during draw/layout operations.

Is there another way to scale a bitmap, without creating a new bitmap? What is best practise in this case?

t.r.hogenkamp
  • 73
  • 2
  • 7

1 Answers1

0

If you're drawing it in something like an imageView, use a matrix. A matrix in the form of:

[[x 0]
 [0 x]]

will scale the bitmap by a factor of x. Which can also be <1 to scale it down.

If you just want to get rid of that specific warning- rather than creating a new bitmap each time onDraw is called, initialize the bitmap once and reuse the same object.

Gabe Sechan
  • 90,003
  • 9
  • 87
  • 127