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文件的实例:

    <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().

    下面是一个例子:

    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文件如下:

    <TextView 
    
            android:id="@+id/textView" 
    
            android:layout_width="50dip" 
    
            android:layout_height="100dip" 
    
            android:text="@string/special_character" /> 

    代码如下:       

    @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

         

  • 相关阅读:
    PAT (Advanced Level) 1114. Family Property (25)
    PAT (Advanced Level) 1113. Integer Set Partition (25)
    PAT (Advanced Level) 1112. Stucked Keyboard (20)
    PAT (Advanced Level) 1111. Online Map (30)
    PAT (Advanced Level) 1110. Complete Binary Tree (25)
    PAT (Advanced Level) 1109. Group Photo (25)
    PAT (Advanced Level) 1108. Finding Average (20)
    PAT (Advanced Level) 1107. Social Clusters (30)
    PAT (Advanced Level) 1106. Lowest Price in Supply Chain (25)
    PAT (Advanced Level) 1105. Spiral Matrix (25)
  • 原文地址:https://www.cnblogs.com/wuyida/p/6300621.html
Copyright © 2011-2022 走看看