Android_AnimationDrawable介紹及使用

來源:互聯網
上載者:User

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(); 
 
              }   
 
              @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(); 
 
              }             
 
           } 
 
           ); 
 
       }   
    }    

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.