首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >有没有一种通过设置开始和结束向量来自动激活VectorDrawable的方法?

有没有一种通过设置开始和结束向量来自动激活VectorDrawable的方法?
EN

Stack Overflow用户
提问于 2016-08-22 02:03:58
回答 1查看 4.3K关注 0票数 6

AnimatedVectorDrawable

我想知道是否可以在Android中创建两个向量绘图,并从第一个向量(第二个矢量)中自动将动画化。

有点像ViewTransitonTransitionManager.go

EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2016-08-25 12:01:47

有一个可绘制的向量(@drawable/vector.xml):

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:viewportWidth="170"
    android:viewportHeight="170"
    android:width="500dp"
    android:height="500dp">
    <path
        android:name="my_vector_path"
        android:fillColor="#FF000000"
        android:pathData="M85,40
                          c10,0 20,0 30,0
                          c0,-5 -10,-20 -30,-20
                          c-20,0 -30,15 -30,20
                          c10,0 20,0 30,0"
    />
</vector>

创建一个动画向量(@drawable/animated_vector.xml):

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
                 android:drawable="@drawable/vector" >
    <target
        android:name="my_vector_path"
        android:animation="@anim/vector_animation"
    />
</animated-vector>

创建动画(@anim/vector_animation.xml):

代码语言:javascript
运行
复制
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <objectAnimator
        android:duration="2000"
        android:propertyName="pathData"
        android:valueType="pathType"
        android:valueFrom="M85,40
                           c10,0 20,0 30,0
                           c0,-5 -10,-20 -30,-20
                           c-20,0 -30,15 -30,20
                           c10,0 20,0 30,0"
        android:valueTo="M108,35
                         c5.587379,-6.7633 9.348007,-16.178439 8.322067,-25.546439
                         c-8.053787,0.32369 -17.792625,5.36682 -23.569427,12.126399
                         c-5.177124,5.985922 -9.711121,15.566772 -8.48777,24.749359
                         c8.976891,0.69453 18.147476,-4.561718 23.73513,-11.329308"/>
</set>

动画包含向量路径的fromto值。

最后,在代码中启动动画:

代码语言:javascript
运行
复制
ImageView imageView = (ImageView) findViewById(R.id.image_view);
AnimatedVectorDrawable animatedVectorDrawable =
        (AnimatedVectorDrawable) getDrawable(R.drawable.animated_vector);
imageView.setImageDrawable(animatedVectorDrawable);
animatedVectorDrawable.start();
票数 8
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/39070491

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档