載入頁面遮擋耗時操作任務頁面--第三方開源--AndroidProgressLayout,androidview.layout
在Android的開發中,往往有這種需求,比如一個耗時的操作,連網擷取網狀圖片、內容,資料庫耗時讀寫等等,在此耗時操作過程中,開發人員也許不希望使用者再進行其他動作(其他動作可能會引起邏輯混亂),而此時需要給使用者一個額外的載入頁面遮擋住主邏輯代碼的運行,待首頁面的耗時操作完成後,自動消失這樣載入過度頁面,恢複出正常應該顯示的頁面。
舉個實際的例子,如代碼使用Android WebView開啟一個網頁連結試圖載入某個網站,但網路品質不佳,需要耗時很久,那麼在這個過程中,較好的使用者體驗做法是:給使用者一個載入進度頁面,遮擋住WebView。當載入的內容成功後在完全切換回正常的邏輯頁面。
Android AndroidProgressLayout實現了這樣的功能,Android AndroidProgressLayout在github上的項目首頁是:https://github.com/antonkrasov/AndroidProgressLayout
測試代碼如下:
activity_main.xml:
1 <com.github.androidprogresslayout.ProgressLayout xmlns:android="http://schemas.android.com/apk/res/android" 2 xmlns:progressLayout="http://schemas.android.com/apk/res-auto" 3 android:id="@+id/progressLayout" 4 android:layout_width="match_parent" 5 android:layout_height="match_parent" > 6 7 <TextView 8 android:id="@+id/textView" 9 android:layout_width="wrap_content"10 android:layout_height="wrap_content"11 android:textSize="40sp"12 android:layout_centerInParent="true" />13 14 </com.github.androidprogresslayout.ProgressLayout>
MainActivity.java:
1 package com.zzw.testandroidprogresslayout; 2 3 import com.github.androidprogresslayout.ProgressLayout; 4 5 import android.app.Activity; 6 import android.os.Bundle; 7 import android.os.Handler; 8 import android.os.Message; 9 import android.view.animation.Animation;10 import android.widget.TextView;11 12 public class MainActivity extends Activity {13 14 @Override15 protected void onCreate(Bundle savedInstanceState) {16 super.onCreate(savedInstanceState);17 setContentView(R.layout.activity_main);18 19 final ProgressLayout progressLayout = (ProgressLayout) findViewById(R.id.progressLayout);20 final TextView textView = (TextView) findViewById(R.id.textView);21 22 Handler handler = new Handler() {23 @Override24 public void handleMessage(Message msg) {25 textView.setText("測試完成");26 27 // 切換回正常顯示頁面28 progressLayout.showContent();29 }30 };31 // 開始載入... 假設從這裡開始一個耗時的操作將開始啟動,在此啟動過程中,開發人員希望使用者稍事休息,等待。。。32 progressLayout.showProgress();33 34 // 假設有一個耗時的載入商務邏輯,需要5秒完成。35 handler.sendEmptyMessageDelayed(0, 5000);36 }37 }