Saving inking data and restoring it #118
Replies: 2 comments 4 replies
-
Hello! This is a really great point, while saving to a bitmap is fine if you just want to save what the drawing looks like, it doesn’t help when loading and editing the data later. We encountered this same scenario when creating our note editor hero app - TwoNote which also uses the InkView. TLDR - I’ll go over some of the complexities of saving ink at a higher quality level, but if you’re mainly interested in code, there’s some code at the bottom you can check out which allows you to edit the InkSDK to meet your needs. To save and load notes as higher quality inking data, there are a couple of approaches. With all of these, we’re looking to convert the ink on our canvas into some other data type that can be persisted. There are a couple of common approaches to this issue.
Regardless of the persistence type (e.g. saving in a file, saving in a Room database, etc.), we can often only save simple data types. Anything with references to a Context, Activity, or a class with specific references to the application instance are difficult to persist without causing memory leaks. Additionally, any interfaces or abstract classes with extensible properties require additional overhead to serialize. For these reasons, the code examples I’m mentioning won’t include serializing DynamicPaintHandlers. It’s not impossible but complicates the explanation of saving and loading data. Alright, the first step in saving ink data is deciding what we want to preserve when we save the ink. For this example, we’ll save the following for each stroke:
We can package this into a single class called a Brush (I’ll be doing the code in Kotlin to align with the project, but the same concepts apply to Java)
Notice that by marking it as Serializable, we can easily convert it to JSON in Kotlin. Java has many tools that can be used for Serialization, one of which is Gson. The returned value from the
To load the canvas with a serialized ink string, we simply need to deserialize the data, iterate through all the brush strokes, and draw them on the canvas!
There are a couple of small other configurations needed to get this working, but overall, that’s the main idea! I created a branch that you can check out for an example on how to load/save using the InkSDK sample. Also definitely check out our note taking hero app sample TwoNote. It also loads and saves serialized ink data using the InkSDK (although the implementation is a bit more complex). Hope this helps! |
Beta Was this translation helpful? Give feedback.
-
We just merged in a change to address saving and loading ink data without users having to manually copy code and change it themselves. If you're interested, feel free to check it out by updating the version number in gradle! implementation "com.microsoft.device:ink:1.0.0-alpha5" |
Beta Was this translation helpful? Give feedback.
-
Hi! I'm trying to use your InkView for a Surface Duo project, and I can't figure out how to retrieve inking data, and then redraw it (in order to make inking persist after closing and reopening the app). I can save as a bitmap, but am struggling to redraw the bitmap into the inkview. But it also feels like there should be higher quality inking data than bitmap. I'm still operating in java (sorry).
I can tell you what doesn't work for redrawing a bitmap, and it's this:
(But as I said, I'd prefer it be more specific data than a bitmap)
Beta Was this translation helpful? Give feedback.
All reactions