VectorDrawable
class. Objects of this class are resources and are described using the XML language, they are placed in the res/drawable
our project. The root element of the class is the vector
tag. The vector
tag has several attributes, two groups of attributes are required, which determine the size of our drawable
. I'm talking about the width
and height
attributes — they are specified using some units of measurement (their standard set for Android is dp
, sp
and px
), as well as viewportWidth
and viewportHeight
— they are specified without units. With the help of width
and height
we can specify the physical size that our drawable
will take on the screen, and the viewport
can be compared with the window through which we look at our image. viewport
sizes can be the same or different from the image size. At the same time, by changing the size of the viewport
, we can change the area occupied by the figure inside the drawable
.viewport
does not have the ability to set pivotPoint
coordinates and resizing the viewport
will be counted from the upper left corner. Also, the vector
element has a number of other attributes, the meaning of which is clear from the name. <vector android:name android:tint android:tintMode android:autoMirrored android:alpha> <vector/>
Path
tag. The most important attribute for the Path
element is the pathData
attribute. Its value is a string of characters separated by commas. These characters are nothing but SVG format commands. <path android:pathData="M141.4,188.2H51V329c0,15.4,16.4,15.5,16.4, 15.5l20.8,0v19.7h0c0,0.3,0,0.5,0,0.8c0,11,8.9,19.9,19.9,19.9c11, 0,19.9-8.9,19.9-19.9c0-0.3,0-0.5,0-0.8h0v-19.7l13.4,0l13.4,0v19.7h0c0, 0.3,0,0.5,0,0.8c0,11,8.9,19.9,19.9,19.9c11,0,19.9-8.9,19.9-19.9c0-0.3, 0-0.5,0-0.8h0v-19.7l20.8,0c0,0,16.4,0,16.4-15.5V188.2H141.4z"/>
Path
object. This object will have an attribute d
, the value of which will be a string with commands identical to those that we can find as the value of the pathData
attribute of the pathData
object in the VectorDrawable resource. This circumstance allows us to easily create VectorDrawable resources — to do this, simply copy the command set from an SVG file to our VectorDrawable resource. <path id="shapePath1" d="M229.1,174.2 C226.2,165.5 222.1,157.5 216.9,150.3 C208.4,138.3 197.6,129.5 184.7,123.6 L202.4,98.5 C202.6,98.2 202.7,98 202.7,97.5 C202.7,96.8 202.3,96.2 201.4,95.7 C200.6,95.1 199.8,94.9 199.2,95.1 L198.4,95.6 L180.1,121.7 C168.8,117.5 156,115.2 141.6,115.2 C127.4,115.2 114.7,117.2 103.6,121.4 L85.5,95.5 L84.7,95 C84.1,94.9 83.3,95 82.5,95.6 C81.6,96.1 81.2,96.7 81.2,97.4 C81.2,97.9 81.3,98.1 81.5,98.4 L98.9,123 C85.8,128.7 74.8,137.7 66.2,149.8 C60.9,157 56.8,165 53.8,173.7 C52.5,177.5 51.7,180.8 51.2,183.5 L231.9,183.5 C231.3,181 230.4,177.9 229.1,174.2 Z" />
<path android:pathData android:fillColor, android:fillAlpha android:strokeColor, android:strokeWidth, android:strokeAlpha android:trimPathStart, android:trimPathEnd, android:trimPathOffset android:strokeLineCap, android:strokeLineJoin, android:strokeMiterLimit />
Group
element. The ability to combine several Path objects into a group allows not only to organize the structure of our drawable-resource, but also gives the opportunity to apply group settings to a set of objects, but most importantly, the group can be used for animation, which will be discussed later.Path
objects, which can be used to define our own color, transparency, stroke, and indicate the position on the screen. Path
objects can be combined into groups, for which, again, you can specify a number of group attributes. The most interesting is that all attributes can be zanimirovat, while to apply one animation to a set of Path
objects, it is convenient to combine these objects into a group. <group android:name android:rotation android:pivotX, pivotY android:scaleX, scaleY android:translateX, translateY> <group/>
AnimatedVectorDrawable
class, which allows you to create animations for the VectorDrawable classes. AnimatedVectorDrawable is a PropertyAnimation extension that allows us to create animations based on changing the values ​​of any object attributes. AnimatedVectorDrawable files also belong to application resources and are created using XML. The root element of a vector animation resource is the animated
- vector tag. This tag has only one drawable
attribute in which we specify the name of the VectorDrawable resource for which we want to create an animation. Inside the animated-vector
element are target
objects that are targets for animations. The most remarkable is that the animated-vector
can contain more than one target
. This fact makes it possible to create complex animations, allowing you to animate different parts of the image with different animations, creating complex animations. <animated-vector android:drawable="@drawable/vector_drawable"> <target android:name="head" android:animation="@anim/headAnimation" /> <target android:animation="@anim/eyes_anim" android:name="eyes"/> </animated-vector>
target
and create three animations for them.target
element has two main attributes — name
, in which we specify the name of the Path
object or the Group
object to be animated, and animation
, where we send a link to the animation resource located in the res/anim
folder. <set> <objectAnimator android:duration="400" android:propertyName="pathData" android:valueType="pathType" android:valueFrom="@string/pathFrom" android:valueTo="@string/pathTo" /> </set>
animator-set
, with an objectAnimator
object inside. As the value of the propertyName
attribute, we can specify any attribute of the Path
or Group
object from VectorDrawable. Depending on the attribute type, we must specify the corresponding value type in the valueType
attribute.property
, for which you can create an animation - an animation of the value of the pathData
attribute of the Path
object. To do this, a new pathData
value appeared in the propertyName
field and the corresponding valueType
value, valueType
, pathType
. In this case, the old fields valueFrom
and valueTo
take as arguments two strings with sets of commands describing the initial and final shape of the figure, respectively. Thanks to this new type of animations, you can create complex animations that transform the shape of a shape that was previously unavailable in Android — for example, morphing animations, which transform the Play button into the Pause button. Google itself recommends saving data with path commands in string resources.Animatable
interface and to start the animation, we request the Drawable
, bring it to the Animatable
and call the start()
method. ((Animatable) imageView.getBackground()).start();
Animatable
interface Animatable
extremely simple - there are only three methods: isRunning()
, start()
, stop()
. Thus, there is no possibility of setting the AnimationListener
. Also there is no possibility to transfer our vector animation to AnimatorSet
. Therefore, the direct ability to create a choreography of animations, or run some actions to complete the animation is missing. More precisely, it was absent - in version 23 of the Android SDK, the Animatable
interface was expanded with the new Animatable2
interface, which allows AnimationCallback to be recorded for our vector animation.Handler.postDelayed()
method.viewportHeight, viewportWidth, fillColor, pathData, valueType
with the namespace of the VectorCompat library.VectorDrawable.getDrawable(), AnimatedVectorDrawable.getDrawable()
and ResourcesCompat.getDrawable()
. The third method is preferable for two reasons: firstly, when using it, there is no difference which class object we are trying to load - VectorDrawable or AnimatedVectorDrawable, and secondly, on devices with Lollipop and higher, it will automatically use the native method from the Android SDK.viewPort
- despite the fact that when viewed in Illustrator, the image is centered relative to the viewPort
, when you open the exported SVG file, for example, in the browser, it is shifted to the side. Therefore, to use SVG files from Illustrator, they must first be saved in another program, for example, free Inkscape .Path
objects. Therefore, primitive figures, it is necessary to transform on the way. In Sketch, this is done with the command Layer> Paths> Vectorize stroke, and in Illustrator> Object> Expand.. , d — . , pathData
VectorDrawable.
, Android Studio , . 1.4 Vector Asset Studio — SVG Vector Drawable SVG . res/drawable
File > New > Vector Asset.

