标签:
1.dialog布局 dialog_loading.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 3 android:id="@+id/dialog_view" 4 android:layout_width="fill_parent" 5 android:layout_height="fill_parent" 6 android:background="@drawable/bg" 7 android:gravity="center" 8 android:minHeight="60dp" 9 android:minWidth="180dp" 10 android:orientation="vertical" 11 android:padding="10dp" > 12 13 <ImageView 14 android:id="@+id/img" 15 android:layout_width="wrap_content" 16 android:layout_height="wrap_content" 17 android:src="@drawable/base_loading_large_icon" /> 18 <!-- 19 <TextView 20 android:gravity="center" 21 android:id="@+id/tipTextView" 22 android:layout_width="wrap_content" 23 android:layout_height="wrap_content" 24 android:layout_marginLeft="10dp" /> 25 --> 26 </LinearLayout>
2.图像的旋转动画 loading_animation.xml
1 <?xml version="1.0" encoding="utf-8"?> 2 <set android:shareInterpolator="false" xmlns:android="http://schemas.android.com/apk/res/android"> 3 <rotate 4 android:interpolator="@android:anim/linear_interpolator" 5 android:pivotX="50%" 6 android:pivotY="50%" 7 android:fromDegrees="0" 8 android:toDegrees="+360" 9 android:duration="1500" 10 android:startOffset="-1" 11 android:repeatMode="restart" 12 android:repeatCount="-1"/> 13 </set>
3.dialog的style
1 <!-- 自定义loading dialog --> 2 <style name="loading_dialog" parent="android:style/Theme.Dialog"> 3 <item name="android:windowFrame">@null</item> 4 <item name="android:windowNoTitle">true</item> 5 <item name="android:windowIsFloating">true</item> 6 <item name="android:windowContentOverlay">@null</item> 7 </style>
4.dialog类
1 package com.jingle.progressdialog; 2 3 import android.annotation.SuppressLint; 4 import android.app.Dialog; 5 import android.content.Context; 6 import android.view.LayoutInflater; 7 import android.view.View; 8 import android.view.animation.Animation; 9 import android.view.animation.AnimationUtils; 10 import android.widget.ImageView; 11 import android.widget.LinearLayout; 12 13 @SuppressLint("InflateParams") 14 public class LoadingDialog { 15 16 private LoadingDialog() { 17 18 } 19 20 private static Dialog mLoadingDialog = null; 21 22 // 单例模式 23 public static Dialog getLoadingDialog(Context context, String msg) { 24 25 LayoutInflater inflater = LayoutInflater.from(context); 26 View v = inflater.inflate(R.layout.dialog_loading, null);// 得到加载对话框的view 27 LinearLayout layout = (LinearLayout) v.findViewById(R.id.dialog_view);// 加载对话框布局 28 29 ImageView spaceshipImage = (ImageView) v.findViewById(R.id.img);// 加载的图像 30 // TextView tipTextView = (TextView) v.findViewById(R.id.tipTextView);// 31 // 提示文字 32 // tipTextView.setText(msg);// 设置加载信息 33 // 加载动画 34 Animation hyperspaceJumpAnimation = AnimationUtils.loadAnimation( 35 context, R.anim.loading_animation); 36 // 使用ImageView显示动画 37 spaceshipImage.startAnimation(hyperspaceJumpAnimation); 38 39 if (mLoadingDialog == null) { 40 mLoadingDialog = new Dialog(context, R.style.loading_dialog0); 41 } 42 43 // Dialog loadingDialog = new Dialog(context, R.style.loading_dialog);// 44 // 创建自定义样式dialog 45 46 // loadingDialog.setCancelable(false);// 不可以用“返回键”取消 47 mLoadingDialog.setContentView(layout, new LinearLayout.LayoutParams( 48 LinearLayout.LayoutParams.MATCH_PARENT, 49 LinearLayout.LayoutParams.MATCH_PARENT));// 设置布局 50 return mLoadingDialog; 51 52 } 53 }
5.调用dialogMainActivity.java
1 package com.jingle.progressdialog; 2 3 import java.util.Timer; 4 import java.util.TimerTask; 5 6 import android.app.Activity; 7 import android.app.Dialog; 8 import android.os.Bundle; 9 10 public class MainActivity extends Activity { 11 private Dialog dialog = null; 12 13 @Override 14 protected void onCreate(Bundle savedInstanceState) { 15 super.onCreate(savedInstanceState); 16 setContentView(R.layout.activity_main); 17 18 initDialog(); 19 } 20 21 private void initDialog() { 22 dialog = LoadingDialog.getLoadingDialog(this, ""); 23 dialog.show(); 24 Timer timer = new Timer(); 25 timer.schedule(new Task(), 3000); 26 } 27 28 class Task extends TimerTask { 29 public void run() { 30 dialog.dismiss();//解散dialog 31 } 32 } 33 34 }
标签:
原文地址:http://www.cnblogs.com/jinglecode/p/4389116.html