有兩種實現效果,一種是系統默認效果,一種是自定義效果;默認效果比較簡單但效果也比較單調,自定義能夠作出比較好看的效果來;android
public class Util { private static ProgressDialog processDia; /** * 顯示加載中對話框 * * @param context */ public static void showLoadingDialog(Context context,String message,boolean isCancelable) { if (processDia == null) { processDia= new ProgressDialog(context,R.style.dialog); //點擊提示框外面是否取消提示框 processDia.setCanceledOnTouchOutside(false); //點擊返回鍵是否取消提示框 processDia.setCancelable(isCancelable); processDia.setIndeterminate(true); processDia.setMessage(message); processDia.show(); } } /** * 關閉加載對話框 */ public static void closeLoadingDialog() { if (processDia != null) { if (processDia.isShowing()) { processDia.cancel(); } processDia = null; } } }
其中style.xml中須要設置dialog的背景、字體等屬性ide
<style name="dialog" parent="@android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowIsFloating">true</item> <item name="android:windowIsTranslucent">true</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@android:color/transparent</item> <item name="android:backgroundDimEnabled">true</item> <item name="android:windowContentOverlay">@null</item> <item name="android:textSize">25sp</item> </style>
定義動畫切換特效,anim/loading.xmlpost
<?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <item android:drawable="@drawable/progress_loading_image_01" android:duration="150"/> <item android:drawable="@drawable/progress_loading_image_02" android:duration="150"/> </animation-list>
自定義dialog頁面progress_dialog.xml字體
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_gravity="center" android:orientation="vertical" > <ImageView android:id="@+id/loadingIv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:background="@anim/loading"/> <TextView android:id="@+id/loadingTv" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_alignBottom="@+id/loadingIv" android:layout_centerHorizontal="true" android:textSize="20sp" android:text="正在加載中.." /> </RelativeLayout>
建立diglog類MyProgressDialog動畫
public class MyProgressDialog extends ProgressDialog { private AnimationDrawable mAnimation; private ImageView mImageView; private TextView mTextView; private String loadingTip; private int resid; /** * * @param context 上下文對象 * @param content 顯示文字提示信息內容 * @param id 動畫id */ public MyProgressDialog(Context context, String content, int resid) { super(context); this.loadingTip = content; this.resid = resid; //點擊提示框外面是否取消提示框 setCanceledOnTouchOutside(false); //點擊返回鍵是否取消提示框 setCancelable(false); setIndeterminate(true); } @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.progress_dialog); mTextView = (TextView) findViewById(R.id.loadingTv); mImageView = (ImageView) findViewById(R.id.loadingIv); mImageView.setBackgroundResource(resid); // 經過ImageView對象拿到背景顯示的AnimationDrawable mAnimation = (AnimationDrawable) mImageView.getBackground(); mImageView.post(new Runnable() { @Override public void run() { mAnimation.start(); } }); mTextView.setText(loadingTip); } }
最後在activity中進行調用,這樣就完成了一個自定義的dialog提示框this
public class MainActivity extends Activity { private MyProgressDialog dialog; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); Button btn = (Button)findViewById(R.id.btn_start); btn.setOnClickListener(new OnClickListener() { @Override public void onClick(View view) { showMyDialog(view); } }); } /** * 顯示對話框 * @param v */ public void showMyDialog(View v){ dialog =new MyProgressDialog(this, "正在加載中",R.anim.loading); dialog.show(); Handler handler =new Handler(); handler.postDelayed(new Runnable() { @Override public void run() { dialog.dismiss(); } }, 3000); } }