How to Serialize an Object of Android.Graphics.Path

Can I serialize the paths drawn on canvas for redrawing the paths on relaunch of the application

I took the reference from
another question here I made few modifications and it worked for me quite well.


To understand we can think we just need to store a map of Actions and Points. We need path.moveTo(int x, int y), path.lineTo(int x, int y),
path.quadTo(int x1, int y1, int x2, int y2) and path.reset() methods for scribbling.

Actions in this case being: lineTo, moveTo, quadTo, reset and points being the corresponding points.

I took two arrays 1 for x and another for y. For quadTo(x1,y1,x2,y2) we need two points, for reset we need no points and for other we need a single point(x,y).
We can think actions being the keys and {arrayX[], arrayY[]} being the value for an action. For actions as lineTo and moveTo size of the arrayX[] and arrayY[] is 1 and for quadTo the size is 2 and for reset the size is 0 (or we can have both the arrays null ) as in that case we need no points. We just need to be careful while retrieving the values of the points from the array corresponding to the Action key. when action is lineTo we just draw the path on the canvas.

Trying to store android.graphics.Path values persistently

I wouldn't recommend to store this kind of data in SharedPreferences as you will have to implement many thinks like getCount() etc which SQLite already offers.

Did you think about using Serialization for your Path objects? You could easily store them in your database afterwards.



Related Topics



Leave a reply



Submit