Android Question Canvas initialization cause OOM

hookshy

Well-Known Member
Licensed User
Longtime User
On most of devices calling twice canvas.initialize(imageview) does not make any diference but I do have crashes due to oom with same code on samsung devices.

1.Question: Can the process crash with oom due to multiple initialization for canvas object ?
Do I have to use several canvas objects for each view that I want to draw into ?
What hapens to canvas object in the next initialisation if I do not call view.invalidate on first usage ?


Exemple of usage:
B4X:
'on large apps I may have missed the invalidate statement after drawing so I do not understand how the drawing consumes memory and how to optimise it.
canvas.initialize(imageview1)
canvas.DrawLine(x0,y0,X,Y,maincolor,pensize)
imageview1.invalidate
canvas.initialize(imageview2)
canvas.DrawLine(x0,y0,X,Y,maincolor,pensize)
imageview2.invalidate

I generaly use an imageview sized activity.width and activity.height I think device can run out of memory due to imgeview size on large display.
2.Question: Initialising a canvas on a large imageview screen sized can cause oom ?


3.
Canvas documentation sais:
The view background will be drawn on the canvas during initialisation.
Note that you should not change view background after calling this method.

3.Question: Can somebody explaing how the change of the background view can affect the canvas, can this be related to OOM ?


Thank you for your suport.
 

Erel

B4X founder
Staff member
Licensed User
Longtime User
The error has nothing to do with Invalidate. When you initialize a canvas a mutable bitmap is created with the same size of the target view. If the view is large and especially on high resolution devices it can result in a very large bitmap.

You should avoid initializing the canvas multiple times. Do it once with a mutable bitmap and reuse it whenever needed. You can also create a small bitmap and let the ImageView scale it up.
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
Thanks for the explanatory ...
performed several tests, I will try to draw with canvas to a bitmap and reuse this bitmap
Will track the initialise method to keep memory in control.
 
Upvote 0

hookshy

Well-Known Member
Licensed User
Longtime User
I draw several times on same view and I have to delete the previous draw
I use the code below ...and initialise the canvas each time I want to draw something , this means that the canvas is initialize multiple times
I tried initialise the canvas on activity create but it seems that with this code below nothing could be draw , then when I initialize only once in this sub when cleaning the actual image view I got error with null pointer exception .

Can you tell where I am doing wrong
Code below used to draw on the image view , it clean the view before drawing again

B4X:
Sub preview_margin

Log("delete preview")

imgmove=False'keep parent still

preview.Bitmap=Null
preview.Invalidate

plansa.initialize(preview)
sr.Initialize(0,0,preview.Width,preview.Height)
plansa.DrawRect(sr,Colors.ARGB(0,255,255,255),True,0dip)
preview.Invalidate

End Sub
 
Upvote 0
Top