In the previous article we introduced the basic use of dialog, "Android UI Design" dialog dialog box (a) Continue to introduce today, Nonsense said, today mainly achieve progressdialog and transparent dialog two kinds of effects, Finally, let's introduce a dialog animated open Source Library on GitHub that contains a variety of animation effects, as follows:
First, ProgressDialog basic use
1.ProgressDialog Key Code
Mprogressdialog =NewProgressDialog (mainactivity. This);//Round ProgressBar //Mprogressdialog.setprogress (Progressdialog.style_spinner); //Horizontal ProgressBarMprogressdialog.setprogressstyle (progressdialog.style_horizontal);//Set ProgressDialog titleMprogressdialog.settitle ("Stay up every day");//Set ProgressDialog tipsMprogressdialog.setmessage ("The heart that wants to die has had ...");//Set the icon for the ProgressDialog progress barMprogressdialog.seticon (R.drawable.dialog);//If set to False, the current value of the scrollbar automatically moves back and forth between the minimum and maximum values, creating an animated effect that tells others "I am working," but does not indicate to which stage the work progresses. //The main task is to do some tasks that do not determine the operating time as a hint. and "Explicit" (true) is the ability to set the current progress value based on your progress. Mprogressdialog.setindeterminate (true);//Whether you can press the fallback key to cancelMprogressdialog.setcancelable (true);//Set a BUTTON on the progressdialog to choose three values: Button_positive,button_negative,button_neutralMprogressdialog.setbutton (Progressdialog.button_positive,"OK",NewDialoginterface.onclicklistener () {@Override Public void OnClick(Dialoginterface Dialog,intwhich) {//Click on the action after confirmation} }); Mprogressdialog.setbutton (Progressdialog.button_negative,"Cancel",NewDialoginterface.onclicklistener () {@Override Public void OnClick(Dialoginterface Dialog,intwhich) {//Click on the action after canceling} }); Mprogressdialog.show ();
2. To demonstrate the effect, add an asynchronous task Asynctask simulation progress Update
Public class mytask extends asynctask<Void, Integer, Integer> {@OverrideprotectedInteger doinbackground (Void ... params) {//Simulation Progress Update for(inti =0; I <= -; i++) {Try{Thread.Sleep ( +); Publishprogress (i); }Catch(Interruptedexception e) {E.printstacktrace (); } }return 1; } @Overrideprotected voidOnprogressupdate (Integer ... values) {if(values[0] == -) {Mprogressdialog.dismiss (); }Else{mprogressdialog.setprogress (values[0]); }} @Overrideprotected voidOnPostExecute (Integer result) {if(Result = =1) {Toast.maketext (mainactivity. This,"Download done!" ", Toast.length_short). Show (); } } }
3. Start a task after the Mprogressdialog.show () method
// 启动AsyncTask异步任务更新Progress进度task = new MyTask();task.execute();
4. Operation effect
Second, transparent dialog
1. Prepare dialog background image
2. Add a custom transparency style to Values/styles.xml
<stylename="Mydialogstyle"> <Item name="Android:windowbackground"> @android: color/transparent</Item> <Item name="Android:windowframe"> @null </Item><!--border--<Item name="Android:windownotitle">true</Item><!--Untitled--<Item name="Android:windowisfloating">true</Item><!--whether it appears on the activity--<Item name="Android:windowistranslucent">true</Item><!--Translucent--<Item name="Android:windowcontentoverlay"> @null </Item> <Item name="Android:windowanimationstyle"> @android: style/animation.dialog</Item> <Item name="Android:backgrounddimenabled">true</Item><!--Blur--</style>
3. Create a new dialogactivity and modify the theme to dialog style
<activityandroid:name=".DialogActivity" android:label="@string/title_activity_dialog"<!-- 执行自己上面自定义的样式 -->android:theme="@style/MyDialogStyle" ></activity>
4. Add a layout file Activity_dialog.xml
<relativelayout xmlns: Android = "http://schemas.android.com/apk/res/android" xmlns:tools =" Http://schemas.android.com/tools " android:layout_width = "match_parent" android:layout_height =; <relativelayout android: Layout_width = "200DP" android:layout_height< /span>= "200DP" android:layout_centerinparent = android:background = "@drawable/back" ; <linearlayout android: Layout_width = "fill_parent" android:layout _height = "fill_parent" android:layout_alig Nparentleft = "true" android:layout_alignpa Renttop = "true" android:gravity =
"center"
android:orientation =" vertical "; <progressbar andro Id:id = "@+id/progressbar" style = "Android:attr/progressbarstylelarge" andro Id:layout_width = "wrap_content" androi D:layout_height = "wrap_content" androi d:layout_gravity = "center_horizontal" /> <TextViewandroid:layout_width="Wrap_content"android:layout_height= "Wrap_content" android:layout_margintop="10DP"android:text="Logging in ..." Android:textcolor="#fff"android:textsize="20sp" /> </linearlayout> </relativelayout> </relativelayout>
5. Pop-up dialog, at this time the dialog is actually an activity, in need to pop up where the use of startactivity (Intent) method can be ejected dialog.
new Intent(MainActivity.this,DialogActivity.class); startActivity(intent);
6. Operation Effect
Third, niftydialogeffects Open source project
GitHub Address: Https://github.com/sd6352051/NiftyDialogEffects
Add Nineoldandroids-2.4.0.jar package under 1.libs directory
2. Create a dialog instance using the method
NiftyDialogBuilder dialogBuilder=NiftyDialogBuilder.getInstance(this);dialogBuilder .withTitle("Modal Dialog") .withMessage("This is a modal Dialog.") .show();
3. Setting the Dialog parameter
Dialogbuilder. Withtitle("Modal Dialog")//Set Caption. Withtitlecolor("#FFFFFF")//Title Color. Withdividercolor("#11000000")//Split Line. Withmessage("This is a modal Dialog.")//Prompt information. Withmessagecolor("#FFFFFFFF")//Information color. Withdialogcolor("#FFE74C3C")//dialog Color. Withicon(Getresources (). Getdrawable(R. drawable. Icon)//Set icon. Withduration( the)//Animation time. Witheffect(effect)//animation style. Withbutton1Text ("OK")//button1 . Withbutton2Text ("Cancel")//button2 . Iscancelableontouchoutside(true)//touch outside disappears. Setcustomview(R. Layout. Custom_view,v. GetContext())//Set Custom layout. Setbutton1Click (New View. Onclicklistener() {@Override public void OnClick (View v) {Toast. Maketext(V. GetContext(),"I ' m btn1", Toast. LENGTH_short). Show();} }). Setbutton2Click (New View. Onclicklistener() {@Override public void OnClick (View v) {Toast. Maketext(V. GetContext(),"I ' m btn2", Toast. LENGTH_short). Show();} }). Show();
4. Supported animation types, click to view supported animations, example download: Niftydialogeffects Demo
5. The effect is as shown at the beginning of this article
"Android UI Design" dialog dialog box (ii)