Adnroid 自定義ProgressDialog加載中(加載圈)
前兩天在做項(xiàng)目的時(shí)候發(fā)現(xiàn)有時(shí)候在訪問(wèn)網(wǎng)絡(luò)數(shù)據(jù)的時(shí)候由于后臺(tái)要做的工作較多,給我們返回?cái)?shù)據(jù)的時(shí)間較長(zhǎng),所以老大叫我加了一個(gè)加載中的logo圖用來(lái)提高用戶體驗(yàn).
于是就在網(wǎng)上找了許多大神寫(xiě)的案例,再結(jié)合自己的情況完成了一個(gè)Loading工具類
效果:
ok,現(xiàn)在來(lái)說(shuō)說(shuō)怎么做的
先自定義一個(gè)類繼承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() {//開(kāi)啟 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){//點(diǎn)擊加載并按鈕模仿網(wǎng)絡(luò)請(qǐng)求 loading = new Loading_view(this,R.style.CustomDialog); loading.show(); new Handler().postDelayed(new Runnable() {//定義延時(shí)任務(wù)模仿網(wǎng)絡(luò)請(qǐng)求 @Override public void run() { loading.dismiss();//3秒后調(diào)用關(guān)閉加載的方法 } }, 3000); } }
為什么會(huì)這樣,不懂然后就去百度,google然后在一大神的文章里發(fā)現(xiàn)了,但是我在寫(xiě)這文章的時(shí)候才發(fā)現(xiàn)當(dāng)初沒(méi)有保存大神的地址再也找不到了
原來(lái)需要在創(chuàng)建自定義的loading 的時(shí)候在傳入 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 再來(lái)一次
以上所述是小編給大家介紹的Adnroid 自定義ProgressDialog加載中(加載圈),希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android ProgressDialog進(jìn)度條使用詳解
- Android 自定義ProgressDialog進(jìn)度條對(duì)話框用法詳解
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- Android自定義ProgressDialog進(jìn)度等待框
- Android編程實(shí)現(xiàn)加載等待ProgressDialog的方法
- Android ProgressBar進(jìn)度條和ProgressDialog進(jìn)度框的展示DEMO
- android中ProgressDialog與ProgressBar的使用詳解
相關(guān)文章
Android系列---JSON數(shù)據(jù)解析的實(shí)例
JSON(JavaScript Object Notation)和XML,并稱為客戶端和服務(wù)端交互解決方案的倚天劍和屠龍刀,這篇文章主要介紹了Android系列---JSON數(shù)據(jù)解析的實(shí)例,有興趣的可以了解一下。2016-11-11Android基礎(chǔ)控件(EditView、SeekBar等)的使用方法
這篇文章主要介紹了Android基礎(chǔ)控件的屬性及使用方法,介紹了基礎(chǔ)控件有TextView、ImageView、Button、EditView等,感興趣的小伙伴們可以參考一下2016-03-03深入理解Android熱修復(fù)技術(shù)原理之so庫(kù)熱修復(fù)技術(shù)
通常情況下,大多數(shù)人希望android下熱補(bǔ)丁方案能夠做到補(bǔ)丁的全方位修復(fù),包括類修復(fù)/資源修復(fù)/so庫(kù)的修復(fù)。 這里主要介紹熱補(bǔ)丁之so庫(kù)修復(fù)思路2021-06-06Android基于ImageSwitcher實(shí)現(xiàn)圖片切換功能
這篇文章主要介紹了Android基于ImageSwitcher實(shí)現(xiàn)圖片切換功能的相關(guān)資料,需要的朋友可以參考下2016-02-02Android?MaterialButton使用實(shí)例詳解(告別shape、selector)
我們平時(shí)寫(xiě)布局,當(dāng)遇到按鈕需要圓角、或者描邊等,通常的方法是新建一個(gè)xml文件,在shape標(biāo)簽下寫(xiě),然后通過(guò)android:background或setBackground(drawable)設(shè)置,這篇文章主要給大家介紹了關(guān)于Android?MaterialButton使用詳解的相關(guān)資料,需要的朋友可以參考下2022-09-09Android隱藏和沉浸式虛擬按鍵NavigationBar的實(shí)現(xiàn)方法
今天小編就為大家分享一篇Android隱藏和沉浸式虛擬按鍵NavigationBar的實(shí)現(xiàn)方法,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-07-07Android中ActionBar和ToolBar添加返回箭頭的實(shí)例代碼
這篇文章主要介紹了Android中ActionBar和ToolBar添加返回箭頭的實(shí)例代碼,需要的朋友可以參考下2017-09-09