Android_AnimationDrawable介绍及使用

本文介绍如何使用Drawable资源实现帧动画,并提供了具体的XML配置方法和代码示例。解释了如何利用AnimationDrawable进行动画展示,并强调了动画启动时机的重要性。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

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

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

  1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"
  2. android:oneshot="true">
  3. <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />
  4. <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />
  5. <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />
  6. </animation-list>
<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().

下面是一个例子:

  1. AnimationDrawable rocketAnimation;
  2. public void onCreate(Bundle savedInstanceState) {
  3. super.onCreate(savedInstanceState);
  4. setContentView(R.layout.main);
  5. ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);
  6. rocketImage.setBackgroundResource(R.drawable.rocket_thrust); //roket_trust为定义的XML文件
  7. rocketAnimation = (AnimationDrawable) rocketImage.getBackground();
  8. }
  9. public boolean onTouchEvent(MotionEvent event) {
  10. if (event.getAction() == MotionEvent.ACTION_DOWN) {
  11. rocketAnimation.start();
  12. return true;
  13. }
  14. return super.onTouchEvent(event);
  15. }
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文件如下:

  1. <TextView
  2. android:id="@+id/textView"
  3. android:layout_width="50dip"
  4. android:layout_height="100dip"
  5. android:text="@string/special_character" />
<TextView 

        android:id="@+id/textView" 

        android:layout_width="50dip" 

        android:layout_height="100dip" 

        android:text="@string/special_character" /> 

代码如下:

  1. @Override
  2. public void onWindowFocusChanged(boolean hasFocus) {
  3. // TODO Auto-generated method stub
  4. super.onWindowFocusChanged(hasFocus);
  5. specialCharacterStr = (String) mTextView.getText();
  6. Log.d("special_character", "specialCharacterStr is :" + specialCharacterStr);
  7. int width = mTextView.getMeasuredWidth();
  8. int height = mTextView.getMeasuredHeight();
  9. Log.d("window_focus", "textview width is:" + width);
  10. Log.d("window_focus", "textview height is:" + height);
  11. }
  12. 可以获得宽和高,即只有当View完全关联到Window的情况下,才可以获得View的宽高和给View设置背景
  13. AnimationDrawable: android.graphic.drawable.AnimationDrawable
  14. //获得我们xml定义的AnimationDrawable
  15. animDrawable=(AnimationDrawable) getResources().getDrawable(R.anim.frame_animation);
  16. 一段参考代码:
  17. @Override
  18. publicvoid onWindowFocusChanged(boolean hasFocus) {
  19. // TODO Auto-generated method stub
  20. if(hasFocus) {
  21. imageView.setBackgroundResource(R.anim.frame_animation);
  22. animDrawable = (AnimationDrawable) imageView.getBackground();
  23. animDrawable.start();
  24. AlphaAnimation aas=new AlphaAnimation(0.1f,1.0f);
  25. //设置动画时间长度
  26. aas.setDuration(3500);
  27. //启动动画
  28. imageView.startAnimation(aas);
  29. //设置动画监听
  30. aas.setAnimationListener(new AnimationListener()
  31. {
  32. @Override
  33. publicvoid onAnimationEnd(Animation arg0) {
  34. //停止帧动画
  35. imageView.setVisibility(View.GONE);
  36. Log.i(TAG,"FY_stop");
  37. animDrawable.stop();
  38. }
  39. @Override
  40. publicvoid onAnimationRepeat(Animation animation) {
  41. }
  42. @Override
  43. publicvoid onAnimationStart(Animation animation) {
  44. //将imageView的背景设置成动画
  45. imageView.setBackgroundResource(R.anim.frame_animation);
  46. animDrawable = (AnimationDrawable)imageView.getBackground();
  47. //设置动画透明度
  48. animDrawable.setAlpha(80);
  49. //启动动画
  50. animDrawable.start();
  51. }
  52. }
  53. );
  54. }
  55. }
@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();

              }  

              @Override

              publicvoid onAnimationRepeat(Animation animation) {

              }

              @Override

              publicvoid onAnimationStart(Animation animation) {

                  //将imageView的背景设置成动画

                  imageView.setBackgroundResource(R.anim.frame_animation);

                  animDrawable = (AnimationDrawable)imageView.getBackground();

                  //设置动画透明度

                  animDrawable.setAlpha(80);

                  //启动动画

                  animDrawable.start();

              }            

           }

           );

       }  
    }	


推荐一个应用程序,大家感觉文章对你有用麻烦帮着下载顶上去:http://www.talkphone.cn/Down/Soft/Detail/49172_0.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值