首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >如何添加动画矢量绘图动画?

如何添加动画矢量绘图动画?
EN

Stack Overflow用户
提问于 2018-12-14 12:26:40
回答 2查看 15.3K关注 0票数 8

我试图在我的android应用程序中将一个向量路径动画到另一个路径进行测试,但它不能正常工作。屏幕上没有显示动画,也没有显示任何动画。

我的向量文件是:

代码语言:javascript
复制
    <vector xmlns:android="http://schemas.android.com/apk/res/android"
    android:width="8dp"
    android:height="5dp"
    android:viewportWidth="8"
    android:viewportHeight="5">
  <path
      android:name="redot"
      android:pathData="M2.5,2.5L6,2.5"
      android:strokeWidth="4"
      android:fillColor="#00000000"
      android:strokeColor="#E61B1B"
      android:fillType="evenOdd"
      android:strokeLineCap="round"/>
</vector>

和我的VectorAnimation文件是:

代码语言:javascript
复制
    <?xml version="1.0" encoding="utf-8"?>
    <animated-vector xmlns:tools="http://schemas.android.com/tools"
        xmlns:android="http://schemas.android.com/apk/res/android"
        tools:targetApi="lollipop"
        android:drawable="@drawable/ic_reddot">


        <target
            android:animation="@anim/redanim"
            android:name="redot"/>

    </animated-vector>

我在anim文件夹中的动画文件是:

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>

    <set xmlns:android="http://schemas.android.com/apk/res/android">

        <objectAnimator
            android:duration="10000"
            android:propertyName="pathData"
            android:valueFrom="M2.5,2.5L6,2.5"
            android:valueTo="M2.5,2.5L31,2.5"
            android:valueType="pathType" />

    </set>

和最后我的MainActivityCode如下所示:

代码语言:javascript
复制
    public class MainActivity extends AppCompatActivity {
    private TextView testObj;
    private ImageView reddot;
    private AnimatedVectorDrawable animation;

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    @Override
    protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_main);
      // testObj = (TextView) findViewById(R.id.testObj);
     // testObj.setVisibility(View.INVISIBLE);
    reddot = (ImageView) findViewById(R.id.reddot);

               Drawable d =  reddot.getBackground();
            if (d instanceof AnimatedVectorDrawable) {
                Log.d("testanim", "onCreate: instancefound" );
                animation = (AnimatedVectorDrawable) d;
                animation.start();
            }

        }
    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2018-12-17 11:57:13

使用变形器工具并导出由变形器生成的动画矢量绘图文件。将此文件添加到可绘制文件夹中,然后将其作为背景添加到要动画的图像视图中。

我的avd_anim.xml文件:

代码语言:javascript
复制
<animated-vector
xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:aapt="http://schemas.android.com/aapt">
<aapt:attr name="android:drawable">
    <vector
        android:name="vector"
        android:width="80dp"
        android:height="12dp"
        android:viewportWidth="80"
        android:viewportHeight="12">
        <path
            android:name="path"
            android:pathData="M 6 6 L 74 6"
            android:strokeColor="#e61b1b"
            android:strokeWidth="12"
            android:strokeLineCap="round"
            android:fillType="evenOdd"/>
    </vector>
</aapt:attr>
<target android:name="path">
    <aapt:attr name="android:animation">
        <objectAnimator
            android:propertyName="pathData"
            android:duration="1000"
            android:valueFrom="M 6 6 L 74 6"
            android:valueTo="M 6 6 L 1 6"
            android:valueType="pathType"
            android:interpolator="@android:anim/linear_interpolator"/>
    </aapt:attr>
</target>

我的activity_main.xml

代码语言:javascript
复制
<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

   <ImageView
       android:id="@+id/object"
       android:layout_width="wrap_content"
       android:layout_height="wrap_content"
       android:layout_marginStart="8dp"
       android:layout_marginTop="8dp"
       android:layout_marginEnd="8dp"
       android:layout_marginBottom="8dp"
       android:src="@drawable/avd_anim"
       app:layout_constraintBottom_toBottomOf="parent"
       app:layout_constraintEnd_toEndOf="parent"
       app:layout_constraintStart_toStartOf="parent"
       app:layout_constraintTop_toTopOf="parent" />

</android.support.constraint.ConstraintLayout>

最后我的MainActivity.class

代码语言:javascript
复制
public class MainActivity extends AppCompatActivity {
private ImageView image;
private AnimatedVectorDrawable animation;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        image = (ImageView) findViewById(R.id.object);
    }

    @Override
    protected void onStart() {
        super.onStart();
        Drawable d = image.getDrawable();
        if (d instanceof AnimatedVectorDrawable) {
            Log.d("testanim", "onCreate: instancefound" + d.toString());
            animation = (AnimatedVectorDrawable) d;
            animation.start();
        }
    }

}
票数 18
EN

Stack Overflow用户

发布于 2018-12-14 15:09:06

您应该使用redot.getDrawable()而不是getBackground(),如果您使用app:srcCompat="@drawable/..."而不是android:src=@drawable/... for ImageView,则应该添加

代码语言:javascript
复制
if (d instanceof AnimatedVectorDrawableCompat) {                        
    AnimatedVectorDrawableCompat avd = (AnimatedVectorDrawableCompat) d;       
    avd.start();                                                               
}       

android:animation="@anim/redanim"应该是android:animation="@animator/redanim"。使用动画文件夹

票数 1
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/53779756

复制
相关文章

相似问题

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