Android 自定义Progress Bar

Android 提供ProgressDialog,可以提示进度,但你也可以自己实现,示例如下:

 


<?xml version="1.0" encoding="utf-8"?>  
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"  
    android:layout_gravity="center_vertical" android:layout_width="fill_parent"  
    android:layout_height="wrap_content">  
    <ProgressBar android:layout_width="wrap_content"  
        android:layout_height="wrap_content" android:id="@+id/progressbar_default"  
        android:scaleType="fitCenter" android:layout_alignParentLeft="true"  
        android:layout_margin="5dip" />  
  
  
    <ProgressBar android:layout_width="fill_parent"  
        android:layout_height="wrap_content" style="?android:attr/progressBarStyleHorizontal"  
        android:id="@+id/progressbar_Horizontal" android:max="100"  
        android:layout_toRightOf="@id/progressbar_default"  
        android:layout_margin="5dip" />  
    <TextView android:layout_width="fill_parent"  
        android:layout_height="wrap_content" android:id="@+id/progressbar_text"  
        android:layout_toRightOf="@id/progressbar_default" android:paddingTop="25dip"  
        android:layout_margin="5dip" android:text="init text"/>  
</RelativeLayout>  

public class ClearProgressDialog extends AlertDialog implements  
        DialogInterface.OnClickListener {  
  
    private ProgressBar progressBar;  
    private int progress = 0;  
  
    protected ClearProgressDialog(Context context) {  
        super(context);  
  
        init();  
    }  
  
    public ClearProgressDialog(Context context, boolean cancelable,  
            OnCancelListener cancelListener) {  
        super(context, cancelable, cancelListener);  
        init();  
    }  
  
    public ClearProgressDialog(Context context, int theme) {  
        super(context, theme);  
        init();  
    }  
  
    public void init() {  
        View view = getLayoutInflater().inflate(R.layout.progresslayout,  
                null);  
        progressBar = (ProgressBar) view.findViewById(R.id.progressbar_Horizontal);  
        setButton(getContext().getText(R.string.stop), this);  
        setIcon(R.drawable.andclear);  
        setTitle(R.string.clear_progress);  
        setView(view);  
        //progressBar = (ProgressBar) findViewById(R.id.progressbar_Horizontal);  
  
    }  
  
    public void onClick(DialogInterface dialog, int which) {  
  
    }  
  
    Handler pHandle = new Handler() {  
        public void handleMessage(Message msg) {  
  
        }  
    };  
  
    public void setProgress(int per, String str) {  
  
    }  
  
    public void stop() {  
  
    }  
  
}  


重写android.app.ProgressDialog实现自定义进度条弹出框布局

1.简介系统ProgressDialog的主要特征
1.在ProgressDialog的源码里可以明显的看到,在STYLE_HORIZONTAL和STYLE_SPINNER分别显示的是不同的XML,这就意味着你的进度条要么是转圈,要么是条形的。
2.不管是上述的任何情况下,系统对各部分文字显示都已经完全格式化。

2.实际情况
但是实际的应用中,我们或者需要改变文字的位置,或者需要转圈和条形共存,甚至是做出完全颠覆系统进度条的个性进度条,这个时候我们必须去重新设计属于你自己的进度条。(个人一直认为应用中的组件尽量不用系统的,而是重写系统的,这样做出来的应用才是百家争鸣)。
下面就实现我自己的进度条中碰到的几个可能需要注意的地方给大家交待下:
1.在系统ProgressDialog的构造函数

  1. public ProgressDialog(Context 
  2. context) 
  3. <P p 
  4. {

  5. this(context, 
  6. com.android.internal.R.style.Theme_Dialog_Alert);

  7. }
复制代码
中涉及了一个theme:com.android.internal.R.style.Theme_Dialog_Alert,这是我当时遇到的第一个问题,开始的时候翻遍源码,终于在data/res/values/themes.xml里找到,
  1. <style name="Theme.Dialog.Alert"><item name="windowBackground">@android:color/transparent</item> 

  2. <item name="windowTitleStyle">@android:style/DialogWindowTitle</item> 

  3. <item name="windowIsFloating">true</item> 

  4. <item name="windowContentOverlay">@null</item> 

  5. </style>
复制代码
但是发现他还关联其他style,继续找下去,结果写到自己的XML里还是错误一大堆,最后仔细看了下,发现不就是个theme吗,这就简单了,有2种方向:1.自己写theme.2.使用系统的theme。我写的时候是
  1. public ProgressDialog1(Context context) {
  2.        this(context, android.R.style.Theme_Panel);
  3.         mContext = context;
  4.         }
复制代码
调用系统的android.R.style.Theme_Panel.
注意:找个地方就是你个性释放的开始。
2.我要实现的是转圈和条形并存。那么肯定得在布局文件上下手了。
找个地方分2块说.第1,布局是XML文件;2,布局是代码生成。
您可能会问,这有区别吗?事实上,区别还是蛮大的,不知道你注意到没有如下属性
style="?android:attr/progressBarStyleHorizontal"
试问,如何代码实现?
先说第1种,XML的话比较简单,因为只需要写2个ProgressBar,然后再在代码里控制visible属性就ok,在此不赘述。
第2种,style的实现,这是我碰到的第2个难点
最后我在网上找到1篇文章,关于获取父类私有属性的文章,利用反射机制实现了style的设置。
以下工具类是转载网上那位朋友的工具类,大家可以借鉴下!
  1. public class BeanUtils {
  2. private BeanUtils() {
  3. }
  4. public static void setFieldValue(final Object object,
  5.    final String fieldName, final Object value) {
  6.   Field field = getDeclaredField(object, fieldName);
  7.   if (field == null)
  8.    throw new IllegalArgumentException("Could not find field ["
  9.      + fieldName + "] on target [" + object + "]");
  10.   makeAccessible(field);
  11.   try {
  12.    field.set(object, value);
  13.   } catch (IllegalAccessException e) {
  14.   }
  15. }
  16. protected static Field getDeclaredField(final Object object,
  17.    final String fieldName) {
  18.   return getDeclaredField(object.getClass(), fieldName);
  19. }
  20. protected static Field getDeclaredField(final Class clazz,
  21.    final String fieldName) {
  22.   for (Class superClass = clazz; superClass != Object.class; superClass = superClass
  23.     .getSuperclass()) {
  24.    try {
  25.     return superClass.getDeclaredField(fieldName);
  26.    } catch (NoSuchFieldException e) {
  27.    }
  28.   }
  29.   return null;
  30. }
  31. protected static void makeAccessible(Field field) {
  32.   if (!Modifier.isPublic(field.getModifiers())
  33.     || !Modifier.isPublic(field.getDeclaringClass().getModifiers())) {
  34.    field.setAccessible(true);
  35.   }
  36. }
  37. }
复制代码
有了上面的工具类,就可以简单的设置那些私有属性
比如:        
BeanUtils.setFieldValue(progress_h, "mOnlyIndeterminate", new Boolean(false));
        BeanUtils.setFieldValue(progress_h, "mMinHeight", new Integer(15));


以上就是我重写进度条的全部心得,希望能对阅读完得朋友有些许帮助!

最后附上我的demo,里面我的调用的布局是代码实现的,当然也有XML的。

demo说明:功能是前30条形,30-70转圈,70-100条形  文字跟着变

源码:http://files.cnblogs.com/shanzei/TestProgress.rar

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值