Graphics Drawing Performance

Biscuit

Member
Licensed User
Longtime User
Hi

I have started on my first app in b4a and am just looking for some guidance towards the best way to achieve graphics drawing whilst maintaining good performance. I am new to both b4a and Android devices so I have no reference point...

My first app (just for fun & for learning) is simple kids drawing app that has a bitmap for the background which can be drawn to in various ways as the screen is touched. In a simplest case, I just want to capture the user's touches on the screen and then draw or move them. I know this is running on a mobile device but I can't get much above 10 frames per second so far and feel like I'm missing something :BangHead: I've tried it in the emulator (which would obviously be slow) but also on an HTC Wildfire but they are both similar in speed...

I can't access my code at the mo but in essence I'm doing this:

Activity Create
Create a mutable bitmap to draw with.
Create canvas for that bitmap
Create a canvas for the Activity

Activity Touch
Add new points to draw to a 'master list'

Timer (Interval set to 1ms)
Loop through the master list and draw all of the points to the offscreen bitmap.
Update the screen by drawing the offscreen bitmap to it

This is surprisingly slow even with just a couple of points to draw using Canvas.SetPixel.

If someone can suggest the best technique for doing this or common pitfalls to avoid I'd be most grateful :)

Cheers
 

Biscuit

Member
Licensed User
Longtime User
The example I gave seems overly complicated because this simple example is alluding to other things I'd like to do next. I agree, to capture a signature (or to draw a line) I don't need half of the things I said above. However, it's good to see I have similar variables declared in the same places as in your signature example. Although I notice you are drawing to a panel whereas in my experiments I have been drawing to the Activity by initialising the canvas with the Activity (prior to using the offscreen bitmap). I'll try using panels when I get home later.

I included a timer and an offscreen bitmap because the next function I'd like to implement is to have a ball bouncing across the screen and to do things like this without a trail or any flickering, I've always used double buffering. Invalidate (or Refresh in VB) has always made the screen flicker for me in the past. Perhaps this is fine in b4a, Android, Java?

One other related question is: What's the best way to store a list of data? I currently hold a List where each member is a custom type which stores things like position X&Y, Velocity X&Y etc. Is there a performance penalty in using a generic list? I'll be iterating over it a lot with the list storing Objects so there is presumably a lot of casting going on as I access each member? Should I be storing the data as an Array that's 'strongly typed' instead? I chose a list because I want to add & remove from it easily.

Despite all my questioning, I'm loving b4a :)

Thanks for your help
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

Biscuit

Member
Licensed User
Longtime User
Thanks for your advice and the link to the smiley animation, it's working much better now. I can get around 80 fps if I just draw a single pixel bouncing around the screen, although a lot of this was achieved by removing a lot of Bitmap.GetPixel calls and pre-calculating some stuff.

Just a quick note about searching on the forum. I obviously try to find the info before posting a question so as not to waste your time but if e.g. you search for RGB (another question I asked recently) you get no results?!? This is despite 'RGB' being in the question title & content of my question and also in your response that pointed me to an existing thread. Just thinking if there is a bug in the forum search...?
 
Upvote 0

agraham

Expert
Licensed User
Longtime User
Upvote 0
Top