####帧动画概念以及用法
帧动画非常容易理解,其实就是简单的由N张静态图片收集起来,然后我们通过控制依次显示 这些图片,因为人眼”视觉残留”的原因,会让我们造成动画的”错觉”,跟放电影的原理一样!
#####示例:
![image](http://upload-images.jianshu.io/upload_images/5439590-1648a14907fa833e.jpg?imageMogr2/auto-orient/strip)
#####代码实现:
首先编写我们的动画文件,非常简单,先在res下的drawable创建一个文件:
1 2 3 4 5 6 7 8 9 10
| <?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <!--android:oneshot是设置动画是否只是播放一次,true只播放一次,false循环播放!--> <item android:drawable="@drawable/img_music" android:duration="200"/> <!--drawable 引用图片或其他drawable资源 --> <!--duration 执行时间--> <item android:drawable="@drawable/ic_launcher" android:duration="200"/> </animation-list>
|
动画有了在view中设置src或者background引用动画的drawable文件:
1 2 3 4 5
| <ImageView android:id="@+id/img_anim" android:src="@drawable/zhen_anim" android:layout_width="wrap_content" android:layout_height="wrap_content" />
|
控制动画的开始以及暂停:
1 2 3 4 5
| ImageView img_anim = findViewById(R.id.img_anim); AnimationDrawable anim_drawable = (AnimationDrawable)img_anim.getDrawable(); // 这里如果是src引用的就用getDrawable()如果是background就用getBackground()强转为AnimationDrawable anim_drawable.start(); anim_drawable.stop();
|