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?
If you're drawing it in something like an imageView, use a matrix. A matrix in the form of:
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.