多多色-多人伦交性欧美在线观看-多人伦精品一区二区三区视频-多色视频-免费黄色视屏网站-免费黄色在线

國內最全IT社區平臺 聯系我們 | 收藏本站
阿里云優惠2
您當前位置:首頁 > php開源 > 綜合技術 > Android自定義ProgressDialog

Android自定義ProgressDialog

來源:程序員人生   發布時間:2014-12-14 08:47:36 閱讀次數:5223次

       轉載請注明出處:http://blog.csdn.net/allen315410/article/details/41699063 

       我們在開發Android上利用程序時,有很多時候會遇到“延時”等待的情況,例如數據加載時,特別是在聯網的時候,要求網絡會有個等待時間,在這個等待的時間里需要給用戶1個友好的提示,提示用戶現在正在做甚么操作,需要耐心等待等等,這時候1個進度對話框就能夠解決。Android提供給我們1個很好的控件叫ProgressDialog,用來創建自定義信息和1些相干操作,唯1不好的1點就是Android原生控件給我1種1如既往的單調和丑陋,下面是原生ProgressDialog的源碼和效果:

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); ProgressDialog dialog = new ProgressDialog(this); dialog.setMessage("登錄中……"); dialog.show(); } }


       源碼極為簡單,效果也極為簡陋,怎樣看怎樣不爽。那末怎樣讓這個ProgressDialog看起來爽點呢?其實無妨做1個自定義的ProgressDialog,先看1下自定義ProgressDialog的效果吧!


好了,看上去不錯吧!下面開始1步1步來實現!

1,自定義ProgressDialog的布局。

<?xml version="1.0" encoding="utf⑻"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:background="@drawable/progress_custom_bg" android:gravity="center_horizontal" android:orientation="vertical" android:paddingBottom="20dp" android:paddingLeft="30dp" android:paddingRight="30dp" android:paddingTop="20dp" > <ImageView android:id="@+id/spinnerImageView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@anim/spinner" /> <TextView android:id="@+id/message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginTop="15dp" android:text="Message" android:textColor="#FFFFFF" /> </LinearLayout>

2,轉動條的背景設置。

上面的XML布局中可以看到轉動條是1個ImageView,需要給ImageView設置1個動態的背景,那這個動態的背景該怎樣辦呢?其實就是給ImageView1個動畫背景,給出1定數量的圖片,在動畫中按1定時間勻速切換圖片便可,圖片資源以下:


在res/anim文件夾建立這樣1個動畫集spinner.xml:

<animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <item android:drawable="@drawable/spinner_0" android:duration="60"/> <item android:drawable="@drawable/spinner_1" android:duration="60"/> <item android:drawable="@drawable/spinner_2" android:duration="60"/> <item android:drawable="@drawable/spinner_3" android:duration="60"/> <item android:drawable="@drawable/spinner_4" android:duration="60"/> <item android:drawable="@drawable/spinner_5" android:duration="60"/> <item android:drawable="@drawable/spinner_6" android:duration="60"/> <item android:drawable="@drawable/spinner_7" android:duration="60"/> <item android:drawable="@drawable/spinner_8" android:duration="60"/> <item android:drawable="@drawable/spinner_9" android:duration="60"/> <item android:drawable="@drawable/spinner_10" android:duration="60"/> <item android:drawable="@drawable/spinner_11" android:duration="60"/> </animation-list>
然后給全部的ProgressDialog設置1個背風景progress_custom_bg.xml:

<?xml version="1.0" encoding="UTF⑻"?> <shape xmlns:android="http://schemas.android.com/apk/res/android" > <solid android:color="#ff404040" /> <corners android:bottomLeftRadius="8dp" android:bottomRightRadius="8dp" android:topLeftRadius="8dp" android:topRightRadius="8dp" /> </shape>

然后還需要給自定義ProgressDialog設置1個風格,在res/value/style.xml下這樣定義:

<!-- 自定義Dialog --> <style name="Custom_Progress" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item> <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowNoTitle">true</item> </style>

3,接下來進入重點,我們通過代碼來構建1個自定義的ProgressDialog,具體做法就是自定義類繼承Dialog:

