Is SVG conversion an efficient way to store Path, Paint, and Matrix objects?

In my Android application, I created an SVG image converter class. It parses the SVG XML data and converts it to the corresponding Path, Paint, and Matrix objects, which can then be applied to the Canvas. Using this class, I then implemented View, which uses my SVG converter to draw the images that I created in Inkscape on the screen. So far, so good. (I understand that writing your own SVG converter would be possible to think about rethinking the wheel, given that it was done before, but for me this is a useful training exercise in my first Android application and, I hope, will give me additional flexibility.)

The purpose of using SVG is that I can quickly and easily create a variety of graphic sensor designs. Each gauge usually consists of a section of graphics that you only need to draw once (for example, the front side and legends), plus a graphic that is regularly updated (pointer angle, text with a numerical value).

Currently, my View gauge is not very efficient because every time onDraw () is called, my SVG class is called to crash through the entire SVG file to create all the vector data for the Canvas.

I would like to do this in order to have an intermediate storage of vector data so that only the SVG XML file needs to be parsed. Therefore, I thought that View can lazily initialize itself from the SVG file on the first onDraw (), and then save all the resulting Paths, Paint, and Matrix objects to different lists. Then, on each subsequent onDraw (), I simply pull them out of the list (s) and wade through them onto the canvas.

An additional advantage of this would be the use of separate lists for storing sections of vector graphics that are "moved", for example. gauge gauge. I thought about this by assigning a specific “magic” ID to the group of paths in Inkscape that represent the pointer; the SVG parser class would then recognize that this separate group needs to be separately stored in the "fixed" graph. Then, when I need to update the pointer angle according to the measurement data, the view will only apply rotational transformation to this group of vector data. In fact, I'm thinking of making it so that the moving pointer graphics are actually drawn in the child view, so that only the child view is redrawn when the pointer needs to be updated.

The ultimate goal of all this is that I (or maybe even users) could run an image visualization program like Inkscape and quickly create a new caliber widget design. I insert some metadata to indicate which bits of the graphic should be processed according to the measurement data.

Instead of asking for a solution to the problem as such, I would like to hear opinions about what I am doing here and whether what I propose can be done in a much more optimized way. Could memory be very inefficient for caching groups of Path and Paint objects?

Also, as soon as that is enough (!), I will gladly publish my SVG class if anyone finds it useful.

+6
source share
1 answer

Introduce and measure! Start with a simple approach - save the analyzed vector data in lists in memory. If memory usage and rendering speed are acceptable: the problem is resolved. If not, try other things and measure. Some ideas:

  • parse SVG once, make bitmaps once, reuse bitmaps
  • visualize SVG as part of the build process, bitmap bitmaps with the application

Except in the simplest cases, we are not very well versed in how effective a particular technique is. Thus, the popular saying that premature optimization is the root of all evil.

+2
source

Source: https://habr.com/ru/post/886730/


All Articles