照例先看效果图
其实原理很简单,就是放两张图片来回切换
<?xml version="1.0" encoding="utf-8"?>
<animation-list xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false" >
<item
android:drawable="@drawable/progress_loading_image_01"
android:duration="150"/>
<item
android:drawable="@drawable/progress_loading_image_02"
android:duration="150"/>
</animation-list>
再写一个自定义的Dialog类
public class CustomProgressDialog extends ProgressDialog {
private AnimationDrawable mAnimation;
private Context mContext;
private ImageView mImageView;
private String mLoadingTip;
private TextView mLoadingTv;
private int count = 0;
private String oldLoadingTip;
private int mResid;
public CustomProgressDialog(Context context, String content, int id) {
super(context);
this.mContext = context;
this.mLoadingTip = content;
this.mResid = id;
setCanceledOnTouchOutside(true);//设置点击可取消
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
}
private void initData() {
mImageView.setBackgroundResource(mResid);
// 通过ImageView对象拿到背景显示的AnimationDrawable
mAnimation = (AnimationDrawable) mImageView.getBackground();
// 为了防止在onCreate方法中只显示第一帧的解决方案之一
mImageView.post(new Runnable() {
@Override
public void run() {
mAnimation.start();
}
});
mLoadingTv.setText(mLoadingTip);
}
public void setContent(String str) {
mLoadingTv.setText(str);
}
private void initView() {
setContentView(R.layout.progress_dialog);
mLoadingTv = (TextView) findViewById(R.id.loadingTv);
mImageView = (ImageView) findViewById(R.id.loadingIv);
}
/*@Override
public void onWindowFocusChanged(boolean hasFocus) {
// TODO Auto-generated method stub
mAnimation.start();
super.onWindowFocusChanged(hasFocus);
}*/
}