我正在编写一个Android应用程序,想播放一个简单的SVG动画。我知道Android不支持SVG;那么我的选择是什么?
从Android Lollipop(API级别21)开始,可以使用AnimatedVectorDrawable实现动态矢量图。
还有一些工具可帮助动画化矢量可绘制项,如shapeshifter和该工具作者的博客文章《图标动画技术介绍》。
Add your downloaded XML file to your project and see how it looks. Heres an example of an VectorDrawable prepared for a rotation and path morph animation:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<group
android:name="rotationGroup"
android:pivotX="300.0"
android:pivotY="300.0"
android:rotation="45.0" >
<path
android:name="v"
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</group>
3. 现在创建一个AnimatedVectorDrawable,在其中引用创建的VectorDrawable中的rotationGroup和path morph。
<?xml version="1.0" encoding="UTF-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android" android:drawable="@drawable/vectordrawable">
<target android:name="rotationGroup" android:animation="@anim/rotation" />
<target android:name="v" android:animation="@anim/path_morph" />
</animated-vector>
<objectAnimator
android:duration="6000"
android:propertyName="rotation"
android:valueFrom="0"
android:valueTo="360" />
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="3000"
android:propertyName="pathData"
android:valueFrom="M300,70 l 0,-70 70,70 0,0 -70,70z"
android:valueTo="M300,70 l 0,-70 70,0 0,140 -70,0 z"
android:valueType="pathType"/>
</set>
start()
。