前言
这里介绍了ProgressBar以及它的子类的一些列的使用,主要介绍如下:
ProgressBar的介绍
主题介绍
(1)Indeterminate Progress
第一种就是模糊的progressbar,当你不知道一个操作需要多长时间。不确定的模式是默认的进度条,显示了一个循环动画没有特定数量的进度表示。
<ProgressBar
android:layout_width="wrap_content"
android:layout_height="match_parent"/>
效果图是一个不断转动的圆圈,这里只是一个截图:
(2)Widget.ProgressBar.Horizontal
给ProgressBar设置这个主题,这是确定的模式的进度条,当你想显示一个特定数量的进度。例如,检索文件的剩余百分比、数量记录在批写入数据库中,或一个音频文件的剩余百分比。这个可以认为是determinate ProgressBar 是确定的。
<ProgressBar
android:secondaryProgress="70"
android:progress="50"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
这里progress就是一级进度,android:secondaryProgress表示的二级进度,如图所示:
(3)Widget.ProgressBar.Small
这是一个小号版的ProgressBar.注意是相对于同一个width和height的Progressbar
<ProgressBar
android:secondaryProgress="70"
android:progress="50"
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
你会发现在这里,android:progress 和 android:secondaryProgress这两个属性是不管用的。
(4)Widget.ProgressBar.Large
可以认为是一个大号版的ProgressBar.注意是相对于同一个width和height的Progressbar
<ProgressBar
style="@android:style/Widget.ProgressBar.Large"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
具体运行效果图:
(5)Widget.ProgressBar.Inverse
在使用不确定进度的Progressbar时候,你会注意到Progressbar其实是逆时针旋转的,而这个属性就是让其顺时针旋转。
<ProgressBar
style="@android:style/Widget.ProgressBar.Inverse"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
(6)Widget.ProgressBar.Small.Inverse
看名字就可以知道,可以认为是缩小版的顺时针旋转的ProgressBar
(7)Widget.ProgressBar.Large.Inverse
可以认为是放大版的顺时针旋转的ProgressBar
XML attributes 介绍
其中还有属于View的属性,这里暂且不粘贴了
下面进行部分分开介绍:
android:animationResolution
超时动画帧之间的毫秒。需要的是整数,可以是100之类的。暂时未找到作用
android:indeterminate
不确定的模式。在这种模式下进度条可以无限循环动画。这个属性就是给进度条样式来使用的,在不确定模式下是不管用的。
<ProgressBar
android:indeterminate ="true"
android:progress="25"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
上面也设置了progress,发现进度设置不管用了,如图:
android:indeterminateBehavior
定义了不确定的模式应该如何达到最大值时的行为。该值必须为repeat或者cycle,repeat表示进度从0重新开始;cycle表示进度保持当前值,并且回到0。其实表现在ui上就是:如果是cycle,转动的进度条,转了两圈之后,默认是逆时针转过去,那么就会顺时针在转回来,如果是repeat,进度条转了两圈之后,会继续转下去,就是默认的ProgressBar的转动效果
<ProgressBar
android:indeterminateBehavior ="repeat"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
android:indeterminateDrawable
定义可以拉的ProgressBar(针对于不确定模式).十分重要的一个属性,主要是对于drawable的使用,下面举了几个例子:
(1)先在drawable文件下面建立如下所示:
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item>
<rotate
android:drawable="@drawable/ic_launcher"
android:fromDegrees="0.0"
android:pivotX="50.0%"
android:pivotY="50.0%"
android:toDegrees="360.0" />
<span style="white-space:pre"> </span> <!-- 其中360.0值越大,转的圈圈越快 -->
</item>
</layer-list>
在代码中使用:
<ProgressBar
android:indeterminateDrawable ="@drawable/myprogress"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
效果图如下:
这里截取的是静态图,这个头像就会转动起来
(2)在drawable文件下面建立如图所示文件
<?xml version="1.0" encoding="utf-8"?>
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
android:fromDegrees="0"
android:pivotX="50%"
android:pivotY="50%"
android:toDegrees="360" >
<shape
android:innerRadiusRatio="3"
android:shape="ring"
android:thicknessRatio="8"
android:useLevel="false" >
<gradient
android:centerColor="#FFFFFF"
android:centerY="0.50"
android:endColor="#b90e36"
android:startColor="#000000"
android:type="sweep"
android:useLevel="false" />
</shape>
</rotate>
在布局中使用:
<ProgressBar
android:indeterminateDrawable ="@drawable/mysprogreass"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
(3)如下在建立文件。
<layer-list xmlns:android="http://schemas.android.com/apk/res/android">
<item android:id="@android:id/background">
<shape>
<corners android:radius="25dip" />
<gradient
android:angle="0"
android:centerColor="#e6e6e6"
android:centerY="0.75"
android:endColor="#e6e6e6"
android:startColor="#e6e6e6" />
</shape>
</item>
<item android:id="@android:id/secondaryProgress">
<clip>
<shape>
<corners android:radius="25dip" />
<gradient
android:angle="0"
android:centerColor="#edf3"
android:centerY="0.75"
android:endColor="#edf3"
android:startColor="#edf3" />
</shape>
</clip>
</item>
</layer-list>
状态一:
状态二:
上面的效果其实是动起来的。
也就是说,通过这个属性,可以自定义ProgressBar的样式, 通过Drawable中的样式。
android:indeterminateDuration
时间不定的动画。针对于模糊的Progressbar,Duration时间,表现在UI上就是那个圆圈转动的快慢,你设置的时间越短,那么就会转动的越快。
android:indeterminateOnly
限制不定模式。如果是进度条样式,设置为true,就看不见进度了,像android:indeterminate一样,如果为false,就展示原来进度条,如果是一般样式,设置为true,则ProgrssBar变得不可见,设置为false ,则和原来一样。
android:indeterminateTint
色彩应用于不确定模式,那么必须要和不确定模式一起使用才有效果,就是设置颜色。
<ProgressBar
style="@android:style/Widget.ProgressBar.Horizontal"
android:indeterminate="true"
android:indeterminateTint ="#2424df"
android:id="@+id/progressbar_tsssest"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
但实际运行起来并没有效果
android:max
定义了最大值,如100
android:maxHeight
这是一个可选参数,定义最大高度
android:maxWidth
这是一个可选参数,定义最大宽度
android:min
定义最小值,如100
android:minHeight
这是一个可选参数,定义最小高度
android:minWidth
这是一个可选参数,定义最小宽度
android:mirrorForRtl
定义如果相关的画板时需要反映在RTL模式。默认是假的。
android:progress
设置进度,一级进度
android:secondaryProgress
设置二级进度
方法:
(1)构造方法
ContentLoadingProgressBar
android.support.v4.widget.ContentLoadingProgressBar,其直接父类是: android.widget.ProgressBar,
但是你直接在xml中使用没有效果,必须加上ProgressBar的主题效果:
<android.support.v4.widget.ContentLoadingProgressBar
android:progress="55"
android:id="@+id/contentLoadingProgressBar_q"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"/>
具体运行效果图:
所以它的效果可以当做ProgressBar来用,但是它还有自己的方法,下面只介绍它自己独特的方法:
XML Attributes
可以发现其实和ProgressBar的一样。还有一部分的xml属性,就是View的
自己的方法
(1)hide()
隐藏ProgressBar,如果它存在。打开方法:
通过图片可以看出,其实这个方法的本质就是View.setVisibility(View.GONE)
(2) show()
有隐藏的方法,那么就会有显示出来的方法:显示进度视图后等待至少延迟。
观察图片可以发现PostDelayed(mDelayedShow,MIN_DELAY),这个MIN_DELAY是500ms
调用这个效果可以实现的是延迟500ms后将视图展示出来。
(3)onAttachedToWindow()
这个回调表示当view依附在Window上面时候调用。
This is called when the view is attached to a window. At this point it has a Surface and will start drawing. Note that this function is guaranteed to be called before onDraw(android.graphics.Canvas), however it may be called any time before the first onDraw -- including before or after onMeasure(int, int).
(4)onDetachedFromWindow()
This is called when the view is detached from a window. At this point it no longer has a surface for drawing.
上述两个方法,感觉可以和Activity的生命周期有关,这里这样估计是为了提高效率,暂时不知道
(5)构造方法: