I'm struggling to find the best way to draw smoothly as possible some pre-calculated trajectories of physical objects on a custom SurfaceView
. So the scenario is like this:
- I have a 100k entries SQLite database that stores x,y positions of about 10 objects.
- I have to animate my objects using the data provided by the database while drawing trajectories.
- I have to be able to interact with the
SurfaceView
zomming and panning appropriately through touch events.
What strategy is the best?
(a) Pre loading data and store them in 200k entries float arrays.
- (a1) Then, at every
draw(Canvas canvas)
call extract the segment of the array with data from the initial time to the actual time, construct aPath
with these points and draw it. - (a2) Same as (a1) but instead of using
Path
, call theCanvas.drawLines()
function. That function require a float array with repetition of points so that the end point of a line will be the start point of the next line. This function may be faster thandrawPath()
but the array must be constructed at every iteration... - (a3) Otherwise, save drawings in a
Bitmap
so that at every iteration I have to draw only the entireBitmap
+ the last segment of the trajectories. Only when theMatrix
change redraw all stuff with the newMatrix
on theBitmap
. This slow down only when touch events happens to change theMatrix
requesting the entire redrawing.
- (a1) Then, at every
(b) Live read data from database with a concurrent
Thread
. Then, save in a float array only the positions read. This may be a little more heavy because of the additional thread and live querying but I have to handle only smaller arrays of float (only the part I'm interested in and not all hystory).- (b1) Redraw all at every
draw(Canvas canvas)
call as in (a1) usingPath
- (b2) Redraw all at every
draw(Canvas canvas)
call as in (a2) usingdrawLines()
- (b3) Save in a
Bitmap
to optimize drawings as in (a3)
- (b1) Redraw all at every
Options (a) are better because I can smoothly draw lines using all points in database while with options (b) if the process slow down the live updating system can skip some points making the drawing less smooth. Options (b) are better bacause of the simplicity and readability of code.
I only tested options (b) but I'm wondering if I have to give a try to options (a). Keep in mind that with the trajectories growing up the draw process becomes very slow so optimization is necessary. The Bitmap strategy can solve partially the problem with long trails but I can't have a fluid experience when touch events happens...