package com.example.myexample; import android.app.Dialog; import android.content.Context; import android.graphics.drawable.AnimationDrawable; import android.view.Gravity; import android.view.View; import android.view.WindowManager; import android.widget.ImageView; import android.widget.TextView; public class CustomProgress extends Dialog { public CustomProgress(Context context) { super(context); } public CustomProgress(Context context, int theme) { super(context, theme); } /** * 當窗口焦點改變時調用 */ public void onWindowFocusChanged(boolean hasFocus) { ImageView imageView = (ImageView) findViewById(R.id.spinnerImageView); // 獲得ImageView上的動畫背景 AnimationDrawable spinner = (AnimationDrawable) imageView.getBackground(); // 開始動畫 spinner.start(); } /** * 給Dialog設置提示信息 * * @param message */ public void setMessage(CharSequence message) { if (message != null && message.length() > 0) { findViewById(R.id.message).setVisibility(View.VISIBLE); TextView txt = (TextView) findViewById(R.id.message); txt.setText(message); txt.invalidate(); } } /** * 彈出自定義ProgressDialog * * @param context * 上下文 * @param message * 提示 * @param cancelable * 是不是按返回鍵取消 * @param cancelListener * 按下返回鍵監聽 * @return */ public static CustomProgress show(Context context, CharSequence message, boolean cancelable, OnCancelListener cancelListener) { CustomProgress dialog = new CustomProgress(context, R.style.Custom_Progress); dialog.setTitle(""); dialog.setContentView(R.layout.progress_custom); if (message == null || message.length() == 0) { dialog.findViewById(R.id.message).setVisibility(View.GONE); } else { TextView txt = (TextView) dialog.findViewById(R.id.message); txt.setText(message); } // 按返回鍵是不是取消 dialog.setCancelable(cancelable); // 監聽返回鍵處理 dialog.setOnCancelListener(cancelListener); // 設置居中 dialog.getWindow().getAttributes().gravity = Gravity.CENTER; WindowManager.LayoutParams lp = dialog.getWindow().getAttributes(); // 設置背景層透明度 lp.dimAmount = 0.2f; dialog.getWindow().setAttributes(lp); // dialog.getWindow().addFlags(WindowManager.LayoutParams.FLAG_BLUR_BEHIND); dialog.show(); return dialog; } }
在Activity中援用自定義ProgressDialog:

public class MainActivity extends Activity { @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); CustomProgress.show(this, "登錄中...", true, null); } }
好了,全部流程結束,我們簡單的自定義進度對話框就做好了,效果在上面已截圖了,這里不再貼出。里面的代碼我就不解釋了,都比較簡單而且有1些注釋。喜歡的朋友,請在下方點擊下載源碼。


源碼請在這里下載


生活不易,碼農辛苦
如果您覺得本網站對您的學習有所幫助,可以手機掃描二維碼進行捐贈
程序員人生
------分隔線----------------------------
分享到:
------分隔線----------------------------
關閉
程序員人生
主站蜘蛛池模板: 欧美福利在线播放 | 韩国女主播一区二区三区视频 | 91精品免费久久久久久久久 | 宇都宫紫苑在线 | 一二三四视频观看中文在线看 | 嫩草影院在线观看未满十八 | 一二三四视频社区5在线高清视频 | 精品一区二区久久 | 自拍偷拍亚洲图片 | 加勒比精品久久一区二区三区 | 亚洲视频在线不卡 | 91久久精一区二区三区大全 | 日本中文字幕视频在线看 | 欧美中文字幕 | 日韩精品亚洲精品485页 | 国产精品国产欧美综合一区 | 香蕉超级碰碰碰97视频在线观看 | 亚洲精品国产v片在线观看 亚洲精品国产啊女成拍色拍 | 国产精品久久久久久久久久久不卡 | 国产精品v | 欧美毛片免费观看 | 爱爱精品视频 | 国产小情侣激情小视频免费看 | 亚洲国产情侣一区二区三区 | 性生活视频网 | 久久亚洲欧美 | 亚洲 自拍 另类 制服在线 | 欧美专区在线视频 | 亚洲国产人成在线观看 | 欧美一区二区不卡视频 | 欧美人与物videos另 | 能在线观看的一区二区三区 | 亚洲视频一区二区三区 | 视频三区精品中文字幕 | 男女上下爽无遮挡午夜免费视频 | 久久精品国产99久久久 | 精品国产免费第一区二区三区日韩 | 久久国产精品久久久久久久久久 | 高清中文字幕免费观在线 | 国产成人精品日本亚洲18图 | 最近的中文字幕视频完整 |