android自定義ProgressDialog加載效果
用來記錄自己所用到的知識
前兩天在做項目的時候發(fā)現(xiàn)有時候在訪問網(wǎng)絡(luò)數(shù)據(jù)的時候由于后臺要做的工作較多,給我們返回數(shù)據(jù)的時間較長,所以老大叫我加了一個加載中的logo圖用來提高用戶體驗.
于是就在網(wǎng)上找了許多大神寫的案例,再結(jié)合自己的情況完成了一個Loading工具類
效果:

ok,現(xiàn)在來說說怎么做的
先自定義一個類繼承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);//loading的xml文件
WindowManager.LayoutParams params = getWindow().getAttributes();
params.width = WindowManager.LayoutParams.WRAP_CONTENT;
params.height = WindowManager.LayoutParams.WRAP_CONTENT;
getWindow().setAttributes(params);
}
@Override
public void show() {//開啟
super.show();
}
@Override
public void dismiss() {//關(guān)閉
super.dismiss();
}
}
設(shè)置loading布局文件
<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"http://背景色 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"http://加載圈的樣式 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="加載中..." android:textColor="#9a9b98" android:textSize="12sp"/> </LinearLayout>
背景色(可自行調(diào)整)
<?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>
加載圈樣式(可自行調(diào)整)
<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){//點擊加載并按鈕模仿網(wǎng)絡(luò)請求
loading = new Loading_view(this,R.style.CustomDialog);
loading.show();
new Handler().postDelayed(new Runnable() {//定義延時任務(wù)模仿網(wǎng)絡(luò)請求
@Override
public void run() {
loading.dismiss();//3秒后調(diào)用關(guān)閉加載的方法
}
}, 3000);
}
}

為什么會這樣,不懂然后就去百度,google然后在一大神的文章里發(fā)現(xiàn)了,但是我在寫這文章的時候才發(fā)現(xiàn)當初沒有保存大神的地址再也找不到了
原來需要在創(chuàng)建自定義的loading 的時候在傳入 new Loading_view(this,R.style.CustomDialog);樣式
<style name="CustomDialog" parent="Theme.AppCompat.Dialog"> <item name="android:backgroundDimEnabled">false</item> <item name="android:windowBackground">@android:color/transparent</item> </style>
ok 再來一次

ok成功!
以上就是本文的全部內(nèi)容,希望對大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
- 實例詳解Android自定義ProgressDialog進度條對話框的實現(xiàn)
- Android自定義ProgressDialog進度等待框
- Android ProgressBar進度條和ProgressDialog進度框的展示DEMO
- Android 自定義ProgressDialog進度條對話框用法詳解
- Android 中通過實現(xiàn)線程更新Progressdialog (對話進度條)
- Android ProgressDialog進度條使用詳解
- Android編程實現(xiàn)加載等待ProgressDialog的方法
- Android自定義ProgressDialog加載圖片
- Android ProgressDialog使用總結(jié)
- Android開發(fā)之ProgressDialog進度對話框用法示例
相關(guān)文章
SwipeRefreshLayout+RecyclerView實現(xiàn)上拉刷新和下拉刷新功能
這篇文章主要介紹了SwipeRefreshLayout+RecyclerView實現(xiàn)上拉刷新和下拉刷新功能,具有一定的參考價值,感興趣的小伙伴們可以參考一下2019-01-01
Android使用xUtils3.0實現(xiàn)文件上傳
這篇文章主要為大家詳細介紹了Android使用xUtils3.0實現(xiàn)文件上傳的相關(guān)資料,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-11-11
Android實現(xiàn)微信自動向附近的人打招呼(AccessibilityService)
這篇文章主要為大家詳細介紹了實現(xiàn)微信自動向附近的人打招呼,實現(xiàn)收到指定賬戶推送文章時自動進入微信打開鏈接,具有一定的參考價值,感興趣的小伙伴們可以參考一下2016-12-12
Flutter Recovering Stream Errors小技巧
這篇文章主要為大家介紹了Flutter Recovering Stream Errors小技巧,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-12-12
Android開發(fā)中Listview動態(tài)加載數(shù)據(jù)的方法示例
這篇文章主要介紹了Android開發(fā)中Listview動態(tài)加載數(shù)據(jù)的方法,結(jié)合實例形式較為詳細的分析了Android操作ListView界面布局與數(shù)據(jù)動態(tài)更新相關(guān)操作技巧,需要的朋友可以參考下2017-10-10
Android實現(xiàn)屏蔽微信拉黑和刪除聯(lián)系人功能示例
本篇文章主要介紹了Android實現(xiàn)屏蔽微信拉黑和刪除聯(lián)系人功能示例,具有一定的參考價值,有興趣的可以了解一下。2017-02-02

