2011-05-17 45 views
0

我想在對話框中顯示自定義動畫而不是正在進行的常規動畫對話框。對話框onShowListener()接口

以下是我的佈局XML文件:

<LinearLayout 
xmlns:android="http://schemas.android.com/apk/res/android" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent"> 
<ImageView 
     android:id="@+id/blankImageView" 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:background="@anim/loading_anim"/> 
</LinearLayout> 

對話框的onShowListener,我叫,

 dialog.setOnShowListener(new DialogInterface.OnShowListener(){ 

     @Override 
     public void onShow(DialogInterface dialog) { 
      final ImageView imageView = (ImageView) dlg.findViewById(R.id.blankImageView); 
      final AnimationDrawable yourAnimation = (AnimationDrawable) imageView.getBackground(); 
      yourAnimation.start(); 
     } 

    }); 

正如你所看到的,我開始了動畫OnShowListener對話框後。如果我在其他方法(如onStart(),onCreate()方法的對話框中啓動動畫,動畫將無法工作,因爲AnimationDrawable未附加到窗口。

但onShowListener接口來自API級別8.我的問題是,API級別8之前有什麼辦法知道AnimationDrawable是否連接到窗口?

回答

1

我重寫了onWindowFocusChanged()函數,在那裏我開始了動畫。此功能在API級別1之後就存在。因此,它像所有版本的魅力一樣工作

@Override 
public void onWindowFocusChanged(boolean hasFocus){ 
     ImageView imageView = (ImageView) findViewById(R.id.blankImageView); 
     AnimationDrawable yourAnimation = (AnimationDrawable) imageView.getBackground(); 
     yourAnimation.start(); 
    }