自定义progressDialog~~~

来源:互联网 发布:excel如何将拆分数据 编辑:程序博客网 时间:2024/06/05 19:29

在等待某些请求时,如果什么都不做,会很枯燥,但是progressDialog的显示又比较丑,很多情况下都是使用自定义的效果显示,这里给大家转载一个方法。供大家学习。原文地址http://blog.csdn.net/lovewaterman/article/details/40079549


效果的话懒得截图 简单的说就是全屏显示一个圆形的进度条,当然这个可以根据自己的需求更换动画,例如百度贴吧那种小人快跑的效果。

代码如下

  1. 1.String.xml 文件,progressDialog是继承与Dialog,先设置一下progressDialog的风格,设置背景透明色。  
  2. <style name="CustomDialog" parent="@android:style/Theme.Dialog">    
  3.     <item name="android:windowFrame">@null</item>    
  4.         <item name="android:windowIsFloating">true</item>    
  5.         <item name="android:windowContentOverlay">@null</item>    
  6.         <item name="android:windowAnimationStyle">@android:style/Animation.Dialog</item>    
  7.         <item name="android:windowSoftInputMode">stateUnspecified|adjustPan</item>    
  8.     </style>    
  9.         
  10.     <style name="CustomProgressDialog" parent="@style/CustomDialog">    
  11.         <item name="android:windowBackground">@android:color/transparent</item>    
  12.         <item name="android:windowNoTitle">true</item>    
  13.     </style>    
  14.   
  15. 2.customprogressdialog.xml文件,定义自己的布局,由于我的需求只需要一个进度条以及一串显示的内容,所以布局比较接单  
  16. <?xml version="1.0" encoding="utf-8"?>    
  17. <LinearLayout    
  18.   xmlns:android="http://schemas.android.com/apk/res/android"    
  19.   android:layout_width="fill_parent"    
  20.   android:layout_height="fill_parent"    
  21.   android:orientation="horizontal">    
  22.     <ImageView    
  23.         android:id="@+id/loadingImageView"    
  24.         android:layout_width="wrap_content"    
  25.         android:layout_height="wrap_content"    
  26.         android:background="@anim/progress_round"/>    
  27.     <TextView    
  28.         android:id="@+id/id_tv_loadingmsg"    
  29.         android:layout_width="wrap_content"    
  30.         android:layout_height="wrap_content"    
  31.         android:layout_gravity="center_vertical"    
  32.         android:textSize="20dp"/>    
  33. </LinearLayout>    
  34.   
  35. 3.progress_round.xml文件.这个文件为了实现转动的效果,循环显示这些图片。  
  36. <?xml version="1.0" encoding="utf-8"?>    
  37. <animation-list    
  38.     xmlns:android="http://schemas.android.com/apk/res/android"    
  39.     android:oneshot="false">    
  40.     <item android:drawable="@drawable/progress_1" android:duration="200"/>    
  41.     <item android:drawable="@drawable/progress_2" android:duration="200"/>    
  42.     <item android:drawable="@drawable/progress_3" android:duration="200"/>    
  43.     <item android:drawable="@drawable/progress_4" android:duration="200"/>    
  44.     <item android:drawable="@drawable/progress_5" android:duration="200"/>    
  45.     <item android:drawable="@drawable/progress_6" android:duration="200"/>    
  46.     <item android:drawable="@drawable/progress_7" android:duration="200"/>    
  47.     <item android:drawable="@drawable/progress_8" android:duration="200"/>    
  48. </animation-list>    
  49.   


然后就是 customprogressdialog的类 这里使用单例模式只需在使用时引用

package com.example.TrueLife.util;

import android.app.Dialog;
import android.content.Context;
import android.graphics.drawable.AnimationDrawable;
import android.view.Gravity;
import android.widget.ImageView;
import android.widget.TextView;
import com.example.TrueLife.R;

/**
* Created by Administrator on 2015/8/11.
*/
public class CustomProgressDialog extends Dialog{
private Context context;
private static CustomProgressDialog customProgressDialog = null;
public CustomProgressDialog(Context context){
super(context);
this.context = context;

}
public CustomProgressDialog(Context context, int theme) {
super(context, theme);
}

public static CustomProgressDialog createDialog(Context context){
customProgressDialog = new CustomProgressDialog(context, R.style.CustomProgressDialog);
customProgressDialog.setContentView(R.layout.customprogressdialog);
customProgressDialog.getWindow().getAttributes().gravity = Gravity.CENTER;

return customProgressDialog;
}

public void onWindowFocusChanged(boolean hasFocus){

if (customProgressDialog == null){
return;
}

ImageView imageView = (ImageView) customProgressDialog.findViewById(R.id.start_img_loading);
AnimationDrawable animationDrawable = (AnimationDrawable) imageView.getBackground();
animationDrawable.start();
}

/**
*
* [Summary]
* setTitile 标题
* @param strTitle
* @return
*
*/
public CustomProgressDialog setTitile(String strTitle){
return customProgressDialog;
}

/**
*
* [Summary]
* setMessage 提示内容
* @param strMessage
* @return
*
*/
public CustomProgressDialog setMessage(String strMessage){
TextView tvMsg = (TextView)customProgressDialog.findViewById(R.id.start_tv_loading);

if (tvMsg != null){
tvMsg.setText(strMessage);
}

return customProgressDialog;
}


}


引用方法相信大家都会 如下:


显示

  customProgressDialog = CustomProgressDialog.createDialog(StartActivity.this);


                customProgressDialog.show();


取消显示

 customProgressDialog.dismiss();

0 0