`
Tony_Lee-S
  • 浏览: 79444 次
  • 性别: Icon_minigender_1
  • 来自: 广州
社区版块
存档分类
最新评论

Android逐帧动画(转)

阅读更多
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
分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics