Android_AnimationDrawable介绍及使用

时间:2023-03-10 07:28:21
Android_AnimationDrawable介绍及使用

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>

设置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. }

注意:,一旦给指定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" />

代码如下:

  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. }

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