最近在逛Github的時候發現了一個安卓加載中的動畫架構AVLoadingIndicatorView,這個架構非常的不錯,裡面集合了各種各樣的加載架構,有很多的樣式可以選擇。種類雖然比較多,但不是我想要的樣式,此篇文章主要講的是在其基礎上封裝一個自己想要的樣式。
廢話不多說,直接上圖:
本來就是站在巨人的肩膀上的,至于其具體的用法我就不詳細闡述了,給上大家github的位址,大家可以自己去看:https://github.com/81813780/AVLoadingIndicatorView
加載的動畫雖然實作了,但是就我個人來說還是有幾點需要加上的:
1 這個加載的動畫沒得文字配套。
2 加載的動畫出現後背景沒變化。
3 加載的動畫出現後,界面上的東西仍然能進行點選或者觸摸的操作。
當然每個人的需求不一樣,這三點隻是我個人認為需要加上的,于是自己就動手處理了。
實作後的效果是這樣的:
不知道大家喜歡不喜歡,反正我個人感覺這樣看着要好一點。
下面把代碼貼出來:
1 動畫彈出的工具類UIHelper
public class UIHelper {
/**
* 顯示加載對話框
*
* @param context 上下文
* @param msg 對話框顯示内容
*/
public static void showDialogForLoading(Context context, String msg) {
View view = LayoutInflater.from(context).inflate(R.layout.layout_loading_dialog, null);
TextView loadingText = (TextView) view.findViewById(R.id.id_tv_loading_dialog_text);
AVLoadingIndicatorView avLoadingIndicatorView = (AVLoadingIndicatorView) view.findViewById(R.id.AVLoadingIndicatorView);
loadingText.setText(msg);
final Dialog mLoadingDialog = new Dialog(context, R.style.loading_dialog_style);
mLoadingDialog.setCancelable(false);
mLoadingDialog.setContentView(view, new LinearLayout.LayoutParams(LinearLayout.LayoutParams.MATCH_PARENT,
LinearLayout.LayoutParams.MATCH_PARENT));
mLoadingDialog.show();
avLoadingIndicatorView.smoothToShow();
mLoadingDialog.setOnKeyListener(new DialogInterface.OnKeyListener() {
@Override
public boolean onKey(DialogInterface dialog, int keyCode, KeyEvent event) {
if (keyCode == KeyEvent.KEYCODE_BACK) {
mLoadingDialog.hide();
return true;
}
return false;
}
});
}
}
2 Dialog的布局檔案layout_loading_dialog:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
xmlns:app="http://schemas.android.com/apk/res-auto"
android:background="@drawable/bg_loading_dialog_shape"
android:gravity="center"
android:minHeight="60dp"
android:minWidth="180dp"
android:orientation="vertical"
android:padding="10dp">
<com.wang.avi.AVLoadingIndicatorView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true"
android:id="@+id/AVLoadingIndicatorView"
app:indicatorColor="#1390ea"
app:indicatorName="PacmanIndicator"/>
<TextView
android:id="@+id/id_tv_loading_dialog_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="5dp"
android:text="加載中…"
android:textColor="#555555"
android:textSize="14sp" />
</LinearLayout>
3 Dialog的樣式loading_dialog_stytle:
<!-- 自定義Loading Dialog -->
<style name="loading_dialog_style" parent="@android:style/Theme.Dialog">
<item name="android:windowFrame">@null</item>
<item name="android:windowNoTitle">true</item>
<item name="android:windowBackground">@color/transparent</item>
<item name="android:windowIsFloating">true</item>
<item name="android:windowContentOverlay">@null</item>
</style>
以上的幾點實作後就可以在你想要的地方加上一句代碼 UIHelper.showDialogForLoading(MainActivity.this,"正在加載。。。");即可,當然這邊隻是單純的為了展示效果并沒有加上主動消失掉加載動畫的代碼,這個大家如果感興趣可以自己添加,比如資料請求開始的時候動畫開始,資料請求結束的時候動畫結束等。
最後還是想說非常感謝安卓的開源,大神的分享!