Android动画之逐帧动画(FrameAnimation)详解
2017-01-17 14:44
357 查看
转载请注明出处:http://blog.csdn.net/xiaohao0724/article/details/54582965
Android中的动画,大概分为三种:
1、逐帧动画(FrameAnimation)
2、补间动画(TweenAnimation)
3、属性动画(PropertyAnimation)
今天我们就来学习逐帧动画,废话少说直接上效果图如下:
帧动画的实现方式有两种:
一、在res/drawable文件夹下新建animation-list的XML实现帧动画
1、首先在res/drawable文件夹下添加img00-img24共25张图片
2、新建frame_anim.xml
3、在activity_main中添加控件
4、在代码中获取并开启帧动画
二、通过代码实现帧动画
点击下载源码
Android中的动画,大概分为三种:
1、逐帧动画(FrameAnimation)
2、补间动画(TweenAnimation)
3、属性动画(PropertyAnimation)
今天我们就来学习逐帧动画,废话少说直接上效果图如下:
帧动画的实现方式有两种:
一、在res/drawable文件夹下新建animation-list的XML实现帧动画
1、首先在res/drawable文件夹下添加img00-img24共25张图片
2、新建frame_anim.xml
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="true" > <!-- animation-list 帧动画 --> <!-- android:oneshot的值为 false代表播放多次,true代表只播放一次 --> <!-- duration代表每张图片的播放时间 ,定义一个持续时间为50毫秒的动画帧 --> <item android:drawable="@drawable/img00" android:duration="50"/> <item android:drawable="@drawable/img01" android:duration="50"/> <item android:drawable="@drawable/img02" android:duration="50"/> <item android:drawable="@drawable/img03" android:duration="50"/> <item android:drawable="@drawable/img04" android:duration="50"/> <item android:drawable="@drawable/img05" android:duration="50"/> <item android:drawable="@drawable/img06" android:duration="50"/> <item android:drawable="@drawable/img07" android:duration="50"/> <item android:drawable="@drawable/img08" android:duration="50"/> <item android:drawable="@drawable/img09" android:duration="50"/> <item android:drawable="@drawable/img10" android:duration="50"/> <item android:drawable="@drawable/img11" android:duration="50"/> <item android:drawable="@drawable/img12" android:duration="50"/> <item android:drawable="@drawable/img13" android:duration="50"/> <item android:drawable="@drawable/img14" android:duration="50"/> <item android:drawable="@drawable/img15" android:duration="50"/> <item android:drawable="@drawable/img16" android:duration="50"/> <item android:drawable="@drawable/img17" android:duration="50"/> <item android:drawable="@drawable/img18" android:duration="50"/> <item android:drawable="@drawable/img19" android:duration="50"/> <item android:drawable="@drawable/img20" android:duration="50"/> <item android:drawable="@drawable/img21" android:duration="50"/> <item android:drawable="@drawable/img22" android:duration="50"/> <item android:drawable="@drawable/img23" android:duration="50"/> <item android:drawable="@drawable/img24" android:duration="50"/> </animation-list>
3、在activity_main中添加控件
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" xmlns:tools="http://schemas.android.com/tools" android:layout_width="match_parent" android:layout_height="match_parent" tools:context="com.havorld.frameanimation.MainActivity" > <ImageView android:id="@+id/imageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerInParent="true" /> <!-- android:background="@drawable/frame_anim" --> <LinearLayout android:layout_width="match_parent" android:layout_height="wrap_content" android:layout_alignParentBottom="true" android:orientation="horizontal" android:padding="10dp" > <Button android:id="@+id/start" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="播放" /> <Button android:id="@+id/stop" android:layout_width="0dp" android:layout_height="wrap_content" android:layout_weight="1" android:text="停止" /> </LinearLayout> </RelativeLayout>
4、在代码中获取并开启帧动画
public class MainActivity extends Activity implements OnClickListener { private ImageView imageView; private AnimationDrawable animationDrawable; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); imageView = (ImageView) findViewById(R.id.imageView); findViewById(R.id.start).setOnClickListener(this); findViewById(R.id.stop).setOnClickListener(this); setXml2FrameAnim1(); // setXml2FrameAnim2(); } /** * 通过XML添加帧动画方法一 */ private void setXml2FrameAnim1() { // 把动画资源设置为imageView的背景,也可直接在XML里面设置 imageView.setBackgroundResource(R.drawable.frame_anim); animationDrawable = (AnimationDrawable) imageView.getBackground(); } /** * 通过XML添加帧动画方法二 */ private void setXml2FrameAnim2() { // 通过逐帧动画的资源文件获得AnimationDrawable示例 animationDrawable = (AnimationDrawable) getResources().getDrawable( R.drawable.frame_anim); imageView.setBackground(animationDrawable); } @Override public void onClick(View v) { switch (v.getId()) { case R.id.start: if (animationDrawable != null && !animationDrawable.isRunning()) { animationDrawable.start(); } break; case R.id.stop: if (animationDrawable != null && animationDrawable.isRunning()) { animationDrawable.stop(); } break; default: break; } } }
二、通过代码实现帧动画
/** * 通过代码添加帧动画方法 */ private void setSrc2FrameAnim() { animationDrawable = new AnimationDrawable(); // 为AnimationDrawable添加动画帧 animationDrawable.addFrame( getResources().getDrawable(R.drawable.img00), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img01), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img02), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img03), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img04), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img05), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img06), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img07), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img08), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img09), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img10), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img11), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img12), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img13), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img14), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img15), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img16), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img17), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img18), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img19), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img20), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img21), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img22), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img23), 50); animationDrawable.addFrame( getResources().getDrawable(R.drawable.img24), 50); // 设置为循环播放 animationDrawable.setOneShot(false); imageView.setBackground(animationDrawable); }
点击下载源码
相关文章推荐
- Android动画之逐帧动画(FrameAnimation)详解
- Android中的动画详解系列【1】——逐帧动画
- Android--Animotion动画详解-逐帧动画,补间动画,属性动画。
- Android动画详解(一) 逐帧动画
- Android动画之逐帧动画(Frame Animation)实例详解
- Android中的动画详解系列【1】——逐帧动画
- Android动画之逐帧动画(Frame Animation)实例详解
- Android动画之逐帧动画(Frame Animation)实例详解
- Android动画之逐帧动画(Frame Animation)实例详解
- Android中的动画详解系列【1】——逐帧动画
- Android中的动画实现详解(2)--逐帧动画
- 【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- Android 动画框架详解,第 1 部分
- Android动画详解(xml ¥ Java)
- 【Android游戏开发二十二】(图文详解)游戏中灵活实现动画播放!简述J2me的游戏类库与Android游戏开发!
- 详解Android动画之Tween Animation
- 【转】Android 动画框架详解,第 2 部分
- Android 动画框架详解,第 1 部分
- Android 动画框架详解 (来自IBM developer)
- Android 动画框架详解,第 2 部分