Show parsing SVG

Source: Internet
Author: User

G Company code shows SVG:

Svgparserrenderer drawable =Newsvgparserrenderer (context, String svgcontent); String svgcontent= <?xml version= "1.0" encoding= "Utf-8"? ><! DOCTYPE svg public "-//W3C//DTD svg 1.1//en" "Http://www.w3.org/Graphics/SVG/1.1/DTD/svg11.dtd" ><svg version= " 1.1 "id=" Layer _1 "xmlns=" Http://www.w3.org/2000/svg "xmlns:xlink=" Http://www.w3.org/1999/xlink "x=" 0px "y=" 0px "width=" 48px "height=" 48px "viewbox=" 0 0 "enable-background=" new 0 0 "xml:space=" preserve "><path fill=" #B0B0B0 " D= "m3.494,24c0-0.587,0.476-1.063,1.063-1.063h38.886c0.588,0,1.063,0.476,1.063,1.063l0,0 C0, 0.587-0.475,1.062-1.063,1.062h4.557c3.97,25.063,3.494,24.587,3.494,24l3.494,24z "/><g><g><g ><path fill= "#B0B0B0" d= "m3.806,23.249c0.415-0.414,1.088-0.414,1.502,0l17.054,17.053c0.415, 0.415,0.415,1.087,0,1.503l0,0 c-0.415,0.414-1.088,0.414-1.503,0l3.806,24.751c3.391,24.336,3.391,23.665,3.80 6,23.249l3.806,23.249z "/></g><g><path fill=" #B0B0B0 "d=" m22.361,6.196c0.415,      0.415,0.415,1.087,0,1.502l5.308,24.751c-0.414,0.415-1.087,0.415-1.502,0l0,0          c-0.415-0.415-0.415-1.087,0-1.503l20.858,6.196c21.273,5.781,21.946,5.781,22.361,6.196l22.361,6.196z "/></ G></g></g></svg>(Svgview) msvgview.setsvgrenderer (drawable,NULL);<Com.trevorpage.tpsvg.SVGView Android:id= "@+id/siv_topicon"Android:layout_width= "@dimen/abs__shortcut_svg_size"Android:layout_height= "@dimen/abs__shortcut_svg_size"android:layout_gravity= "Center" ></com.trevorpage.tpsvg.SVGView> Public classSvgviewextendsView {} Public classSvgparserrendererextendsDefaultHandler {}svgview.setsvgrenderer (Svgparserrenderer,NULL);

Parsing of SVG:

Second, the analysis of SVG

The SVG feature is introduced in the above mentioned Android5.0, which inevitably involves the loading and parsing of SVG images. SVG is essentially an XML file, so from an analytic perspective, the tools that parse the XML file should almost always be used to parse the SVG file.
1. Dom parsing
See Android source code can be seen, 5.0 introduced SVG did not use the DOM to parse the SVG source files, although SVG claims to fully support the DOM standard. From the DOM parsing process, I can see that DOM parsing is to load the XML file, assemble it into a DOM tree, and parse the XML file through the relationship between the nodes and the nodes. Although in general, SVG files are relatively small, but there are some very complex pictures will rise to the M level, if you need to load all the parsing, for Android system is more time-consuming, this may be the DOM is one of the reasons for Android elimination.
2. Sax parsing
The SAX (simple API-to-XML) parser is an event-based parser whose core is the event-handling pattern, which mainly revolves around event sources and event handlers. When an event source generates an event, the event handler is called to handle the corresponding processing method, and an event can be processed. When the event source invokes a particular method in the event handler, it is also passed to the event handler for the status information of the corresponding event, so that the event handler can determine its behavior based on the event information provided. The advantage of the SAX parser is that the parsing is fast and consumes less memory. Ideal for use in Android mobile devices
3. Pull Analysis
The pull parser operates in a similar manner to sax, and is an event-based pattern. The difference is that during pull parsing, we need to get the generated events and do the corresponding operations, instead of executing our code in a way that the processor triggers an event like sax. The pull parser is compact, fast, easy to use, and ideal for Android mobile devices, and the pull parser is also used to parse various XML inside the Android system.

Third, the use of SVG in Android5.0 or above version

