標籤:android style blog http java color
Android自訂類似ProgressDialog效果的Dialog.
方法如下:
1.首先準備兩張自己要定義成哪樣子的效果的圖片和背景圖片(也可以不要背景)。
如我要的效果:
2.定義loading_dialog.xml布局檔案(這裡你也可以按自己的布局效果定義,關鍵是要有個imageView):
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
- android:id="@+id/dialog_view"
- android:orientation="vertical"
- android:layout_width="fill_parent"
- android:layout_height="fill_parent"
- android:minHeight="60dp"
- android:minWidth="180dp"
- android:gravity="center"
- android:padding="10dp"
- android:background="@drawable/loading_bg">
- <ImageView
- android:id="@+id/img"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:src="@drawable/publicloading"
- />
- <TextView
- android:id="@+id/tipTextView"
- android:layout_width="wrap_content"
- android:layout_height="wrap_content"
- android:layout_marginLeft="10dp"
- android:text="資料載入中……" />
- </LinearLayout>
3.定義一個loadingDialog中imageView轉動的動畫:loading_animation.xml
[html] view plaincopy
- <?xml version="1.0" encoding="utf-8"?>
- <set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android">
- <rotate
- android:interpolator="@android:anim/linear_interpolator"
- android:pivotX="50%"
- android:pivotY="50%"
- android:fromDegrees="0"
- android:toDegrees="+360"
- android:duration="1500"
- android:startOffset="-1"
- android:repeatMode="restart"
- android:repeatCount="-1"/>
- </set>
4.定義dialog的style.
<!-- 自訂loading dialog --> <style name="loading_dialog" parent="android:style/Theme.Dialog"> <item name="android:windowFrame">@null</item> <item name="android:windowNoTitle">true</item> <item name="android:windowBackground">@drawable/loading_bg</item> <item name="android:windowIsFloating">true</item> <item name="android:windowContentOverlay">@null</item> </style>
[java] view plaincopy
5.寫點建立Dialog的代碼,你可以自己封裝成一個方法。
/** * 得到自訂的progressDialog * @param context * @param msg * @return */ public static Dialog createLoadingDialog(Context context, String msg) { LayoutInflater inflater = LayoutInflater.from(context); View v = inflater.inflate(R.layout.loading_dialog, null);// 得到載入view LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 載入布局 // main.xml中的ImageView ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img); TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 提示文字 // 載入動畫 Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation( context, R.anim.load_animation); // 使用ImageView顯示動畫 spaceshipImage.startAnimation(hyperspaceJumpAnimation); tipTextView.setText(msg);// 設定載入資訊 Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 建立自訂樣式dialog loadingDialog.setCancelable(false);// 不可以用“返回鍵”取消 loadingDialog.setContentView(layout, new LinearLayout.LayoutParams( LinearLayout.LayoutParams.FILL_PARENT, LinearLayout.LayoutParams.FILL_PARENT));// 設定布局 return loadingDialog; }
最後來張整體的:
-------------------------------------------------------------------------------------------------------------------------------------
上一篇寫了使用系統ProgressBar實現的稍微好看的ProgressDialog,如果你想自己的ProgressDialog更具有自己的風格,那用圖片去實現,將會達到你的目的。話不多說,先看效果:看了上篇以後,實現這個也是很簡單的,只需要把布局檔案的ProgressBar換成ImageView就可以了。
換成ImageView以後,我們需要讓這個ImageView動起來,這就需要對它進行一個anim處理。
實現思路是:用一個動畫代替ImageView的圖片資源,然後讓動畫動起來就OK了。
將這個資源當作ImageView的背景圖片
<?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/icon_loading1" android:duration="100"/> <item android:drawable="@drawable/icon_loading2" android:duration="100"/> <item android:drawable="@drawable/icon_loading3" android:duration="100"/> <item android:drawable="@drawable/icon_loading4" android:duration="100"/> <item android:drawable="@drawable/icon_loading5" android:duration="100"/> <item android:drawable="@drawable/icon_loading6" android:duration="100"/> <item android:drawable="@drawable/icon_loading7" android:duration="100"/> <item android:drawable="@drawable/icon_loading8" android:duration="100"/> </animation-list>
然後就要在MyProgressDialog類裡面讓動畫啟動了
ImageView imageView = (ImageView) progressDialog.findViewById(R.id.loadImg); AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground(); animationDrawable.start();
完成這些,然後就可以在Activity裡面調用我們的MyProgressDialog了。
為了方便,給大家附上我的圖片資源,希望能用得上
- 上一篇Android自訂progressDialog使用系統ProgressBar與圖片實現(一)系統ProgressBar下一篇Android PopupWindow與ListView配合使用