2011-05-17 43 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(); 
    }