Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/android/215.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Android 如何在kotlin中初始化动画向量_Android_Kotlin_Animatedvectordrawable - Fatal编程技术网

Android 如何在kotlin中初始化动画向量

Android 如何在kotlin中初始化动画向量,android,kotlin,animatedvectordrawable,Android,Kotlin,Animatedvectordrawable,我在android studio中创建了我的第一个动画向量 我的向量是: <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 studio中创建了我的第一个动画向量

我的向量是:

<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>


似乎我需要在活动中初始化它来设置动画。但是我没有找到任何关于如何在Kotlin中初始化它的教程。谁能帮帮我吗?

这取决于你们如何使用这种可拉伸材料。如果在ImageView上设置,则可以使用

(imageView.drawable as? Animatable)?.start()
(view.background as? Animatable)?.start()
如果它是您可以使用的视图的背景

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

假设有一个可绘制此向量的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

仅供参考,这不是教程。这是本课程的官方文档。谢谢!它浪费了我一整天的时间为它找到一个合适的解决方案!!!当然可以您也可以将其强制转换为AnimatedVectorDrawable,但Animatable更通用,因为所有不同类型的动画drawable都实现了它,因此如果切换到不同类型的动画drawable,代码就不必更改。