使用 AnimatedVectorDrawables,可以将某些动画应用到 vectordrawable 的一部分(请参阅此处)。例如考虑 vectordrawable,称为vectordrawable.xml:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="64dp"
android:width="64dp"
android:viewportHeight="600"
android:viewportWidth="600" >
<path
android:name="path"
android:fillColor="#000000"
android:pathData="M300,70 l 0,-70 70,70 0,0 -70,70z" />
</vector>
这是一个包含单个路径(线)的 vectordrawable。所需的动画将在一个单独的文件中,称为animator.xml,例如:
<set xmlns:android="http://schemas.android.com/apk/res/android">
<objectAnimator
android:duration="4000"
android:propertyName="trimPathEnd"
android:valueFrom="0"
android:valueTo="1" />
</set>
这将是显示正在绘制的线的动画。然后,这两个 xml 文件将输入第三个动画矢量可绘制 xml 文件:
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
android:drawable="@drawable/vectordrawable">
<target
android:animation="@anim/animator"
android:name="path" />
</animated-vector>
所以问题是,如果您的 vectordrawable 路径包含大量路径,您想同时为它们设置动画怎么办?您可以长期这样做,并且必须为每个路径指定不同的名称,然后在您的动画矢量可绘制文件中使用相同的动画师定位每个路径。但是如果你有超过 20 条路径,这将需要很长时间并且是一个混乱的解决方案。
您可以选择将一组路径包含在一个组中,然后定位到一个组。但是组的动画与路径的动画不同,即不能为组设置动画trimPathEnd,因此您不能将此动画应用于一组路径。
我也想建议RichPath这样做,但我不建议RichPathAnimator.animate()
在长循环内调用,您可以以更好的方式获得相同的结果,请检查此完整示例中的第 3 步:
1- 普通vector_drawable.xml
6 条路径:
<vector xmlns:android="http://schemas.android.com/apk/res/android"
android:height="256dp"
android:viewportHeight="36.0"
android:viewportWidth="36.0"
android:width="256dp">
<path
android:pathData="M19.5,6.2l1.3-1.3c0.2-0.2,0.2-0.5,0-0.7c-0.2-0.2-0.5-0.2-0.7,0l-1.5,1.5C17.9,5.2,16.9,5,16,5c-1,0-1.9,0.2-2.7,0.6l-1.5-1.5c-0.2-0.2-0.5-0.2-0.7,0c-0.2,0.2-0.2,0.5,0,0.7l1.3,1.3C11,7.3,10,9,10,11h12C22,9,21,7.2,19.5,6.2z"
android:strokeColor="#A4c639"
android:strokeWidth="0.5"
android:trimPathEnd="1"/>
<path
android:pathData="M13.9,8.9H13V8h0.9V8.9z"
android:strokeColor="#A4c639"
android:strokeWidth="0.5"
android:trimPathEnd="1"/>
<path
android:pathData="M19,8.9h-0.9V8H19V8.9z"
android:strokeColor="#A4c639"
android:strokeWidth="0.5"
android:trimPathEnd="1"/>
<path
android:pathData="M10,22c0,0.5,0.4,1,1,1h1v3.5c0,0.8,0.7,1.5,1.5,1.5s1.5-0.7,1.5-1.5V23h2v3.5c0,0.8,0.7,1.5,1.5,1.5s1.5-0.7,1.5-1.5V23h1c0.5,0,1-0.5,1-1V12H10V22z"
android:strokeColor="#A4c639"
android:strokeWidth="0.5"
android:trimPathEnd="1"/>
<path
android:pathData="M24.5,12c-0.8,0-1.5,0.7-1.5,1.5v7c0,0.8,0.7,1.5,1.5,1.5s1.5-0.7,1.5-1.5v-7C26,12.7,25.3,12,24.5,12z"
android:strokeColor="#A4c639"
android:strokeWidth="0.5"
android:trimPathEnd="1"/>
<path
android:pathData="M7.5,12C6.7,12,6,12.7,6,13.5v7C6,21.3,6.7,22,7.5,22S9,21.3,9,20.5v-7C9,12.7,8.3,12,7.5,12z"
android:strokeColor="#A4c639"
android:strokeWidth="0.5"
android:trimPathEnd="1"/>
</vector>
2-RichPathView
在您的layout
insead 中使用ImageView
:
<com.richpath.RichPathView
android:id="@+id/richPathView"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
app:vector="@drawable/vector_drawable" />
3- 同时为所有路径设置动画。
RichPathView pathView = findViewById(R.id.richPathView);
RichPath[] paths = new RichPath[6];
for (int i = 0; i < paths.length; i++) {
paths[i] = pathView.findRichPathByIndex(i);
}
RichPathAnimator
.animate(paths)
.trimPathEnd(0, 1)
.duration(1600)
.start();
另外,如果我们有这样的 API 会更干净pathView.findAllRichPaths()
,我在这里打开了一个问题
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句