Adnroid 自定義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 再來一次

以上所述是小編給大家介紹的Adnroid 自定義ProgressDialog加載中(加載圈),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會及時回復大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
flutter InheritedWidget使用方法總結(jié)
這篇文章主要為大家介紹了flutter InheritedWidget使用方法總結(jié)示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪2022-11-11
Android實現(xiàn)使用微信登錄第三方APP的方法
這篇文章主要介紹了Android實現(xiàn)使用微信登錄第三方APP的方法,結(jié)合實例形式分析了Android微信登錄APP的操作步驟與具體功能實現(xiàn)技巧,需要的朋友可以參考下2016-11-11
android ViewPager實現(xiàn)自動無限輪播和下方向?qū)A點
本篇文章主要介紹了android ViewPager實現(xiàn)自動輪播和下方向?qū)A點,具有一定的參考價值,感興趣的小伙伴們可以參考一下。2017-02-02
規(guī)避Android開發(fā)中內(nèi)存泄漏陷阱的解決方案
在Android開發(fā)中,內(nèi)存泄漏是一個常見但容易被忽視的問題,它會導致應(yīng)用程序占用過多的內(nèi)存資源,最終影響應(yīng)用的性能和用戶體驗,本文將深入探討Android常見的內(nèi)存泄漏問題,并提供優(yōu)化指南,需要的朋友可以參考下2024-05-05
Android實現(xiàn)偵聽電池狀態(tài)顯示、電量及充電動態(tài)顯示的方法
這篇文章主要介紹了Android實現(xiàn)偵聽電池狀態(tài)顯示、電量及充電動態(tài)顯示的方法,非常實用的功能,需要的朋友可以參考下2014-09-09
Android ImageView實現(xiàn)圖片裁剪和顯示功能
這篇文章主要介紹了Android ImageView實現(xiàn)圖片裁剪和顯示功能,非常不錯,具有參考借鑒價值,需要的朋友可以參考下2017-02-02

