简介

ProgressBar 继承自View,用于在界面上显示一个进度指示的界面。
1、ProgressBar有两个进度,一个是android:progress,另一个是android:secondaryProgress。后者主要是为缓存需要所涉及的,比如在看网络视频时候都会有一个缓存的进度条以及还要一个播放的进度,在这里缓存的进度就可以是android:secondaryProgress,而播放进度就是android:progress。

2、ProgressBar分为确定的和不确定的,确定的是我们能明确看到进度,相反不确定的就是不清楚、不确定一个操作需要多长时间来完成,这个时候就需要用不确定的ProgressBar了。
属性android:indeterminate如果设置为true的话,那么ProgressBar就可能是圆形的滚动条或者水平的滚动条(由样式决定),但是我们一般时候,是直接使用Style类型来区分圆形还是水平ProgressBar的。
progress_bar.setIndeterminate(true);  
设置为不明确(true)就是,滚动条的当前值自动在最小到最大值之间来回移动,形成这样一个动画效果,这个只是告诉别人“我正在工作”,但不能提示工作进度到哪个阶段。主要是在进行一些无法确定操作时间的任务时作为提示。而“明确”(false)就是根据你的进度可以设置现在的进度值。

3、ProgressBar的样式设定其实有两种方式,这两种方式效果是一样的。
style="@android:style/Widget.ProgressBar.Horizontal" 或 style="@android:attr/progressBarStyleHorizontal"
或为
style="?android:attr/progressBarStyleHorizontal"
代码中设置方法:
mProgressBar = new ProgressBar(this, null, android.R.attr.progressBarStyleHorizontal);

水平PB系统样式分析

水平ProgressBar系统样式的源码如下:
<style name="Widget.ProgressBar.Horizontal">
        <item name="indeterminateOnly">false</item>
        <item name="progressDrawable">@drawable/progress_horizontal</item>
        <item name="indeterminateDrawable">@drawable/progress_indeterminate_horizontal</item>
        <item name="minHeight">20dip</item>
        <item name="maxHeight">20dip</item>
        <item name="mirrorForRtl">true</item>  
</style>  
很明显progressDrawable用来设置绘制显示进度的进度条的Drawable对象
indeterminateDrawable用来设置绘制不显示进度的进度条的Drawable对象

继续看一下progress_horizontal的源码,如下:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/background">
        <shape>
            <corners android:radius="5dip" />
            <gradient
                android:angle="270"
                android:centerColor="#ff5a5d5a"
                android:centerY="0.75"
                android:endColor="#ff747674"
                android:startColor="#ff9d9e9d" />
        </shape>
    </item>
    <item android:id="@android:id/secondaryProgress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:angle="270"
                    android:centerColor="#80ffb600"
                    android:centerY="0.75"
                    android:endColor="#a0ffcb00"
                    android:startColor="#80ffd300" />
            </shape>
        </clip>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <corners android:radius="5dip" />
                <gradient
                    android:angle="270"
                    android:centerColor="#ffffb600"
                    android:centerY="0.75"
                    android:endColor="#ffffcb00"
                    android:startColor="#ffffd300" />
            </shape>
        </clip>
    </item>
</layer-list>  
这里面有3个item,分别为:background、secondProgress、progress,看名字就能知道其大概作用,其实把这个文件copy一份到自己的项目下,就可以随心所欲的修改圆角、渐变等shape属性。

如:
    <item
        android:id="@android:id/background"
        android:drawable="@drawable/progress_patch_green">
    </item>  
    <item android:id="@android:id/progress">
        <clip>
            <nine-patch android:src="@drawable/progress_patch_galy" />
        </clip>
    </item>  

注意
  • 三个元素不必都出现,比如secondaryProgress,如果不设置的话使用相关属性或方法不会生效。
  • clip标签是有意义的,不能去掉,否则后面的背景就被前面的覆盖掉了。
  • 这三个之间的叠放顺序不能乱,background是最底层(放在最上面),中间的是second,最上层是progress。

圆形PB系统样式分析

我们以progressBarStyleLarge为例进行探索,找到这个布局文件,的源码如下:
    <style name="Widget.ProgressBar.Large">
        <item name="indeterminateDrawable">@drawable/progress_large_white</item>
        <item name="minWidth">76dip</item>
        <item name="maxWidth">76dip</item>
        <item name="minHeight">76dip</item>
        <item name="maxHeight">76dip</item>
    </style>
