Adnroid custom ProgressDialog loading, custom progressdialog
Used to record the knowledge you use
When I was working on a project two days ago, I found that sometimes I had to do a lot of work in the background when accessing network data. It took a long time to return data to us, so the boss told me to add a loaded logo image to improve the user experience.
So I found a lot of cases written by great gods on the Internet and completed a Loading tool Class Based on my own situation.
Effect:
OK. Now let's talk about how to do it.
First define a class to inherit ProgressDialog
Public class Loading_view extends ProgressDialog {
Public Loading_view (Context context ){
Super (context );
}
Public Loading_view (Context context, int theme ){
Super (context, theme );
}
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
Init (getContext ());
}
Private void init (Context context ){
SetCancelable (true );
SetCanceledOnTouchOutside (false );
SetContentView (R. layout. loading); // xml file of loading
WindowManager. LayoutParams params = getWindow (). getAttributes ();
Params. width = WindowManager. LayoutParams. WRAP_CONTENT;
Params. height = WindowManager. LayoutParams. WRAP_CONTENT;
GetWindow (). setAttributes (params );
}
@ Override
Public void show () {// enable
Super. show ();
}
@ Override
Public void dismiss () {// close
Super. dismiss ();
}
}
Set the loading layout File
<LinearLayout xmlns: android = "http://schemas.android.com/apk/res/android"
Android: layout_width = "100dp"
Android: layout_height = "100dp"
Android: layout_gravity = "center_horizontal"
Android: background = "@ drawable/shape_dialog_bg" // background color
Android: layout_centerInParent = "true"
Android: orientation = "vertical">
<RelativeLayout
Android: layout_width = "match_parent"
Android: layout_height = "wrap_content"
Android: layout_marginTop = "10dp">
<ProgressBar
Android: id = "@ + id/pb_load"
Android: layout_width = "65dp"
Android: layout_height = "65dp"
Android: indeterminateDrawable = "@ drawable/progressbar" // style of the loaded circle
Android: layout_centerInParent = "true"/>
</RelativeLayout>
<TextView
Android: id = "@ + id/TV _load_dialog"
Android: layout_width = "wrap_content"
Android: layout_height = "wrap_content"
Android: layout_gravity = "center_horizontal"
Android: text = "loading ..."
Android: textColor = "#9a9b98"
Android: textSize = "12sp"/>
</LinearLayout>
Background color (adjustable)
<? Xml version = "1.0" encoding = "UTF-8"?>
<Shape xmlns: android = "http://schemas.android.com/apk/res/android">
<Corners android: radius = "8dp"/>
<Solid android: color = "#88000000"/>
</Shape>
Load the circle style (you can adjust it yourself)
<animated-rotate
xmlns:android="http://schemas.android.com/apk/res/android"
android:pivotX="50%" android:pivotY="50%"
android:fromDegrees="0"
android:toDegrees="720">
<shape
android:shape="ring"
android:innerRadiusRatio="3"
android:thicknessRatio="15"
android:useLevel="false">
<gradient
android:type="sweep"
android:useLevel="false"
android:startColor="#55c6c6c6"
android:centerColor="#c6c6c6"
android:centerY="0.50"
android:endColor="#c6c6c6" />
</shape>
</animated-rotate>
OK
Public class MainActivity extends AppCompatActivity {
Private Loading_view loading;
@ Override
Protected void onCreate (Bundle savedInstanceState ){
Super. onCreate (savedInstanceState );
SetContentView (R. layout. activity_main );
}
Public void loding (View v) {// click the load and button to simulate the network request
Loading = new Loading_view (this, R. style. CustomDialog );
Loading. show ();
New Handler (). postDelayed (new Runnable () {// define a delayed task to simulate a network request
@ Override
Public void run (){
Loading. dismiss (); // call the method to disable loading after 3 seconds
}
},3000 );
}
}
Why is that? If you don't understand it, go to Baidu, google, and find it in the article of a great god, however, when I was writing this article, I found that the address that didn't save the great god could no longer be found.
The new Loading_view (this, R. style. CustomDialog); style must be input when the custom loading is created.
<Style name = "CustomDialog" parent = "Theme. AppCompat. Dialog">
<Item name = "android: backgroundDimEnabled"> false </item>
<Item name = "android: windowBackground"> @ android: color/transparent </item>
</Style>
OK again
OK!