Android Dialog窗口显示动画
Android实现动画的方法很多,很方便,但我们使用的时候,还是遇到问题,记录备忘。
比如帧动画
这个动画要显示在Dialog的窗口里,网上都说很简单
ImageView iv = (ImageView) findViewById(R.id.ImageView01);
AnimationDrawable ad = (AnimationDrawable)iv.getBackground();
ad.start();
看起来是很简单,可是如果我们这样做:
final Dialog customDialog = new Dialog(this);
customDialog.setTitle(R.string.attention);
customDialog.setContentView(R.layout.connectionview);
ImageView image =
(ImageView)customDialog.findViewById(R.id.anim);
image.setBackgroundResource(R.drawable.connection);
AnimationDrawable ad =
(AnimationDrawable)image.getBackground();
ad.start();
customDialog.show();
你会发现,动画不动了,成了静画;修改成如下:
final Dialog customDialog = new Dialog(this);
customDialog.setTitle(R.string.attention);
customDialog.setContentView(R.layout.connectionview);
ImageView image =
(ImageView)customDialog.findViewById(R.id.anim);
image.setBackgroundResource(R.drawable.connection);
AnimationDrawable ad =
(AnimationDrawable)image.getBackground();
customDialog.show();
ad.start();
还是静画!
查了很多资料,都没有说原因,只有个地方说道,这个start函数,不能在onCreate里调用。
为什么动画不动,至今也没搞明白。估计还是因为时机不对,是不是这样可以呢?
customDialog.show();
AnimationDrawable ad =
(AnimationDrawable)image.getBackground();
ad.start();
没有去试。
最后的解决方法是这样的:
final Dialog customDialog = new Dialog(this);
customDialog.setTitle(R.string.attention);
customDialog.setContentView(R.layout.connectionview);
ImageView image =
(ImageView)customDialog.findViewById(R.id.anim);
image.setBackgroundResource(R.drawable.connection);
customDialog.setOnShowListener(new
DialogInterface.OnShowListener(){
public void onShow(android.content.DialogInterface dialog){
ImageView image
=(ImageView)customDialog.findViewById(R.id.anim);
AnimationDrawable ad =
(AnimationDrawable)image.getBackground();
if(ad != null) {
ad.start();
}
}
});
customDialog.show();
这里利用的onShowListener,注意这个接口需要高版本的SDK。