同样一眼看出indeterminateDrawable便是主角了

继续看一下progress_large_white源码,如下:
<animated-rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/spinner_white_76"
    android:frameDuration="100"
    android:framesCount="12"
    android:pivotX="50%"
    android:pivotY="50%" />
其中android:framesCount="12" 应该是啥帧的count
android:frameDuration="100"应该是转圈持续的时间
上面的两个属性可能不能使用

下面我们在drawable文件夹中新建一个xml文件,更改其样式
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:drawable="@drawable/ic_launcher"
    android:fromDegrees="0"
    android:pivotX="50%"
    android:pivotY="50%"
    android:toDegrees="360" />

直接给ProgressBar设置我们覆盖的属性
    <ProgressBar
        style="@android:style/Widget.ProgressBar.Large"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:indeterminateDrawable="@drawable/pb" />
上面是通过一张图片填充android:indeterminateDrawable,我们也可以定义一个动画或者自定义shape来实现,跟图片的用法一样

演示代码


public class MainActivity extends Activity {
    ProgressBar pb, pb1, pb2;
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        pb = (ProgressBar) findViewById(R.id.pb);
        pb1 = (ProgressBar) findViewById(R.id.pb1);
        pb2 = (ProgressBar) findViewById(R.id.pb2);
        pb1.setIndeterminate(true);//滚动条的当前值自动在最小到最大值之间来回移动,即不显示具体的进度
        pb2.setProgress(pb.getProgress());
        pb2.setSecondaryProgress(pb.getSecondaryProgress());
        pb2.incrementProgressBy(-5);//进度值增加
        pb2.incrementSecondaryProgressBy(5);//第二个进度条进度值增加
    }
}

演示布局

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical" >
    <!-- 普通圆形ProgressBar 表示一个过程正在执行中,没有设置它的风格,那么它就是圆形的,一直会旋转的进度条 -->
    <ProgressBar
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <!-- 此时,给ProgressBar设置一个style风格属性后,该ProgressBar就有了一个风格,这里是大号ProgressBar的风格 -->
    <ProgressBar
        style="?android:attr/progressBarStyleLarge"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <!-- 小号圆形ProgressBar -->
    <ProgressBar
        style="?android:attr/progressBarStyleSmall"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />
    <!-- 标题型圆形ProgressBar -->
    <ProgressBar
        style="?android:attr/progressBarStyleSmallTitle"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content" />

    <!-- 系统默认的水平进度条 -->
    <ProgressBar
        android:id="@+id/pb"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="55" />
    <!-- 设置 indeterminate或indeterminateOnly属性为true则不显示具体的进度 -->
    <ProgressBar
        android:id="@+id/pb1"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="20dp"
        android:indeterminate="true"
        android:indeterminateOnly="true"
        android:max="100"
        android:progress="50"
        android:secondaryProgress="55" />
    <!-- 设置progressDrawable属性可设置背景 -->
    <ProgressBar
        android:id="@+id/pb2"
        style="?android:attr/progressBarStyleHorizontal"
        android:layout_width="match_parent"
        android:layout_height="3dp"
        android:progressDrawable="@drawable/progressbar_bg_red" />
</LinearLayout>

<layer-list xmlns:android="http://schemas.android.com/apk/res/android" >
    <item android:id="@android:id/background">
        <shape>
            <solid android:color="#dcdcdc" />
        </shape>
    </item>
    <item android:id="@android:id/progress">
        <clip>
            <shape>
                <solid android:color="#f00" />
            </shape>
        </clip>
    </item>
</layer-list>

