Reprint Please specify source:http://blog.csdn.net/allen315410/article/details/41699063
When we develop the application on Android, there are many times when we encounter "delay" waiting, such as when the data is loaded, especially when the network is connected, the request net will have a wait time, in this waiting time need to give the user a friendly prompt, prompting the user is now doing what operation, You need to wait patiently and so on, when a progress dialog box can be resolved. Android gives us a good control called ProgressDialog, used to create custom information and some related operations, the only downside is that Android native controls give me a kind of monotonous and ugly as always, Here is the source and effect of native ProgressDialog:
public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (R.layout.activity_main); ProgressDialog dialog = new ProgressDialog (this);d ialog.setmessage ("Login ...");d ialog.show ();}}
The source code is extremely simple, the effect is extremely humble, how to see how uncomfortable. So how do you make this progressdialog look cool? In fact, you might as well make a custom progressdialog, first look at the effect of custom ProgressDialog!
Well, it looks good! Let's start with a step-by-step implementation!
1, customize the layout of the ProgressDialog.
<?xml version= "1.0" encoding= "Utf-8"? ><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= "Verti Cal "Android:paddingbottom=" 20DP "android:paddingleft=" 30DP "android:paddingright=" 30DP "android:paddingtop=" 2 0DP "> <imageview android:id=" @+id/spinnerimageview "android:layout_width=" Wrap_content "and roid: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_marg intop= "15DP" android:text= "Message" android:textcolor= "#FFFFFF"/></linearlayout>
2, the background setting of the scroll bar.
The above XML layout can see the scroll bar is a imageview, need to set a dynamic background for imageview, that dynamic background what to do? In fact, to give ImageView an animated background, give a certain number of pictures, in the animation in a certain time at a constant speed switch picture can be, picture resources as follows:
Create such an animation set spinner.xml in the Res/anim folder:
<animation-list xmlns:android= "Http://schemas.android.com/apk/res/android" android:oneshot= "false" > < Item android:drawable= "@drawable/spinner_0" android:duration= "/> <item android:drawable=" @drawable/spinner_1 "android:duration="/> <item android:drawable= "@drawable/spinner_2" an droid:duration= "/> <item android:drawable=" @drawable/spinner_3 "android:duration="/> &L T;item android:drawable= "@drawable/spinner_4" android:duration= "/> <item android:drawable = "@drawable/spinner_5" android:duration= "/> <item android:drawable=" @drawable/spinner_6 " android:duration= "/> <item android:drawable=" @drawable/spinner_7 "android:duration="/> " <item android:drawable= "@drawable/spinner_8" android:duration= "/> <item android:drawab le= "@drawable/spinner_9" android:duration= "/> <item android:drawable=" @drawable/spinner_10 "android:duration="/> " <item android:drawable= "@drawable/spinner_11" android:duration= "/></animation-list>"
Then set a background color for the entire ProgressDialog progress_custom_bg.xml:
<?xml version= "1.0" encoding= "UTF-8"? ><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>
You will then need to set a style for your custom progressdialog, which is defined under Res/value/style.xml:
<!--custom 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, the next step is to focus, we build a custom ProgressDialog by code, the custom class Inherits 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);} /** * When window focus is changed call */public void Onwindowfocuschanged (Boolean hasfocus) {ImageView ImageView = (ImageView) Findviewbyid (r.i D.spinnerimageview);//Get animated background on ImageView animationdrawable spinner = (animationdrawable) imageview.getbackground (); /Start animation Spinner.start ();} /** * To Dialog set prompt message * * @param message */public void Setmessage (Charsequence message) {if (message! = null && mes Sage.length () > 0) {Findviewbyid (r.id.message). setvisibility (view.visible); TextView txt = (TextView) Findviewbyid (r.id.message); txt.settext (message); txt.iNvalidate ();}} /** * Bounce Definition ProgressDialog * * @param context * Context * @param message * hint * @param cancelable * Press the back key to cancel * @param Cancellistener * Press the back key to listen * @return */public static customprogress Show (context context , Charsequence message, Boolean cancelable, Oncancellistener cancellistener) {customprogress dialog = new Customprogress ( Context, r.style.custom_progress);d ialog.settitle ("");d Ialog.setcontentview (R.layout.progress_custom); 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);} Press the return key to cancel dialog.setcancelable (cancelable);//monitor Return key processing Dialog.setoncancellistener (Cancellistener);// Set Center Dialog.getwindow (). GetAttributes (). Gravity = Gravity.center; Windowmanager.layoutparams LP = Dialog.getwindow (). GetAttributes ();//Set background layer Transparency Lp.dimamount = 0.2f;dialog.getwindow () . SetAttributes (LP);//Dialog.getwindow (). adDflags (WindowManager.LayoutParams.FLAG_BLUR_BEHIND);d ialog.show (); return dialog;}} Refer to Custom ProgressDialog in activity:
public class Mainactivity extends Activity {@Overrideprotected void onCreate (Bundle savedinstancestate) {super.oncreate (savedinstancestate); Setcontentview (r.layout.activity_main); Customprogress.show (This, "Login ...", true, NULL);}}
OK, the whole process is over, our simple custom progress dialog is ready, and the effect is already on it, no longer posted here. I don't explain the code inside, it's simple and there are some comments. Like friends, please click below to download the source code.
Please download the source code here
Android Custom ProgressDialog