Android AndroidProgressLayout:加载页面遮挡耗时操作任务页面
在Android的开发中,往往有这种需求,比如一个耗时的操作,联网获取网络图片、内容,数据库耗时读写等等,在此耗时操作过程中,开发者也许不希望用户再进行其他操作(其他操作可能会引起逻辑混乱),而此时需要给用户一个额外的加载页面遮挡住主逻辑代码的运行,待主页面的耗时操作完成后,自动消失这样加载过度页面,恢复出正常应该显示的页面。
举个实际的例子,如代码使用Android WebView打开一个网页链接试图加载某个网站,但网络质量不佳,需要耗时很久,那么在这个过程中,较好的用户体验做法是:给用户一个加载进度页面,遮挡住WebView。当加载的内容成功后在完全切换回正常的逻辑页面。
Android AndroidProgressLayout实现了这样的功能,Android AndroidProgressLayout在github上的项目主页是:https://github.com/antonkrasov/AndroidProgressLayout
AndroidProgressLayout要实现的功能如图:
![](https://img-blog.csdn.net/20151126231244777?watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQv/font/5a6L5L2T/fontsize/400/fill/I0JBQkFCMA==/dissolve/70/gravity/Center)
现在给出一个例子加以说明。
使用AndroidProgressLayout,需要先写一个布局:
- <com.github.androidprogresslayout.ProgressLayout xmlns:android="http://schemas.android.com/apk/res/android"
- xmlns:progressLayout="http://schemas.android.com/apk/res-auto"
- android:id="@+id/progress_layout"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- progressLayout:progressBackground="#4fc3f7" >
-
- <TextView
- android:id="@+id/text"
- android:layout_width="match_parent"
- android:layout_height="match_parent"
- android:gravity="center" />
-
- </com.github.androidprogresslayout.ProgressLayout>
Java代码:
- package zhangphil.demo;
-
- import com.github.androidprogresslayout.ProgressLayout;
-
- import android.app.Activity;
- import android.os.Bundle;
- import android.os.Handler;
- import android.os.Message;
- import android.widget.TextView;
-
-
- public class MainActivity extends Activity {
-
- @Override
- protected void onCreate(Bundle savedInstanceState) {
- super.onCreate(savedInstanceState);
- setContentView(R.layout.activity_main);
-
- final ProgressLayout progressLayout = (ProgressLayout) findViewById(R.id.progress_layout);
-
- final TextView text = (TextView) findViewById(R.id.text);
-
- Handler handler = new Handler() {
- @Override
- public void handleMessage(Message msg) {
- text.setText("加载完毕!");
-
-
- progressLayout.showContent();
- }
- };
-
-
- progressLayout.showProgress();
-
-
- handler.sendEmptyMessageDelayed(0, 5000);
- }
- }