📜 ⬆️ ⬇️

VectorDrawable - Part Three

Earlier in this series of articles, we looked at the implementation of VectorDrawable, using the data under the path tag in SVG. And after that, we applied several simple animations to individual path elements.

Romain Guy wrote an application that draws routes. He used the SVG path to determine the route, and then “draw” a line along this route by adjusting the dash parameter.

Since VectorDrawable supports SVG data from the path tag, can we use the same technique? Of course we can. But, in fact, we do not need it. We can achieve the same effect by manipulating some element attributes in our VectorDrawable.

Drawing the path is not suitable for the Android logo that we used in previous articles, as it consists of shaded areas, not lines. So let's start by defining a simple visible VectorDrawable path. Our figure has the shape of a star, which we received based on the SVG file . This is a relatively simple geometric figure that has a sufficient amount of side lengths to perfectly illustrate the effect we are trying to achieve in this article. Of course, you can use American state border lines like Romain.
')
res / drawable / star.xml
<vector xmlns:android="http://schemas.android.com/apk/res/android" android:viewportHeight="500" android:viewportWidth="500" android:width="500px" android:height="500px"> <group android:scaleX="5.0" android:scaleY="5.0"> <path android:name="star" android:strokeColor="@color/sa_green" android:strokeWidth="2" android:pathData="M 50.0,90.0 L 82.9193546357,27.2774101308 L 12.5993502926,35.8158045183 L 59.5726265715,88.837672697 L 76.5249063296,20.0595700732 L 10.2916450361,45.1785327898 L 68.5889268818,85.4182410261 L 68.5889268818,14.5817589739 L 10.2916450361,54.8214672102 L 76.5249063296,79.9404299268 L 59.5726265715,11.162327303 L 12.5993502926,64.1841954817 L 82.9193546357,72.7225898692 L 50.0,10.0 L 17.0806453643,72.7225898692 L 87.4006497074,64.1841954817 L 40.4273734285,11.162327303 L 23.4750936704,79.9404299268 L 89.7083549639,54.8214672102 L 31.4110731182,14.5817589739 L 31.4110731182,85.4182410261 L 89.7083549639,45.1785327898 L 23.4750936704,20.0595700732 L 40.4273734285,88.837672697 L 87.4006497074,35.8158045183 L 17.0806453643,27.2774101308 L 50.0,90.0Z" /> </group> </vector> 


All that we have here is a very simple path based on a cubic Bezier curve, which we gave the name, for easy access to it from AnimatedVectorDrawable.

AnimatedVectorDrawable itself is also pretty simple. All we need to do is apply animator / path to the specified path.

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


The most interesting is inside the animator, which is a simple Object Animator, which animates the value of the trimPathEnd attribute from an element in VectorDrawable. This attribute controls which part of the path will be drawn. It has a range from 0 (nothing is drawn) to 1 (the whole path will be drawn). It turns out that all we need to do is animate this attribute from 0 to 1. In this way, we will create the impression of drawing along our path, just like Romain in our example.

The animator that does this looks pretty simple: the animation time is set to 5 seconds, in order to clearly see what we did. The linear interpolator is also installed, because we do not want the drawing speed to change over time:

res / animator / path.xml
 <?xml version="1.0" encoding="utf-8"?> <objectAnimator xmlns:android="http://schemas.android.com/apk/res/android" android:propertyName="trimPathEnd" android:valueFrom="0" android:valueTo="1" android:duration="5000" android:valueType="floatType" android:interpolator="@android:interpolator/linear"> </objectAnimator> 


All that remains is to use AnimatedVectorDrawable in our layout (in the example code, I actually implemented everything in ViewPager to be able to switch between examples. This is not necessary).

We were able to achieve the same result as Romain, but used much less code. In fact, for this implementation, we didn’t need a single line of java-code (beyond what we wrote earlier to display and start the animation). This is the result:


This article shows how we can control the path to get some interesting effects. In the next article, we will delve further into this topic and see how we can actually manipulate the data path.

The source code for this article is available here .

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


All Articles