Android中的动画有
- 帧动画
- 属性动画
- 补间动画
大体思路
1.需要定义存放每一帧的xml文件,放在drawable文件夹下
设置图片路径和duration,以及shot属性,false--->只播放一次,true----->播放多次
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <animation-list android:oneshot="false" 3 4 xmlns:android="http://schemas.android.com/apk/res/android"> 5 <item android:duration="50" android:drawable="@drawable/progressbar34" /> 6 <item android:duration="50" android:drawable="@drawable/progressbar33" /> 7 <item android:duration="50" android:drawable="@drawable/progressbar32" /> 8 <item android:duration="50" android:drawable="@drawable/progressbar31" /> 9 <item android:duration="50" android:drawable="@drawable/progressbar30" /> 10 <item android:duration="50" android:drawable="@drawable/progressbar29" /> 11 <item android:duration="50" android:drawable="@drawable/progressbar28" /> 12 <item android:duration="50" android:drawable="@drawable/progressbar27" /> 13 <item android:duration="50" android:drawable="@drawable/progressbar26" /> 14 <item android:duration="50" android:drawable="@drawable/progressbar25" /> 15 <item android:duration="50" android:drawable="@drawable/progressbar24" /> 16 <item android:duration="50" android:drawable="@drawable/progressbar23" /> 17 <item android:duration="50" android:drawable="@drawable/progressbar22" /> 18 <item android:duration="50" android:drawable="@drawable/progressbar21" /> 19 <item android:duration="50" android:drawable="@drawable/progressbar20" /> 20 <item android:duration="50" android:drawable="@drawable/progressbar19" /> 21 <item android:duration="50" android:drawable="@drawable/progressbar18" /> 22 <item android:duration="50" android:drawable="@drawable/progressbar17" /> 23 <item android:duration="50" android:drawable="@drawable/progressbar16" /> 24 <item android:duration="50" android:drawable="@drawable/progressbar15" /> 25 <item android:duration="50" android:drawable="@drawable/progressbar14" /> 26 <item android:duration="50" android:drawable="@drawable/progressbar13" /> 27 <item android:duration="50" android:drawable="@drawable/progressbar12" /> 28 <item android:duration="50" android:drawable="@drawable/progressbar11" /> 29 <item android:duration="50" android:drawable="@drawable/progressbar10" /> 30 <item android:duration="50" android:drawable="@drawable/progressbar9" /> 31 <item android:duration="50" android:drawable="@drawable/progressbar8" /> 32 <item android:duration="50" android:drawable="@drawable/progressbar7" /> 33 <item android:duration="50" android:drawable="@drawable/progressbar6" /> 34 <item android:duration="50" android:drawable="@drawable/progressbar5" /> 35 <item android:duration="50" android:drawable="@drawable/progressbar4" /> 36 <item android:duration="50" android:drawable="@drawable/progressbar3" /> 37 <item android:duration="50" android:drawable="@drawable/progressbar2" /> 38 <item android:duration="50" android:drawable="@drawable/progressbar1" /> 39 </animation-list>
2.指定存放该动画的容器,使用imageView来充当
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 <?xml version="1.0" encoding="utf-8"?> 2 <android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 xmlns:tools="http://schemas.android.com/tools" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" 6 tools:context="com.jing.www.loginlightdemo.MainActivity"> 7 <ImageView 8 android:id="@+id/animation_iv" 9 android:layout_width="wrap_content" 10 android:layout_height="wrap_content" 11 android:layout_gravity="center" 12 android:layout_margin="10dp" 13 android:src="@drawable/progress" /> 14 15 </android.support.constraint.ConstraintLayout>
3.使用代码来播放帧动画
注意:不能再onCreate()方法中进行动画的播放,动画依附于window,在onCreate()时期,window还没有初始化完成
会使动画没有运行而是停留在第一帧,那是因为AnimationDrawable播放动画是依附在window上面,而在Activity onCreate方法中调用时Window还未初始化完毕,所有才会停留在第一帧,要想实现播放必须在onWindowFocusChanged中添加如下代码:
![](https://images.cnblogs.com/OutliningIndicators/ContractedBlock.gif)
1 public class MainActivity extends AppCompatActivity { 2 3 private ImageView imageView; 4 5 @Override 6 protected void onCreate(Bundle savedInstanceState) { 7 super.onCreate(savedInstanceState); 8 setContentView(R.layout.activity_main); 9 imageView = (ImageView) findViewById(R.id.animation_iv); 10 11 } 12 13 @Override 14 public void onWindowFocusChanged(boolean hasFocus) { 15 super.onWindowFocusChanged(hasFocus); 16 imageView.setImageResource(R.drawable.progress); 17 AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getDrawable(); 18 animationDrawable.start(); 19 20 } 21 }