android介面之美---自定義網路請求進度載入對話方塊
阿新 • • 發佈:2019-01-25
1.定義進度對話方塊類
<span style="font-size:18px;">public class ProgressDialog { public Dialog mDialog; private AnimationDrawable animationDrawable = null; public ProgressDialog(Context context, String message) { LayoutInflater inflater = LayoutInflater.from(context); View view = inflater.inflate(R.layout.progress_view, null); TextView text = (TextView) view.findViewById(R.id.progress_message); text.setText(message); ImageView loadingImage = (ImageView) view.findViewById(R.id.progress_view); loadingImage.setImageResource(R.drawable.progress_dialog_loding); animationDrawable = (AnimationDrawable)loadingImage.getDrawable(); if(animationDrawable!=null){ animationDrawable.setOneShot(false); animationDrawable.start(); } mDialog = new Dialog(context, R.style.dialog); mDialog.setContentView(view); mDialog.setCanceledOnTouchOutside(false); } public void show() { mDialog.show(); } public void setCanceledOnTouchOutside(boolean cancel) { mDialog.setCanceledOnTouchOutside(cancel); } public void dismiss() { if(mDialog.isShowing()) { mDialog.dismiss(); animationDrawable.stop(); } } public boolean isShowing(){ if(mDialog.isShowing()) { return true; } return false; } }</span>
2. 對話方塊佈局檔案
3.<span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <FrameLayout xmlns:android="http://schemas.android.com/apk/res/android" android:layout_width="fill_parent" android:layout_height="fill_parent"> <LinearLayout android:layout_width="80dp" android:layout_height="80dp" android:layout_gravity="center" android:background="@drawable/progress_bg" android:gravity="center" android:orientation="vertical"> <ImageView android:id="@+id/progress_view" android:layout_width="50dp" android:layout_height="50dp" android:src="@mipmap/bga_refresh_loading01"/> <TextView android:id="@+id/progress_message" android:layout_width="wrap_content" android:layout_height="wrap_content" android:textColor="#d1d4d7" android:textSize="13sp"/> </LinearLayout> </FrameLayout></span>
<span style="font-size:18px;">progress_dialog_loding.xml 圖片載入動畫</span>
4.用到的圖片資源<pre name="code" class="html"><span style="font-size:18px;"><?xml version="1.0" encoding="utf-8"?> <animation-list xmlns:android="http://schemas.android.com/apk/res/android" android:oneshot="false" > <item android:drawable="@mipmap/bga_refresh_loading01" android:duration="100"/> <item android:drawable="@mipmap/bga_refresh_loading02" android:duration="100"/> <item android:drawable="@mipmap/bga_refresh_loading03" android:duration="100"/> <item android:drawable="@mipmap/bga_refresh_loading04" android:duration="100"/> <item android:drawable="@mipmap/bga_refresh_loading05" android:duration="100"/> <item android:drawable="@mipmap/bga_refresh_loading06" android:duration="100"/> <item android:drawable="@mipmap/bga_refresh_loading07" android:duration="100"/> <item android:drawable="@mipmap/bga_refresh_loading08" android:duration="100"/> <item android:drawable="@mipmap/bga_refresh_loading09" android:duration="100"/> <item android:drawable="@mipmap/bga_refresh_loading10" android:duration="100"/> <item android:drawable="@mipmap/bga_refresh_loading11" android:duration="100"/> <item android:drawable="@mipmap/bga_refresh_loading12" android:duration="100"/> </animation-list></span>
5.在activity中使用
在onResume方法中
<span style="font-size:18px;">if (progressDialog == null) {
progressDialog = new ProgressDialog(mContext, "正在載入...");
progressDialog.show();
}</span>
在請求網路成功或失敗後呼叫
progressDialog.dismiss();方法