1.4 Preview SVG Illustrator. ( ) 21. Material Design. SVG , , . 1,4 Android Gradle png- .
SVG , Android . png , apk-. vector!
Rambler.Android , 10 .
, ,

. , d
— . , pathData
VectorDrawable.
, Android Studio , . 1.4 Vector Asset Studio — SVG Vector Drawable SVG . res/drawable
File > New > Vector Asset.

1.4 Preview SVG Illustrator. ( ) 21. Material Design. SVG , , . 1,4 Android Gradle png- .
SVG , Android . png , apk-. vector!
Rambler.Android , 10 .
, ,

. , d
— . , pathData
VectorDrawable.
, Android Studio , . 1.4 Vector Asset Studio — SVG Vector Drawable SVG . res/drawable
File > New > Vector Asset.

1.4 Preview SVG Illustrator. ( ) 21. Material Design. SVG , , . 1,4 Android Gradle png- .
SVG , Android . png , apk-. vector!
Rambler.Android , 10 .
, ,

. , d
— . , pathData
VectorDrawable.
, Android Studio , . 1.4 Vector Asset Studio — SVG Vector Drawable SVG . res/drawable
File > New > Vector Asset.

1.4 Preview SVG Illustrator. ( ) 21. Material Design. SVG , , . 1,4 Android Gradle png- .
SVG , Android . png , apk-. vector!
Rambler.Android , 10 .
, ,

. , d
— . , pathData
VectorDrawable.
, Android Studio , . 1.4 Vector Asset Studio — SVG Vector Drawable SVG . res/drawable
File > New > Vector Asset.

1.4 Preview SVG Illustrator. ( ) 21. Material Design. SVG , , . 1,4 Android Gradle png- .
SVG , Android . png , apk-. vector!
Rambler.Android , 10 .
, ,

Source: https://habr.com/ru/post/267073/
All Articles