ProgressBar 基本介绍的更多相关文章

  1. 写一个自己定义进度颜色和圆形转动的ProgressBar(具体介绍)

    先上图: 我们得自己定义ProgressBar的样式 <span style="white-space:pre"> </span><style nam ...

  2. ProgressBar、ProgessDialog用法解析

    一.ProgressBar 1. 常用类型 1.1 不确定式圆形进度条 style="@android:style/Widget.Holo.Light.ProgressBar" s ...

  3. 如何实现圆形的进度条(ProgressBar)

    在我们实际的工作中可能经常使用到圆形的进度条,但是这是怎么实现的呢?其实这只不过是修改了一下ProgressBar的模板,我们在下面的代码中我们将ProgressBar的Value值绑定到Border ...

  4. 重新想象 Windows 8 Store Apps 系列文章索引

    [源码下载][重新想象 Windows 8.1 Store Apps 系列文章] 重新想象 Windows 8 Store Apps 系列文章索引 作者:webabcd 1.重新想象 Windows ...

  5. Windows 8 Store Apps

    重新想象 Windows 8 Store Apps 系列文章索引 Posted on 2013-11-18 08:33 webabcd 阅读(672) 评论(3) 编辑 收藏 [源码下载] 重新想象 ...

  6. Android三种实现自定义ProgressBar的方式介绍

    一.通过动画实现 定义res/anim/loading.xml如下: View Row Code<?xml version="1.0" encoding="UTF- ...

  7. ExtJS 4.2 组件介绍

    目录 1. 介绍 1.1 说明 1.2 组件分类 1.3 组件名称 1.4 组件结构 2. 组件的创建方式 2.1 Ext.create()创建 2.2 xtype创建 1. 介绍 1.1 说明 Ex ...

  8. Android笔记——AsyncTask介绍

    AsyncTask和Handler对比 1 ) AsyncTask实现的原理,和适用的优缺点 AsyncTask,是android提供的轻量级的异步类,可以直接继承AsyncTask,在类中实现异步操 ...

  9. Android 中常见控件的介绍和使用

    1 TextView文本框 1.1 TextView类的结构 TextView 是用于显示字符串的组件,对于用户来说就是屏幕中一块用于显示文本的区域.TextView类的层次关系如下: java.la ...

随机推荐

  1. asp.net mvc 强类型视图中传入List 数据到控制器

    问题来源: 在和一位技术老师聊天时,老师问我一个mvc 表单提交的问题,问道:怎样在表单提交的时候,将 带有 List 属性的对象传入控制器? 这时,我有点呆了,以前一直都好像是 单一属性的表单提交, ...

  2. [walkthrough] 在Asp.net MVC6 RC里使用NLog,并且把配置集成到config.json

    说明一下:本文基于随visual studio 2015 RC公开的DNX1.0.0-beta4,git上最新的aspnet的开发版本已经发生了很大变化. 首先,理论部分看[汤姆大叔的博客] 解读AS ...

  3. test知识

    内部测试SIT ——system integration testcase 用户测试UAT——user acceptance test SIT是集成测试UAT是验收测试从时间上看,UAT要在SIT后面 ...

  4. windows C 与 linux C区别?

    windows C库格式为  .dll( 动态链接库英文为DLL,是Dynamic Link Library 的缩写形式,DLL是一个包含可由多个程序同时使用的代码和数据的库,DLL不是可执行文件). ...

  5. mysql的乱码问题

    ALTER TABLE `news`.`snapshots` MODIFY title VARCHAR() CHARACTER SET utf8; ALTER TABLE `news`.`snapsh ...

  6. myeclipse spket spket-1.6.23.jar 破解安装教程

    一年前安装文档就写过了,今天写破解文档,本来开发js/ext是想用aptana的,但是安装包100多M,我还是用spket吧(才11M),这个需要破解一下license,否则用不了. 一 安装教程如下 ...

  7. JQuery 判断IPad、IPhone、Android是横屏还是竖屏(Window.Orientation实现)

    在ipad.iphone网页开发中,我们很可能需要判断是横屏或者竖屏.下面就来介绍如何用 jQuery 判断iPad.iPhone.Android是横屏还是竖屏的方法. 代码如下: function ...

  8. iOS 16进制颜色和UIcolor的转换

    各种颜色之间的转换,会陆续更新, 实现了 16进制颜色(HEX).RGBA.HSBA.UIColor之间的  相互转换 使用示例(加号方法,类名调用) //UIColor 转 RGB.HSB RGBA ...

  9. 【号外号外:微软收购 .NET 的开源实现 Xamarin 项目的公司】

    [首页小编:你好,关于博客园对Xamarin的报道确实一笔而过了,希望能不要把这篇文章移除首页呵呵,祝福帅气,聪明,敏捷,睿智的小编] 一个月后,微软开始免费Xamarin了....还要放开SDK.. ...

  10. GitHub超详细图文攻略 - Git客户端下载安装 GitHub提交修改源码工作流程 Git分支 标签 过滤 Git版本工作流

    最近听同事说他都在使用GitHub,GitHub是程序员的社区,在里面可以学到很多书上学不到的东西,所以最近在准备入手这方面的知识去尝试学习,正好碰到这么详细完整的文章,就转载了,希望对自己和大家有帮 ...