当前位置:操作系统 > 安卓/Android >>

Android_AnimationDrawable介绍及使用

Drawable animation可以加载Drawable资源实现帧动画。AnimationDrawable是实现Drawable animations的基本类。推荐用XML文件的方法实现Drawable动画,不推荐在代码中实现。这种XML文件存放在工程中res/drawable/目录下。XML文件的指令(即属性)为动画播放的顺序和时间间隔。

     在XML文件中<animation-list>元素为根节点,<item>节点定义了每一帧,表示一个drawable资源的帧和帧间隔。下面是一个XML文件的实例:

[java] 
<animation-list xmlns:android="http://schemas.android.com/apk/res/android" 
 
    android:oneshot="true"> 
 
    <item android:drawable="@drawable/rocket_thrust1" android:duration="200" /> 
 
    <item android:drawable="@drawable/rocket_thrust2" android:duration="200" /> 
 
    <item android:drawable="@drawable/rocket_thrust3" android:duration="200" /> 
 
</animation-list> 
     设置Android:oneshot属性为true,表示此次动画只执行一次,最后停留在最后一帧。设置为false则动画循环播放。文件可以添加为Image背景,触发的时候播放。

使用:

    方式1:Drawable Animation本身就是一个Drawable资源文件,所以直接在xml中设置为指定View的背景即可。animation.start().

    方式2:通过View. setBackgroundResource(resID).    animation.start().

下面是一个例子:

[java] 
AnimationDrawable rocketAnimation; 
 
public void onCreate(Bundle savedInstanceState) { 
  super.onCreate(savedInstanceState); 
  setContentView(R.layout.main); 
 
  ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image); 
  rocketImage.setBackgroundResource(R.drawable.rocket_thrust); //roket_trust为定义的XML文件 
  rocketAnimation = (AnimationDrawable) rocketImage.getBackground(); 
 

 
public boolean onTouchEvent(MotionEvent event) { 
 
  if (event.getAction() == MotionEvent.ACTION_DOWN) { 
    rocketAnimation.start(); 
    return true; 
  } 
 
  return super.onTouchEvent(event); 
 

     注意:,一旦给指定View设置Drawable Animation之后,其BackGround就变成AnimationDrawable对象,代码如下: rocketAnimation = (AnimationDrawable) rocketImage.getBackground();

start()方法不能在onCreate()函数中调用。因为AnimationDrawable并未完全关联到Window,在onCreate()方法中,View并未完成显示(同理,在此方法中测量某个View的宽高,常得到0值。也同理SurfaceHolder要增加Callback方法)。在此如果想最快的启动动画,使用监听方法onWindowFoucsChanged().

More:突然想到,组件的宽高无法获得的原因可能是组件并未完全关联到Window测试:在此监听方法下,获取指定组件(TextView)的宽高。

Xml文件如下:

[html] 
<TextView  
 
        android:id="@+id/textView"  
 
        android:layout_width="50dip"  
 
        android:layout_height="100dip"  
 
        android:text="@string/special_character" />  
代码如下:      

[java]
@Override  
 
    public void onWindowFocusChanged(boolean hasFocus) {  
 
        // TODO Auto-generated method stub  
        super.onWindowFocusChanged(hasFocus);  
 
        specialCharacterStr = (String) mTextView.getText();  
        Log.d("special_character", "specialCharacterStr is :" + specialCharacterStr);        
 
        int width = mTextView.getMeasuredWidth();  
        int height = mTextView.getMeasuredHeight();  
 
        Log.d("window_focus", "textview width is:" + width);  
        Log.d("window_focus", "textview height is:" + height);  
    } 
 
 
 
 
可以获得宽和高,即只有当View完全关联到Window的情况下,才可以获得View的宽高和给View设置背景 
 
AnimationDrawable: android.graphic.drawable.AnimationDrawable 
 
//获得我们xml定义的AnimationDrawable 
 
              animDrawable=(AnimationDrawable) getResources().getDrawable(R.anim.frame_animation); 
 
一段参考代码: 
 
@Override 
 
    publicvoid onWindowFocusChanged(boolean hasFocus) { 
 
       // TODO Auto-generated method stub 
 
       if(hasFocus) { 
 
           imageView.setBackgroundResource(R.anim.frame_animation); 
 
           animDrawable = (AnimationDrawable) imageView.getBackground(); 
 
           animDrawable.start(); 
 
           AlphaAnimation aas=new AlphaAnimation(0.1f,1.0f); 
 
           //设置动画时间长度 
 
           aas.setDuration(3500); 
 
           //启动动画 
 
           imageView.startAnimation(aas); 
 
           //设置动画监听 
 
           aas.setAnimationListener(new AnimationListener() 
 
           { 
 
              @Override 
 
              publicvoid onAnimationEnd(Animation arg0) { 
 
                  //停止帧动画 
 
                  imageView.setVisibility(View.GONE); 
 
                  Log.i(TAG,"FY_stop"); 
 
                  animDrawable.stop(); 
 
        &

补充:移动开发 , Android ,
CopyRight © 2012 站长网 编程知识问答 www.zzzyk.com All Rights Reserved
部份技术文章来自网络,