1.简介
在res/drawable下可以定义一个帧动画资源,然后在代码中使用它。
官方文档:
https://developer.android.com/reference/android/graphics/drawable/AnimationDrawable
https://developer.android.com/guide/topics/graphics/drawable-animation
2.定义动画资源
animation_frame.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <animation-list xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/selected" android:oneshot="true" > 4 <!-- 5 android:drawable Reference to a drawable resource to use for the frame. 6 android:duration Amount of time (in milliseconds) to display this frame. 7 android:oneshot If true, the animation will only run a single time and then stop. 8 android:variablePadding If true, allows the drawable's padding to change based on the current state that is selected. 9 android:visible Provides initial visibility state of the drawable; the default value is false. 10 --> 11 <item android:drawable="@drawable/frame1" android:duration="500" /> 12 <item android:drawable="@drawable/frame2" android:duration="500" /> 13 <item android:drawable="@drawable/frame3" android:duration="500" /> 14 <item android:drawable="@drawable/frame4" android:duration="500" /> 15 <item android:drawable="@drawable/frame5" android:duration="500" /> 16 </animation-list>
3.在资源中使用它
1 <ImageView 2 android:id="@+id/frameImage" 3 android:layout_width="64dp" 4 android:layout_height="64dp" 5 android:background="@drawable/animation_frame" 6 app:layout_constraintBottom_toBottomOf="parent" 7 app:layout_constraintEnd_toStartOf="@+id/frameBtn" 8 app:layout_constraintStart_toStartOf="parent" 9 app:layout_constraintTop_toBottomOf="@+id/animation_frame_line" />
4.在代码中使用它
1 fun onFrameAnimationClicked(v : View){ 2 val frameAnimation = binding.frameImage.background as AnimationDrawable 3 frameAnimation.start() 4 }
5.矢量图动画
https://developer.android.com/reference/android/graphics/drawable/AnimatedVectorDrawable
https://developer.android.com/guide/topics/graphics/drawable-animation#AnimVector