zoukankan      html  css  js  c++  java
  • Android_AnimationDrawable介绍及使用

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

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

    [java] view plaincopy
     
    1. <animation-list xmlns:android="http://schemas.android.com/apk/res/android"  
    2.   
    3.     android:oneshot="true">  
    4.   
    5.     <item android:drawable="@drawable/rocket_thrust1" android:duration="200" />  
    6.   
    7.     <item android:drawable="@drawable/rocket_thrust2" android:duration="200" />  
    8.   
    9.     <item android:drawable="@drawable/rocket_thrust3" android:duration="200" />  
    10.   
    11. </animation-list>  

         设置Android:oneshot属性为true,表示此次动画只执行一次,最后停留在最后一帧。设置为false则动画循环播放。文件可以添加为Image背景,触发的时候播放。

    使用:

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

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

    下面是一个例子:

    [java] view plaincopy
     
    1. AnimationDrawable rocketAnimation;  
    2.   
    3. public void onCreate(Bundle savedInstanceState) {  
    4.   super.onCreate(savedInstanceState);  
    5.   setContentView(R.layout.main);  
    6.   
    7.   ImageView rocketImage = (ImageView) findViewById(R.id.rocket_image);  
    8.   rocketImage.setBackgroundResource(R.drawable.rocket_thrust); //roket_trust为定义的XML文件  
    9.   rocketAnimation = (AnimationDrawable) rocketImage.getBackground();  
    10.   
    11. }  
    12.   
    13. public boolean onTouchEvent(MotionEvent event) {  
    14.   
    15.   if (event.getAction() == MotionEvent.ACTION_DOWN) {  
    16.     rocketAnimation.start();  
    17.     return true;  
    18.   }  
    19.   
    20.   return super.onTouchEvent(event);  
    21.   
    22. }  

         注意:,一旦给指定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] view plaincopy
     
    1. <TextView   
    2.   
    3.         android:id="@+id/textView"   
    4.   
    5.         android:layout_width="50dip"   
    6.   
    7.         android:layout_height="100dip"   
    8.   
    9.         android:text="@string/special_character" />   

    代码如下:       

    [java] view plaincopy
     
    1. @Override   
    2.   
    3.     public void onWindowFocusChanged(boolean hasFocus) {   
    4.   
    5.         // TODO Auto-generated method stub   
    6.         super.onWindowFocusChanged(hasFocus);   
    7.   
    8.         specialCharacterStr = (String) mTextView.getText();   
    9.         Log.d("special_character", "specialCharacterStr is :" + specialCharacterStr);         
    10.   
    11.         int width = mTextView.getMeasuredWidth();   
    12.         int height = mTextView.getMeasuredHeight();   
    13.   
    14.         Log.d("window_focus", "textview width is:" + width);   
    15.         Log.d("window_focus", "textview height is:" + height);   
    16.     }  
    17.   
    18.   
    19.   
    20.   
    21. 可以获得宽和高,即只有当View完全关联到Window的情况下,才可以获得View的宽高和给View设置背景  
    22.   
    23. AnimationDrawable: android.graphic.drawable.AnimationDrawable  
    24.   
    25. //获得我们xml定义的AnimationDrawable  
    26.   
    27.               animDrawable=(AnimationDrawable) getResources().getDrawable(R.anim.frame_animation);  
    28.   
    29. 一段参考代码:  
    30.   
    31. @Override  
    32.   
    33.     publicvoid onWindowFocusChanged(boolean hasFocus) {  
    34.   
    35.        // TODO Auto-generated method stub  
    36.   
    37.        if(hasFocus) {  
    38.   
    39.            imageView.setBackgroundResource(R.anim.frame_animation);  
    40.   
    41.            animDrawable = (AnimationDrawable) imageView.getBackground();  
    42.   
    43.            animDrawable.start();  
    44.   
    45.            AlphaAnimation aas=new AlphaAnimation(0.1f,1.0f);  
    46.   
    47.            //设置动画时间长度  
    48.   
    49.            aas.setDuration(3500);  
    50.   
    51.            //启动动画  
    52.   
    53.            imageView.startAnimation(aas);  
    54.   
    55.            //设置动画监听  
    56.   
    57.            aas.setAnimationListener(new AnimationListener()  
    58.   
    59.            {  
    60.   
    61.               @Override  
    62.   
    63.               publicvoid onAnimationEnd(Animation arg0) {  
    64.   
    65.                   //停止帧动画  
    66.   
    67.                   imageView.setVisibility(View.GONE);  
    68.   
    69.                   Log.i(TAG,"FY_stop");  
    70.   
    71.                   animDrawable.stop();  
    72.   
    73.               }    
    74.   
    75.               @Override  
    76.   
    77.               publicvoid onAnimationRepeat(Animation animation) {  
    78.   
    79.               }  
    80.   
    81.               @Override  
    82.   
    83.               publicvoid onAnimationStart(Animation animation) {  
    84.   
    85.                   //将imageView的背景设置成动画  
    86.   
    87.                   imageView.setBackgroundResource(R.anim.frame_animation);  
    88.   
    89.                   animDrawable = (AnimationDrawable)imageView.getBackground();  
    90.   
    91.                   //设置动画透明度  
    92.   
    93.                   animDrawable.setAlpha(80);  
    94.   
    95.                   //启动动画  
    96.   
    97.                   animDrawable.start();  
    98.   
    99.               }              
    100.   
    101.            }  
    102.   
    103.            );  
    104.   
    105.        }    
    106.     }     


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

  • 相关阅读:
    《数据结构第一章复习》
    《图的基本操作》
    《矩阵的一些基本操作》
    <矩阵的基本操作:矩阵相加,矩阵相乘,矩阵转置>
    《两个二维数组(矩阵)相乘》
    C#隐藏与显示系统任务栏和开始菜单栏按钮
    C#通过窗体属性缩小一定尺寸时,无法再缩小窗体尺寸问题
    C#一个窗体调用另一个窗体的方法
    C#异步线程
    C#中MessageBox.Show问题(让提示窗口不显示在任务栏中)
  • 原文地址:https://www.cnblogs.com/yaowen/p/5085793.html
Copyright © 2011-2022 走看看