Android自訂類似ProgressDialog效果的Dialog

來源:互聯網
上載者:User

標籤:android   style   blog   http   java   color   

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.

 

<!-- 自訂loading dialog -->      <style name="loading_dialog" parent="android:style/Theme.Dialog">          <item name="android:windowFrame">@null</item>          <item name="android:windowNoTitle">true</item>           <item name="android:windowBackground">@drawable/loading_bg</item>          <item name="android:windowIsFloating">true</item>          <item name="android:windowContentOverlay">@null</item>      </style>  

 

[java] view plaincopy 

 

 

5.寫點建立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.loading_dialog, null);// 得到載入view          LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 載入布局          // main.xml中的ImageView          ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);          TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字          // 載入動畫          Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation(                  context, R.anim.load_animation);          // 使用ImageView顯示動畫          spaceshipImage.startAnimation(hyperspaceJumpAnimation);          tipTextView.setText(msg);// 設定載入資訊            Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 建立自訂樣式dialog            loadingDialog.setCancelable(false);// 不可以用“返回鍵”取消          loadingDialog.setContentView(layout, new LinearLayout.LayoutParams(                  LinearLayout.LayoutParams.FILL_PARENT,                  LinearLayout.LayoutParams.FILL_PARENT));// 設定布局          return loadingDialog;        }  

 

 


最後來張整體的:

 

 -------------------------------------------------------------------------------------------------------------------------------------

上一篇寫了使用系統ProgressBar實現的稍微好看的ProgressDialog,如果你想自己的ProgressDialog更具有自己的風格,那用圖片去實現,將會達到你的目的。話不多說,先看效果:看了上篇以後,實現這個也是很簡單的,只需要把布局檔案的ProgressBar換成ImageView就可以了。

換成ImageView以後,我們需要讓這個ImageView動起來,這就需要對它進行一個anim處理。

實現思路是:用一個動畫代替ImageView的圖片資源,然後讓動畫動起來就OK了。

將這個資源當作ImageView的背景圖片

 <?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/icon_loading1" android:duration="100"/>      <item android:drawable="@drawable/icon_loading2" android:duration="100"/>      <item android:drawable="@drawable/icon_loading3" android:duration="100"/>      <item android:drawable="@drawable/icon_loading4" android:duration="100"/>      <item android:drawable="@drawable/icon_loading5" android:duration="100"/>      <item android:drawable="@drawable/icon_loading6" android:duration="100"/>      <item android:drawable="@drawable/icon_loading7" android:duration="100"/>      <item android:drawable="@drawable/icon_loading8" android:duration="100"/>  </animation-list>  

 

然後就要在MyProgressDialog類裡面讓動畫啟動了

 
ImageView imageView = (ImageView) progressDialog.findViewById(R.id.loadImg);          AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();          animationDrawable.start();  

 


完成這些,然後就可以在Activity裡面調用我們的MyProgressDialog了。

 

為了方便,給大家附上我的圖片資源,希望能用得上

  • 上一篇Android自訂progressDialog使用系統ProgressBar與圖片實現(一)系統ProgressBar下一篇Android PopupWindow與ListView配合使用

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.