Android中自定义的加载对话框和加载条

来源:互联网 发布:编程对电脑配置的要求 编辑:程序博客网 时间:2024/05/18 00:39

先分享一个常用的转动形式加载对话框。


这个是很早前一个应用,一哥们写的控件。

后来发现联想的应用中基本所用应用加载框都是这个。(开源代码没版权一说吧)

控件比较简单,分享下思路:

1.首先这是一个自定义的dialog,重写了dialog,系统的progressdialog也是继承了dialog。

[java] view plaincopyprint?
  1. /**
  2. * @author Nono
  3. *
  4. */ 
  5. public class CustomProgressBarDialogextends Dialog { 
  6.     private LayoutInflater inflater; 
  7.     private Context mContext; 
  8.     private LayoutParams lp; 
  9.  
  10.     /**
  11.      * @param context
  12.      */ 
  13.     public CustomProgressBarDialog(Context context) { 
  14.         super(context, R.style.NoTitleDialog); 
  15.         this.mContext = context; 
  16.         inflater = (LayoutInflater) mContext 
  17.                 .getSystemService(Context.LAYOUT_INFLATER_SERVICE); 
  18.         layout = inflater.inflate(R.layout.custom_progressbar, null); 
  19.         setContentView(layout); 
  20.         // 设置window属性 
  21.         lp = getWindow().getAttributes(); 
  22.         lp.gravity = Gravity.CENTER; 
  23.         lp.dimAmount = 0;// 去背景遮盖 
  24.         lp.alpha = 1.0f; 
  25.         getWindow().setAttributes(lp); 
  26.  
  27.     } 
  28.  

2.主要是setContentView(view)中的这个view该如何定义。

[java] view plaincopyprint?
  1. <?xml version="1.0" encoding="utf-8"?> 
  2. <LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
  3.     android:orientation="horizontal" android:layout_width="fill_parent" 
  4.     android:layout_height="wrap_content" android:gravity="center"
  5.     <ProgressBar xmlns:android="http://schemas.android.com/apk/res/android" 
  6.         android:layout_width="wrap_content" android:layout_height="wrap_content" 
  7.         style="@style/CustomProgessBarStyle" android:padding="10dip" 
  8.         android:layout_gravity="center" android:gravity="center" 
  9.          /> 
  10.     <TextView android:id="@+id/load_info_text" android:text="@string/loading" android:layout_width="wrap_content" 
  11.         android:layout_height="wrap_content" android:textColor="#FFFFFF" 
  12.         android:padding="10dip" /> 
  13. </LinearLayout> 

3.上面的核心代码就是那个style,下面我们看下这个style代码;

[html] view plaincopyprint?
  1. <stylename="CustomProgessBarStyle"> 
  2.         <itemname="android:indeterminateDrawable">@drawable/custom_progress_bar</item> 
  3.         <itemname="android:minWidth">50dip</item> 
  4.         <itemname="android:maxWidth">50dip</item> 
  5.         <itemname="android:minHeight">50dip</item> 
  6.         <itemname="android:maxHeight">50dip</item> 
  7.     </style> 

4.我们看第一个item,也就是不稳定的图片,也是关键代码。自定义的drawable,我们知道res下的drawable文件中可以定义多种样式的drawable资源文件,

比如常用的动画放大缩小平移资源,
还有设计按钮中常用的 selector选择器,以及我们现在用到的 animation-list。

[html] view plaincopyprint?
  1. <animation-listxmlns:android="http://schemas.android.com/apk/res/android"   
  2.        android:oneshot="false"android:visible="true"> 
  3.        
  4.      <itemandroid:drawable="@drawable/flower1"android:duration="500"/>  
  5.      <itemandroid:drawable="@drawable/flower2"android:duration="500"/>  
  6.      <itemandroid:drawable="@drawable/flower3"android:duration="500"/>  
  7.      <itemandroid:drawable="@drawable/flower4"android:duration="500"/>  
  8. </animation-list> 

四张图片,间隔时间为500毫秒。于是小花就转动起来了。


以上即是小花图ui设计思路。

下面在提下关于logo界面进入时,有时我们也会用到的一个加载动画效果:

比如:



这个纯粹是是一个ProgressBar,然后自定义了一下样式而已:

[html] view plaincopyprint?
  1. <LinearLayoutxmlns:android="http://schemas.android.com/apk/res/android" 
  2.     android:orientation="vertical"android:layout_width="fill_parent" 
  3.     android:background="@drawable/lottery_main_bg"android:gravity="center" 
  4.     android:layout_height="fill_parent"> 
  5.     <ProgressBarandroid:indeterminateDrawable="@drawable/custom_progress_bar" 
  6.         android:layout_marginTop="30dip"android:indeterminate="false" 
  7.         android:id="@+id/CustomprogressBar"android:layout_width="225dip" 
  8.         android:layout_height="15dip"/> 
  9. </LinearLayout> 

然后是drawable文件:

[html] view plaincopyprint?
  1. <animation-listxmlns:android="http://schemas.android.com/apk/res/android"   
  2.        android:oneshot="false"android:visible="true"> 
  3.      <itemandroid:drawable="@drawable/load01"android:duration="200"/>  
  4.      <itemandroid:drawable="@drawable/load02"android:duration="200"/>  
  5.      <itemandroid:drawable="@drawable/load03"android:duration="200"/>  
  6.      <itemandroid:drawable="@drawable/load04"android:duration="200"/>  
  7.       <itemandroid:drawable="@drawable/load05"android:duration="200"/>  
  8.      <itemandroid:drawable="@drawable/load06"android:duration="200"/>  
  9. </animation-list> 

其实总觉得这样挺浪费资源的,一个加载条就要这么多图片资源。

以前也看到过说直接用一张图,然后类似于游戏中地图那样的切割法,在代码中动态剪彩和绘制。

有兴趣的可以尝试下。

原创粉丝点击