主要是等待框加入到屏幕正中
package com.ct.waiting; import android.app.Activity; import android.os.Bundle; import android.view.View; import android.view.View.OnClickListener; import android.widget.Button; public class MainActivity extends Activity implements OnClickListener { /** Called when the activity is first created. */ Waiting mWaiting; private Button btnShow; private Button btnHide; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); init(); } private void init(){ btnShow = (Button)findViewById(R.id.btn_showwaiting); btnHide = (Button)findViewById(R.id.btn_hidewaiting); btnHide.setOnClickListener(this); btnShow.setOnClickListener(this); } private void showWaiting(){ if(mWaiting == null){ mWaiting = new Waiting(this); }else { mWaiting.showWaiting(R.string.waiting); } } private void hideWaiting(){ if(mWaiting != null){ mWaiting.hideWaiting(); } } @Override public void onClick(View v) { // TODO Auto-generated method stub switch (v.getId()) { case R.id.btn_hidewaiting: hideWaiting(); break; case R.id.btn_showwaiting: showWaiting(); break; default: break; } } }
waiting类
package com.ct.waiting; import android.content.Context; import android.graphics.PixelFormat; import android.os.Handler; import android.os.Message; import android.view.LayoutInflater; import android.view.View; import android.view.ViewGroup; import android.view.WindowManager; import android.view.WindowManager.LayoutParams; import android.widget.TextView; public class Waiting { private Context context; private View waitView; private int tipResId; private boolean blur; private WindowManager windowManager; private Handler handler; private boolean waiting; public Waiting(Context context) { this.context = context; this.blur = false; this.waiting = false; this.windowManager = (WindowManager) context .getSystemService(Context.WINDOW_SERVICE); } public void wait(final Runnable runnable) { if (runnable == null) return; if (handler == null) { handler = new Handler() { @Override public void handleMessage(Message msg) { hideWaiting(); } }; } showWaiting(); new Thread() { public void run() { handler.removeMessages(0); try { runnable.run(); } catch (Throwable e) { } handler.sendEmptyMessage(0); }; }.start(); } public synchronized void showWaiting() { // showWaiting(tipResId); showWaiting(-1); } public synchronized void showWaiting(int tipResId) { try { if (waiting) return; if (waitView == null) { LayoutInflater inflate = LayoutInflater.from(context); waitView = inflate.inflate(R.layout.waiting_layout, null); } if (tipResId > 0) { ((TextView) waitView.findViewById(R.id.identify_label)) .setText(tipResId); } int flags = blur ? LayoutParams.FLAG_FULLSCREEN | LayoutParams.FLAG_BLUR_BEHIND : LayoutParams.FLAG_NOT_FOCUSABLE | LayoutParams.FLAG_NOT_TOUCHABLE; LayoutParams lp = new LayoutParams( ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT, LayoutParams.TYPE_APPLICATION, flags, PixelFormat.TRANSLUCENT); windowManager.addView(waitView, lp); waiting = true; } catch (Throwable e) { } } public synchronized void hideWaiting() { try { if (waiting && waitView != null) { windowManager.removeView(waitView); // waitView = null; } waiting = false; } catch (Throwable e) { } } public Waiting setBlur(boolean blur) { this.blur = blur; return this; } public Waiting setTipResource(int strResId) { this.tipResId = strResId; return this; } public boolean isWaiting() { return waiting; } }
视图文件
R.layout.waiting_layout
<?xml version="1.0" encoding="UTF-8"?> <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" android:id="@+id/waiting_view" android:layout_width="300dip" android:layout_height="fill_parent" android:background="@android:drawable/dialog_frame" android:orientation="horizontal" android:padding="20dp" > <ProgressBar android:layout_width="fill_parent" android:layout_height="wrap_content" android:layout_margin="5dp" /> <TextView android:id="@+id/identify_label" android:layout_width="wrap_content" android:layout_height="fill_parent" android:gravity="center" android:paddingLeft="3dp" android:paddingRight="5dp" android:textColor="#eee" android:textSize="22dp" /> </LinearLayout>