t.r.hogenkamp
t.r.hogenkamp

Reputation: 73

onDraw - How to scale bitmap without creating a new bitmap?

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?

Upvotes: 0

Views: 71

Answers (1)

Gabe Sechan
Gabe Sechan

Reputation: 93678

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.

Upvotes: 0

Related Questions