SVG

SVG
guide: SVG technology introduced by WeChat.
http://mp.weixin.qq.com/s?__biz=MzAwNDY1ODY2OQ==&mid=207863967&idx=1&sn=3d7b07d528f38e9f812e8df7df1e3322&scene=4#wechat_redirect
1. Concept
1) SVG, Scalable Vector Graphics, this image format It has been widely used in the front end.
W3C's explanation of SVG: http://www.w3school.com.cn/svg/svg_intro.asp
First of all, we need to explain what is a vector image and what is a bitmap image? 
    1. Vector image: SVG is an open standard text-based vector graphics description language introduced by W3C. It is an image format based on XML and specially designed for the network. SVG is a language that uses XML to describe two-dimensional graphics. , so it can directly open the xml file for modification and editing. 
    2. Bitmap image: The storage unit of a bitmap image is the pixel value of each point on the image, so the file will be relatively large, such as GIF, JPEG, PNG, etc. are all bitmap image formats.
2) Vector, in Android refers to Vector Drawable, which is the vector diagram in Android, it can be said that Vector is the implementation of SVG in Android (it does not support all SVG syntax, the supported ones are completely sufficient)
Supplement: When the Vector image was first released, it only supported Android 5.0+. Since AppCompat 23.2, Vector can be used on all systems above Android 2.1, just refer to com.android.support:appcompat-v7:23.2.0 or above version will do. (The so-called compatibility is also a pitiful compatibility, that is, the lower version does not actually use SVG, but generates PNG images) 2. Vector Drawable: When Android 5.0 was released, Google provided Vector support, namely: Vector Drawable class. Compared with ordinary Drawables, Vector Drawable has the following advantages: (1) Vector images can be automatically adapted, and there is no need to set different pictures by resolution. (2) Vector images can greatly reduce the volume of images. The same image, implemented with Vector, may only be a few tenths of PNG. (3) It is easy to use, and many design tools can directly export SVG images, thus converting them into Vector images with powerful functions. (4) Very complex animations can be achieved without writing a lot of code. Mature and stable, the front end has been widely used. 1) Introduction to Vector syntax By using its Path tag, almost all other tags in SVG can be implemented. Although it may be a little more complicated, these things can be done by tools, so don't worry about it being complicated to write. (1) The path instruction parsing is as follows: M = moveto(MX,Y) : Move the brush to the specified coordinate position, which is equivalent to moveTo() in android Path















L = lineto(LX,Y): Draw a straight line to the specified coordinate position, equivalent to lineTo() in android Path
H = horizontal lineto(HX): Draw a horizontal line to the specified X coordinate position 
V = vertical lineto(VY): Draw a vertical line to the specified Y coordinate position 
C = curveto(C X1,Y1,X2,Y2,ENDX,ENDY): cubic Bezier curve 
S = smooth curveto(S X2,Y2,ENDX,ENDY) The same cubic Bezier Curve, smoother 
Q = quadratic Belzier curve(QX,Y,ENDX,ENDY): Quadratic Bezier curve 
T = smooth quadratic Belzier curveto(T ENDX,ENDY): Map the same quadratic Bezier curve, smoother 
A = elliptical Arc(A RX,RY,XROTATION,FLAG1,FLAG2,X,Y): arc, equivalent to arcTo()
Z = closepath(): close the path (the link start and end points will be drawn automatically)
Note, 'M' handles , just moved the brush and didn't draw anything.


Note: 1. Regarding these grammars, developers do not need to be proficient in all of them, but can understand them. These path tags and data generation can be handed over to tools to achieve.
(General artisans will do it for you! PS, Illustrator, etc. all support exporting SVG images)
2. Programmers: There is no need to learn to use these design tools, developers can use some tools to convert some basic images by themselves, such as: http://inloop.github.io/svg2android/ 
3. You can also use an SVG editor to write SVG images, for example: http://editor.method.ac/ (perfect match! You can use http://editor.method.ac/ to generate SVG images first, and then Generate VectorDrawable xml code with http://inloop.github.io/svg2android/)

4. Use the AndroidStudio plugin to complete the SVG addition (Vector Asset Studio). Details: http://www.jianshu.com/p/d6c39f2dd5e7

AS will automatically generate compatibility images (higher versions will generate SVG images of xxx.xml; lower versions will automatically generate xxx.png images)

5. Some websites can find SVG resource
SVG download address: http://www.shejidaren.com/8000-flat-icons.html
 http://www.flaticon.com/


3. Static Vector image
1) Generate image
For example : A picture we generated with as is as follows:

<vector android:alpha="0.78" android:height="24dp"

android:viewportHeight="24.0" android:viewportWidth="24.0"

android:width="24dp" xmlns:android="http://schemas.android.com/apk/res/android">
<path android:fillColor="#FF000000" android:pathData="M19.35,10.04C18.67,6.59 15.64,4 12,4 9.11,4 6.6,5.64 5.35,8.04 2.34,8.36 0,10.91 0,14c0,3.31 2.69,6 6,6h13c2.76,0 5,-2.24 5,-5 0,-2.64 -2.05,-4.78 -4.65,-4.96zM14,13v4h-4v-4H7l5,-5 5,5h-3z"/>
</vector>
和 (一个矩形)
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:width="200dp"
android:height="200dp"
android:viewportHeight="500"
android:viewportWidth="500">


<path
android:name="square"
android:fillColor="#000000"
android:pathData="M100,100 L400,100 L400,400 L100,400 z"/>


</vector>
Explain several tags in the header:
android:width \ android:height: define the width and height of the image
android:viewportHeight \ android:viewportWidth: define the proportion of the image to be divided, such as 500 in the example, that is, divide the image of 200dp size It is divided into 500 copies, and the coordinates in the Path label are all used in the coordinate system divided here.
This has a very good effect, that is, the image size is separated from the image, and the image size can be modified at will without modifying the coordinates in PathData. 2) Use pictures, just use them as ordinary pictures. <ImageView android:id="@+id/iv" android:layout_width="wrap_content" android:layout_height="wrap_content" app:src="@drawable/vector_image"/> or code setting: ImageView iv = (ImageView) findViewById (R.id.iv); iv.setImageResource(R.drawable.vector_image); iv.setBackgroundResource(R.drawable.vector_image)         If it is a Button, you can set the selector (write two SVG Drawables) <?xml version=" 1.













<selector xmlns:android="http://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/selector1" android:state_pressed="true"/>
<item android:drawable="@drawable/selector2"/>
</selector>


4. Dynamic Vector

Dynamic Vector is the essence of Android Vector Drawable

5. Performance problems of VectorDrawable:
1) The drawing efficiency of Bitmap is not necessarily higher than that of Vector. They have a certain balance point. When Vector is relatively simple, its efficiency must be higher than Bitmap. Therefore, in order to ensure the high efficiency of Vector Efficiency, Vector needs to be simpler, and PathData is more standard and streamlined. When the Vector image becomes very complex, Bitmap needs to be used instead.
2) Vector is suitable for small ICONs such as ICON, Button, and ImageView icons, or is needed For animation effects, since Bitmap has a cache function in GPU, but Vector does not, Vector image cannot be redrawn frequently.
3) When Vector image is too complex, not only should pay attention to drawing efficiency, but initialization efficiency is also an important factor to be considered.



Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=325581120&siteId=291194637
SVG