Android有两种动画,一种是tweened animation(渐变动画) ,主要用于控件的,大小缩放,透明度等,另一种是frame by frame(逐帧动画),也就是我今天要讨论的动画。逐帧动画顾名思义就是一帧一帧的播放动画,就像动画片的播放原理一样,它是通过不挺得替换图片,当替换图片的速度大于人眼的反应时间时就会给人一种画面在动的感觉。

废话不多说直接上代码:

1:在res文件夹下面建立一个文件夹anim;

2:在anim文件夹下建立一个文件firefox_animation.xml(名字自己任意定义)如下:
<?xml version="1.0" encoding="utf-8"?>  <animation-list           xmlns:android="http://schemas.android.com/apk/res/android"         android:oneshot="false">       <item android:duration="300" android:drawable="@drawable/home_hotapp" />        <item android:duration="300" android:drawable="@drawable/home_hotapp_click" /></animation-list>

说明:你想变多少张图片就要有几个item,在每一个item中 duration是定义这张图片存在的时间以毫秒为单位,drawable是图片的路径
3:定义布局文件
<?xml version="1.0" encoding="utf-8"?><LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"      android:layout_width="wrap_content" android:layout_height="wrap_content"      android:orientation="vertical">      <ImageView android:id="@+id/imageview1" android:layout_width="100px"           android:layout_height="100px" android:src="@anim/firefox_animation">      </ImageView>      <Button android:id="@+id/start" android:layout_width="fill_parent"           android:layout_height="wrap_content" android:text="Start">      </Button>      <Button android:id="@+id/start_once" android:layout_width="fill_parent"           android:layout_height="wrap_content" android:text="Start_once">      </Button></LinearLayout>

4:Activy中的代码:
public class MainA extends Activity {    private AnimationDrawable draw = null;    private Button start = null;    private Button startOnce = null;    private boolean isoneshot = true;    public void onCreate(Bundle bundle)    {     super.onCreate(bundle);     this.setContentView(R.layout.main);     ImageView view = (ImageView)findViewById(R.id.imageview1);     draw = (AnimationDrawable)view.getDrawable();     start = (Button)findViewById(R.id.start);     start.setOnClickListener(new OnClickListener()     {      public void onClick(View v) {       // TODO Auto-generated method stub       startAnimation();      }     });     startOnce = (Button)findViewById(R.id.start_once);     startOnce.setOnClickListener(new OnClickListener()     {      public void onClick(View v) {       // TODO Auto-generated method stub       if(isoneshot)       {        startOnce.setText("startOnce");       }       else       {        startOnce.setText("Play Repace");       }       draw.setOneShot(isoneshot);       isoneshot = !isoneshot;      }     });    }    private void startAnimation()    {     if(draw.isRunning())     {      draw.stop();     }     else     {      draw.stop();      draw.start();     }    }   }

5:完成点击start按钮动画开始。
问题来了,如果我想不需要点击按钮,一进入Activity就让动画动起来怎么办?
聪明的同学肯定会说直接把startAnimation()方法放到onCreate()方法中不就可以了吗,甚至还会有同学说在onCreate()方法中代码只会在创建的时候执行,应该是放到onResume()方法中,这样无论怎么样代码都会执行,动画效果都会出来,事实上呢?要相信科学少年,结果是什么,勤奋的同学自己44就知道了。
如果想要在一进入页面的时候就执行动画我们就要对代码做一个小小的修改,同时我们去掉了那两个按钮。
其他的部分不变代码如下:
publicclass Main2 extends Activity {    private AnimationDrawable draw = null;    publicvoid onCreate(Bundle bundle) {       super.onCreate(bundle);       this.setContentView(R.layout.main);       ImageView view = (ImageView) findViewById(R.id.imageview1);       view.setBackgroundResource(R.anim.firefox_animation);       draw = (AnimationDrawable) view.getBackground();       view.getViewTreeObserver().addOnPreDrawListener(opdl);    }    OnPreDrawListener opdl=new OnPreDrawListener(){        publicboolean onPreDraw() {             draw.start();                returntrue;        }};}

运行之后您会惊喜的发现,动画执行了。

小弟浅见:应该是在onCreate()方法中AnimationDrawable虽然初始化了,但是并没有完全,动画只会走一帧,永远停在第一张图片上,加入ViewTreeObserver观察者,ViewTreeObserver能够一直监听LayoutTree中的布局绘制情况,而OnPreDrawListener他在图片初始化完了之后会自动执行回调方法OnPreDrawListener,这样动画就动起来。

以上就是我使用逐帧动画时的一些小心得,有什么不对的地方还请各位大虾指点,谢谢。
转自:http://blog.csdn.net/gzhan1603/article/details/7576667

更多相关文章

  1. 没有一行代码,「2020 新冠肺炎记忆」这个项目却登上了 GitHub 中
  2. android 消息传递机制进阶EventBus的深入探究
  3. Android(安卓)仿知乎创意广告 广告还能这么玩?
  4. Android(安卓)Studio(7)---查找例子
  5. android 自定义菜单
  6. Android(安卓)属性动画使用(三)
  7. Android中绘制简单几何图形和路径Path
  8. 最受Java编码员和程序员欢迎的好助手:Android(安卓)IDE工具和应用
  9. android压缩图片保存为文件后显示方向被改的解决方法

随机推荐

  1. android 属性大全
  2. android深度搜索学习笔记四(硬件抽像hal第
  3. 从Android发展看Meego
  4. Android(安卓)设置DrawableRight和Drawab
  5. Android高手进阶教程(二)之 ----Android(
  6. Unity3d和Android之间互相调用
  7. Android常见的存储方式
  8. 基于Android智能终端的远程控制系统
  9. Android基础入门教程——7.2.1 Android(
  10. Android(安卓)系统启动过程