Android實(shí)現(xiàn)網(wǎng)絡(luò)加載時(shí)的對(duì)話框功能
效果預(yù)覽
簡(jiǎn)要說(shuō)明
現(xiàn)在android程序網(wǎng)絡(luò)請(qǐng)求操作是必不可少的,然而擁有好的交互體驗(yàn)的程序?qū)W(wǎng)絡(luò)耗時(shí)操作的處理尤為重要。
代碼說(shuō)明:
dialog_loading.xml
<?xml version="1.0" encoding="utf-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/dialog_view" android:orientation="vertical" android:layout_width="fill_parent" android:layout_height="fill_parent" android:gravity="center"> <ImageView android:id="@+id/img" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@android:color/transparent" android:src="@drawable/progress" /> <TextView android:id="@+id/tipTextView" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_marginLeft="10dp" android:text="數(shù)據(jù)加載中……" /> </LinearLayout>
這個(gè)布局就是我們自定義的顯示布局,比較簡(jiǎn)單明了,最外層一個(gè)垂直排列的線性布局,里面依次是一個(gè)imageview和textview。
loading_animation.xml
<?xml version="1.0" encoding="utf-8"?> <set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android"> <rotate android:interpolator="@android:anim/linear_interpolator" android:pivotX="50%" android:pivotY="50%" android:fromDegrees="0" android:toDegrees="+360" android:duration="1500" android:startOffset="-1" android:repeatMode="restart" android:repeatCount="-1"/> </set>
這個(gè)就是我們?cè)O(shè)置的旋轉(zhuǎn)的屬性動(dòng)畫(huà)的基本屬性操作,這個(gè)xml存在于res下的anim文件夾下(手動(dòng)創(chuàng)建文件夾)
CustomProgressDialog.class package com.cc.customprogressdialog.util; import android.app.Dialog; import android.content.Context; import android.graphics.Bitmap; import android.os.Bundle; import android.view.LayoutInflater; import android.view.View; import android.view.animation.Animation; import android.view.animation.AnimationUtils; import android.widget.ImageView; import android.widget.LinearLayout; import com.cc.customprogressdialog.R; /** * Created by CC on 2017/2/4. */ public class CustomProgressDialog extends Dialog { Context context; private ImageView spaceshipImage; private Animation hyperspaceJumpAnimation; public CustomProgressDialog(Context context) { super(context); this.context = context; } public CustomProgressDialog(Context context, int theme) { super(context, theme); this.context = context; } @Override protected void onCreate(Bundle savedInstanceState) { LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(R.layout.dialog_loading, null);// 得到加載view LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加載布局 // main.xml中的ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img); // 加載動(dòng)畫(huà) hyperspaceJumpAnimation = AnimationUtils.loadAnimation(context, R.anim.loading_animation); // 使用ImageView顯示動(dòng)畫(huà) spaceshipImage.startAnimation(hyperspaceJumpAnimation); setCancelable(false);// 不可以用“返回鍵”取消 setContentView(layout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.MATCH_PARENT, LinearLayout.LayoutParams.MATCH_PARENT));// 設(shè)置布局 } }
這個(gè)類(lèi)就是自定義的ProgressDialog,代碼的關(guān)鍵步驟我都寫(xiě)了注釋。
使用
package com.cc.customprogressdialog; import android.os.AsyncTask; import android.os.Bundle; import android.os.SystemClock; import android.support.v7.app.AppCompatActivity; import android.view.View; import android.widget.Button; import com.cc.customprogressdialog.util.CustomProgressDialog; public class MainActivity extends AppCompatActivity { private Button btn; private CustomProgressDialog mProgressDialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); btn = (Button) findViewById(R.id.btn); btn.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View view) { new AsyncTask<Void, Void, Void>() { @Override protected void onPreExecute() { super.onPreExecute(); mProgressDialog = new CustomProgressDialog(MainActivity.this, R.style.loading_dialog); mProgressDialog.show(); } @Override protected Void doInBackground(Void... voids) { SystemClock.sleep(2000); return null; } @Override protected void onPostExecute(Void aVoid) { super.onPostExecute(aVoid); mProgressDialog.dismiss(); } }.execute(); } }); } }
上述代碼我們看到我在主activity里面添加一個(gè)按鈕,實(shí)現(xiàn)其點(diǎn)擊事件,在點(diǎn)擊事件中我創(chuàng)建了一個(gè)異步操作,模擬網(wǎng)絡(luò)耗時(shí)。
注意一點(diǎn)我在創(chuàng)建CustomProgressDialog的時(shí)候傳入了一個(gè)style,系統(tǒng)默認(rèn)的不給力,所以只能自己寫(xiě)了一個(gè)。
<!-- 自定義loading dialog --> <style name="loading_dialog" parent="android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:background">#00000000</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>
屬性的參數(shù)意思有興趣的自行百度,在這里不一一介紹了。
實(shí)現(xiàn)的代碼就這么簡(jiǎn)單但很實(shí)用,希望對(duì)各位讀者有所幫助。最后附上完整的代碼:
http://xiazai.jb51.net/201702/yuanma/CustomProgressDialog
以上所述是小編給大家介紹的Android實(shí)現(xiàn)網(wǎng)絡(luò)加載時(shí)的對(duì)話框功能,希望對(duì)大家有所幫助,如果大家有任何疑問(wèn)請(qǐng)給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對(duì)腳本之家網(wǎng)站的支持!
- Android加載loading對(duì)話框的功能及實(shí)例代碼(不退出沉浸式效果)
- Android自定義Dialog實(shí)現(xiàn)加載對(duì)話框效果
- Android加載對(duì)話框同時(shí)異步執(zhí)行實(shí)現(xiàn)方法
- Android中自定義對(duì)話框(Dialog)的實(shí)例代碼
- Android實(shí)現(xiàn)底部對(duì)話框BottomDialog彈出實(shí)例代碼
- Android實(shí)現(xiàn)點(diǎn)擊AlertDialog上按鈕時(shí)不關(guān)閉對(duì)話框的方法
- 實(shí)例詳解Android自定義ProgressDialog進(jìn)度條對(duì)話框的實(shí)現(xiàn)
- Android中AlertDialog各種對(duì)話框的用法實(shí)例詳解
- 詳解Android 全局彈出對(duì)話框SYSTEM_ALERT_WINDOW權(quán)限
- Android實(shí)現(xiàn)加載對(duì)話框
相關(guān)文章
Fragment通過(guò)FragmentManager實(shí)現(xiàn)通信功能詳細(xì)講解
這篇文章主要介紹了Fragment通過(guò)FragmentManager實(shí)現(xiàn)通信功能,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)吧2023-01-01Android應(yīng)用中制作選中后圖標(biāo)變大浮動(dòng)效果的代碼分享
這篇文章主要介紹了Android應(yīng)用中制作選中后圖標(biāo)變大浮動(dòng)效果的代碼分享,這里作者舉了一個(gè)體育賽事app的例子,需要的朋友可以參考下2016-02-02Android輸入框控件ClearEditText實(shí)現(xiàn)清除功能
這篇文章主要為大家詳細(xì)介紹了Android輸入框控件ClearEditText實(shí)現(xiàn)清除功能,感興趣的小伙伴們可以參考一下2016-05-05關(guān)于Android添加fragment后版本不兼容問(wèn)題
這篇文章主要介紹了Android添加fragment后版本不兼容問(wèn)題的解決方法,需要的朋友可以參考下2017-12-12解決EditText、ListView以及GridView同時(shí)使用,輸入法自動(dòng)跳出來(lái)的方法
本篇文章是對(duì)在Android中EditText、ListView以及GridView同時(shí)使用,輸入法自動(dòng)跳出來(lái)的解決方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Android實(shí)現(xiàn)網(wǎng)絡(luò)加載圖片點(diǎn)擊大圖后瀏覽可縮放
這篇文章主要為大家詳細(xì)介紹了Android實(shí)現(xiàn)網(wǎng)絡(luò)加載圖片點(diǎn)擊大圖后瀏覽可縮放,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2017-12-12Android之自定義實(shí)現(xiàn)BaseAdapter(通用適配器二)
這篇文章主要為大家詳細(xì)介紹了Android之自定義實(shí)現(xiàn)BaseAdapter通用適配器第二篇,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-08-08android設(shè)備間實(shí)現(xiàn)無(wú)線投屏的示例代碼
Android提供了MediaProjection來(lái)實(shí)現(xiàn)錄屏,通過(guò)MediaProjection可以獲取當(dāng)前屏幕的視頻流,而視頻流需要通過(guò)編解碼來(lái)壓縮進(jìn)行傳輸,通過(guò)MediaCodec可實(shí)現(xiàn)視頻的編碼和解碼,這篇文章主要介紹了android設(shè)備間實(shí)現(xiàn)無(wú)線投屏,需要的朋友可以參考下2022-06-06