waiting_layout.xml 布局的代码:
<?xml version="1.0" encoding="UTF-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal" android:layout_width="280dip"
android:layout_gravity="center" android:gravity="center"
android:layout_height="120dip" android:background="@drawable/alpha_black_back">
<ProgressBar android:layout_width="wrap_content"
style="?android:attr/progressBarStyleLarge" android:layout_height="wrap_content"
android:layout_margin="5dip" />
<TextView android:id="@+id/identify_label" android:textColor="#fff"
android:paddingLeft="3dip" android:paddingRight="5dip"
android:layout_width="wrap_content" android:textSize="22sp"
android:layout_height="fill_parent" android:gravity="center"
android:text="@string/common_message_pleaseWait" />
</LinearLayout>
在Activity上层新建一个Window,用于承载loading. 这样做的好处,避免在Activity上加一个ProgressView. 具体代码如下:
protected final void showWaiting(boolean isBlur) {
isWaiting = true;
try {
WindowManager.LayoutParams lp = null;
if (isBlur) {
lp = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_FULLSCREEN
| WindowManager.LayoutParams.FLAG_BLUR_BEHIND
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
PixelFormat.TRANSLUCENT);
} else {
lp = new WindowManager.LayoutParams(
ViewGroup.LayoutParams.WRAP_CONTENT,
ViewGroup.LayoutParams.WRAP_CONTENT,
WindowManager.LayoutParams.TYPE_APPLICATION,
WindowManager.LayoutParams.FLAG_NOT_FOCUSABLE
| WindowManager.LayoutParams.FLAG_NOT_TOUCHABLE
| WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON,
PixelFormat.TRANSLUCENT);
}
WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
if (waitView == null) {
LayoutInflater inflate = (LayoutInflater) getSystemService(Context.LAYOUT_INFLATER_SERVICE);
waitView = inflate.inflate(R.layout.waiting_layout, null);
}
mWindowManager.addView(waitView, lp);
} catch (Throwable e) {
isWaiting = false;
Log.e(TAG, "[showWaiting]");
}
}
protected final void hideWaiting() {
isWaiting = false;
try {
if (waitView != null) {
WindowManager mWindowManager = (WindowManager) getSystemService(Context.WINDOW_SERVICE);
mWindowManager.removeView(waitView);
waitView = null;
}
} catch (Throwable e) {
Log.e(TAG, "[showWaiting]", e);
}
}