本文实例为大家分享了android实现加载动画对话框的具体代码,供大家参考,具体内容如下
先来两张效果图
自定义对话框:
public class LoadingProgressDialog extends ProgressDialog {
private AnimationDrawable mAnimation;
private Context mContext;
private ImageView mImageView;
private String mLoadingTitle;
private TextView mLoadingTv;
private int mResid;
public LoadingProgressDialog(Context context, String content, int id) {
super(context);
this.mContext = context;
this.mLoadingTitle = content;
this.mResid = id;
setCanceledOnTouchOutside(true);
}
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
initView();
initData();
}
private void initData() {
mImageView.setBackgroundResource(mResid);
mAnimation = (AnimationDrawable) mImageView.getBackground();
mImageView.post(new Runnable() {
@Override
public void run() {
mAnimation.start();
}
});
mLoadingTv.setText(mLoadingTitle);
}
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);
}
}
在layout文件夹下建立progress_dialog.xml
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:gravity="center"
android:layout_gravity="center"
android:orientation="vertical">
android:id="@+id/loadingIv"
android:layout_width="wrap_content"
android:layout_height="wrap_content" />
android:id="@+id/loadingTv"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#ffffff"
android:textSize="20sp" />
在res文件夹下建立anim文件夹,然后在里面建立frame.xml
动画是由一张一张的图片逐帧播放的,里面每一个item就是一张图片,动画有多少帧就有多少张图片,有多少图片就有多少item。
xmlns:android="http://schemas.android.com/apk/res/android"
android:oneshot="false">
android:drawable="@drawable/loading_01"
android:duration="100" />
android:drawable="@drawable/loading_02"
android:duration="100" />
android:drawable="@drawable/loading_03"
android:duration="100" />
android:drawable="@drawable/loading_04"
android:duration="100" />
用法:
LoadingProgressDialog dialog =new LoadingProgressDialog(MainActivity.this, "正在加载中...",R.anim.frame);
//打开
dialog.show();
//隐藏
dialog.dismiss();
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持脚本之家。