Android自定義類似ProgressDialog效果的Dialog

Android自定義類似ProgressDialog效果的Dialog.

方法如下:

1.首先準備兩張自己要定義成哪樣子的效果的圖片和背景圖片(也可以不要背景)。

如我要的效果:



2.定義loading_dialog.xml佈局文件(這裏你也可以按自己的佈局效果定義,關鍵是要有個imageView):

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"  
  3.     android:id="@+id/dialog_view"   
  4.     android:orientation="vertical"  
  5.     android:layout_width="fill_parent"   
  6.     android:layout_height="fill_parent"  
  7.     android:minHeight="60dp"  
  8.     android:minWidth="180dp"  
  9.     android:gravity="center"  
  10.     android:padding="10dp"  
  11.     android:background="@drawable/loading_bg">  
  12.     <ImageView   
  13.         android:id="@+id/img"  
  14.         android:layout_width="wrap_content"   
  15.         android:layout_height="wrap_content"   
  16.         android:src="@drawable/publicloading"  
  17.         />  
  18.     <TextView   
  19.         android:id="@+id/tipTextView"  
  20.         android:layout_width="wrap_content"   
  21.         android:layout_height="wrap_content"  
  22.         android:layout_marginLeft="10dp"  
  23.         android:text="數據加載中……" />  
  24. </LinearLayout>  

3.定義一個loadingDialog中imageView轉動的動畫:loading_animation.xml

[html] view plaincopy
  1. <?xml version="1.0" encoding="utf-8"?>  
  2. <set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">  
  3.     <rotate   
  4.         android:interpolator="@android:anim/linear_interpolator"  
  5.         android:pivotX="50%"  
  6.         android:pivotY="50%"  
  7.         android:fromDegrees="0"  
  8.         android:toDegrees="+360"  
  9.         android:duration="1500"  
  10.         android:startOffset="-1"  
  11.         android:repeatMode="restart"  
  12.         android:repeatCount="-1"/>  
  13. </set>  

4.定義dialog的style.

[java] view plaincopy
  1. <!-- 自定義loading dialog -->  
  2.     <style name="loading_dialog" parent="android:style/Theme.Dialog">  
  3.         <item name="android:windowFrame">@null</item>  
  4.         <item name="android:windowNoTitle">true</item>   
  5.         <item name="android:windowBackground">@drawable/loading_bg</item>  
  6.         <item name="android:windowIsFloating">true</item>  
  7.         <item name="android:windowContentOverlay">@null</item>  
  8.     </style>  


5.寫點創建Dialog的代碼,你可以自己封裝成一個方法。

[java] view plaincopy
  1. /** 
  2.      * 得到自定義的progressDialog 
  3.      * @param context 
  4.      * @param msg 
  5.      * @return 
  6.      */  
  7.     public static Dialog createLoadingDialog(Context context, String msg) {  
  8.   
  9.         LayoutInflater inflater = LayoutInflater.from(context);  
  10.         View v = inflater.inflate(R.layout.loading_dialog, null);// 得到加載view  
  11.         LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加載佈局  
  12.         // main.xml中的ImageView  
  13.         ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);  
  14.         TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字  
  15.         // 加載動畫  
  16.         Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(  
  17.                 context, R.anim.load_animation);  
  18.         // 使用ImageView顯示動畫  
  19.         spaceshipImage.startAnimation(hyperspaceJumpAnimation);  
  20.         tipTextView.setText(msg);// 設置加載信息  
  21.   
  22.         Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 創建自定義樣式dialog  
  23.   
  24.         loadingDialog.setCancelable(false);// 不可以用“返回鍵”取消  
  25.         loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(  
  26.                 LinearLayout.LayoutParams.FILL_PARENT,  
  27.                 LinearLayout.LayoutParams.FILL_PARENT));// 設置佈局  
  28.         return loadingDialog;  
  29.   
  30.     }  

最後來張整體的效果圖:

via: http://blog.csdn.net/qjlhlh/article/details/7979179 

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章