简体   繁体   English

如何在 kotlin 中初始化动画矢量

[英]how to init animated vector in kotlin

I followed this tutorial and created my first animated vector in android studio.我按照本教程在 android 工作室创建了我的第一个动画矢量。

my vector is:我的向量是:

<animated-vector xmlns:android="http://schemas.android.com/apk/res/android"
  xmlns:aapt="http://schemas.android.com/aapt"
  android:width="491.4dp"
  android:height="297.83dp"
  android:viewportWidth="491.4"
  android:viewportHeight="297.83"
  android:drawable="@drawable/ic_logo">
  <target android:name="name1">
    <aapt:attr name="android:animation">
      <objectAnimator
        android:duration="2000"
        android:repeatCount="-1"
        android:repeatMode="reverse">
        <propertyValuesHolder android:propertyName="alpha" >
          <keyframe
            android:fraction="0"
            android:value="1f" />
          <keyframe
            android:fraction=".5"
            android:value="0f" />
          <keyframe
            android:fraction="1"
            android:value="1f" />
        </propertyValuesHolder>
      </objectAnimator>
    </aapt:attr>
  </target>
  <target android:name="name2">
    <aapt:attr name="android:animation">
      <objectAnimator
        android:duration="2000"
        android:repeatCount="-1"
        android:repeatMode="reverse">
        <propertyValuesHolder android:propertyName="alpha" >
          <keyframe
            android:fraction="0"
            android:value="1f" />
          <keyframe
            android:fraction=".5"
            android:value="0f" />
          <keyframe
            android:fraction="1"
            android:value="1f" />
        </propertyValuesHolder>
      </objectAnimator>
    </aapt:attr>
  </target>
</animated-vector>

It seems I need to init it in the activity to set animation to work.看来我需要在活动中初始化它以设置 animation 工作。 But I did not find any tutorial about how to init it in Kotlin.但是我在 Kotlin 中没有找到任何关于如何初始化它的教程。 Could anyone help me please?有人可以帮我吗?

It depends how you apply this Drawable.这取决于你如何应用这个 Drawable。 If it's set on an ImageView, you can use如果它设置在 ImageView 上,您可以使用

(imageView.drawable as? Animatable)?.start()

If it's the background of a view you can use如果它是视图的背景,您可以使用

(view.background as? Animatable)?.start()

Assuming there is an imageview with this vector drawable on it假设有一个 imageview 上面有这个矢量可绘制

ImageView imageView = view.findViewById(R.id.animatedImage);

AnimatedVectorDrawable drawable = (AnimatedVectorDrawable)imageView.getDrawable();

drawable.start(); //this starts the animation

drawable.setVisible(true,true); //this also starts the animation from start

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM