1. 建立一个my_progress_dialog.xml布局文件
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="200dip" android:layout_height="200dip" >
<ImageView
android:id="@+id/iv_progressDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:src="@drawable/my_progress_dialog_rotate" />
<TextView
android:id="@+id/tv_progressDialog"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/iv_progressDialog"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dip"
android:gravity="center"
android:text="load..." />
</RelativeLayout>
2.在res目录下建立文件anim/my_progress_dialog_rotate_anim.xml,自定义动画
<?xml version="1.0" encoding="utf-8"?> <set xmlns:android="http://schemas.android.com/apk/res/android" android:shareInterpolator="false" >
<!-- 定义一个MyProgressDialog中imageView转动的动画 -->
<rotate android:duration="1500"
android:fromDegrees="0"
android:interpolator="@android:anim/linear_interpolator"
android:pivotX="50%"
android:pivotY="50%"
android:repeatCount="-1"
android:repeatMode="restart"
android:startOffset="-1"
android:toDegrees="+360" />
</set>
3.在values的styles.xml中添加Dialog样式
<!-- 自定义loading dialog -->
<style name="myProgressDialog" parent="android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@android:color/white</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
4.自定义一个方法实现Dialog
/** * 得到自定义的progressDialog
* * @param context
* @param msg
* @return
*/
public static Dialog createLoadingDialog(Context context, String msg) {
LayoutInflater inflater = LayoutInflater.from(context);
View v = inflater.inflate(R.layout.my_progress_dialog, null);// 得到加载view
ImageView spaceshipImage = (ImageView) v.findViewById(R.id.iv_progressDialog);
TextView tipTextView = (TextView) v.findViewById(R.id.tv_progressDialog);// 提示文字
// 加载动画
Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.my_progress_dialog_rotate_anim); // 使用ImageView显示动画 spaceshipImage.startAnimation(hyperspaceJumpAnimation);
tipTextView.setText(msg);// 设置加载信息
Dialog loadingDialog = new Dialog(context, R.style.myProgressDialog);// 创建自定义样式dialog
loadingDialog.setCancelable(false);
loadingDialog .setContentView(v, new LinearLayout.LayoutParams(150, 150));
return loadingDialog;
}