1. How to use
Given the many advantages of SVG, Android has introduced this feature in version 5.0. The following methods are used in more than 5.0 versions:
1) Get SVG picture data
The first method, of course, is down from the Internet (usually used directly for testing), here are just a few of the most common content download site:
http://sc.chinaz.com/
Http://www.freevectors.net
http://www.freevectordownload.com/
The second is hand-made: When the real project, Method 1 must not meet the corresponding needs. At this time we can only do our own drawing (should be the art to be responsible for ^_^ | | ), the most commonly used software is PS, AI or CDR can also. Specific production methods here do not introduce, direct Internet search on the line. Here is a more detailed example: http://blog.csdn.net/tianjian4592/article/details/44733123
2) Convert normal SVG image data to Android usable data
General SVG image data is directly in the HTML or JSP can be used, Android, if you want to use SVG will need to convert the intermediate link to the vector tag including the XML file (if animated, The outside also needs to include a layer of animated-vector to reference the vector resource, the most important of which is the path element, which is the drawing trajectory during the image loading display. The specific conversion method on-line query has two kinds: one is manual rewriting, the other is the direct use of automatic conversion tool conversion (Http://inloop.github.io/svg2android/). The specific conversion rules and the corresponding symbolic meanings can be found in: Http://www.w3.org/TR/SVG11/paths.html#PathData.
3) use in the project
SVG is relatively simple to use in more than 5.0 of Android projects, using drawable controls (such as Mageview, etc.) to refer to the converted XML file resources in the second step.
2. Demo instance
Instance one: mytest (using the drawable control to reference the 5.0 new feature vector Resource)
"Key Code Summary"
Use the vector tag to include the path element
[Sharp_rect.xml]

<?xml version= "1.0" encoding= "Utf-8"?><Vectorxmlns:android= "http://schemas.android.com/apk/res/ Android "android:width=" 180DP " android:height= "320DP"  Android:viewportwidth= "android:viewportheight=
                
                  "the" > 
                 <path android:name= "Sharp_rect"  Android:fillcolor= "#000000" android:pathdata=" M 320,180 L 0,320 0,0 180,0 z "/></vector> 
                 

Use ImageView to reference vector resources Sharp_rect
[Activity_main.xml]

<relativelayout xmlns:android="Http://schemas.android.com/apk/res/android" xmlns:tools="Http://schemas.android.com/tools" android:layout_width= "match_parent" Android:layout_height= "Match_parent" Android: Paddingleft= " @dimen/activity_horizontal_margin" Android: Paddingright= " @dimen/activity_horizontal_margin" Android: Paddingtop= " @dimen/activity_vertical_margin" Android: Paddingbottom= " @dimen/activity_vertical_margin" tools: Context= @drawable/ Sharp_rect "Android:layout_width=" wrap_content "Android:layout_height=" wrap_content "/></RELATIVELAYOUT>     

Demo Accessories: Mytest.zip

Example two: "Vectorcard" uses 5.0 new features Animated-vector tag references the SVG path element in XML
"Key Code Summary"
[To_stop.xml]

<ObjectanimatorXmlns:android=" http://schemas.android.com/apk/res/android" android:duration= " android:interpolator=
           
             "@android: Interpolator/decelerate_cubic" 
            android:propertyname="Pathdata" android:valuetype=" PathType " android:repeatmode=" reverse " android:repeatcount=" 1 " android:valuefrom=" m100,100 l400,250 l100,400 l100,400 z " android:valueto=" m100,100 l400,100 l400,400 l100,400 z "/>
                        
           

[Animated_play.xml]

<?xml version="1.0" encoding="utf-8"?><animated-vector xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/play_icon"> <target android:animation="@animator/to_stop" android:name="play" /></animated-vector>

Demo Accessories: App.zip

3. Summary
Android5.0 The addition of this new feature is mainly dependent on Vctor, the corresponding class is vectordrawable and animatedvectordrawable. The former is mainly used for vector image loading, the latter is mainly used for vector animation loading. The two should be differentiated in the process of use.

Iv. Android5.0 use SVG to explore and summarize the following

After this short time of continuous exploration and experiment, according to Svg_android organization method mainly has the following two kinds:
1, based on Svg_android library
Extract a Java file similar to the one in the Svg_android library that is suitable for 5.0, and can parse SVG, and customize the SVG-like control to build the Android project (the instance uses Sax to parse the SVG file).
Corresponding example: "Svgmapview-master"
Demo Accessories: Svgmapview-master.zip

"description" This method requires the developer to rewrite the parsing function, the current instance does not support all the parsing of the SVG syntax, such as the layout of the method into the project, the need for a large number of extensions to parse or even load SVG function functions.
2, based on the JNI technology
Load and interest rate SVG work is given to the C + + process, and the upper layer uses Java calls corresponding to the. So library exposed interfaces.
Corresponding example: "Imageviewsvg"
Demo Accessories: Imageviewsvg.zip

"description" Although the scheme uses JNI technology to load and parse SVG files, it has a limited resolution and a single function, such as the need to lay out the scheme in the project, for the JNI side also need to expand a large number of SVG analytic functions. And we need to optimize the corresponding loading processing links.

V. Summary

As a whole, it is possible to apply SVG to the following versions of Android5.0 at this time. But its stability, and the subsequent workload, is one of the issues that needs our careful consideration.

Show parsing SVG

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.