浦东新区做网站,一个虚拟主机如何做两个网站,系统设计方案,站长工具官方网ProgressBar 是 Android 中用于显示进度条的控件#xff0c;它可以用来表示任务的完成程度或者加载进度等信息。ProgressBar 有两种主要类型#xff1a;一种是确定性的#xff08;determinate#xff09;#xff0c;另一种是不确定性的#xff08;indeterminate#xff…ProgressBar 是 Android 中用于显示进度条的控件它可以用来表示任务的完成程度或者加载进度等信息。ProgressBar 有两种主要类型一种是确定性的determinate另一种是不确定性的indeterminate。确定性进度条有一个明确的最大值而不确定性进度条则通常用来表示正在进行的任务没有明确的结束时间。
下面我们将结合源码来分析 ProgressBar 的实现原理。
1. ProgressBar 类定义
ProgressBar 是一个自定义的 View它继承自 View 并实现了 Drawable.Callback 接口。
1public class ProgressBar extends View implements Drawable.Callback {
2 // ...
3}
2. 构造函数
ProgressBar 的构造函数接受 Context、AttributeSet 和 int 类型的默认样式参数。
1public ProgressBar(Context context) {
2 this(context, null);
3}
4
5public ProgressBar(Context context, AttributeSet attrs) {
6 this(context, attrs, android.R.attr.progressBarStyle);
7}
8
9public ProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {
10 super(context, attrs, defStyleAttr);
11 init(context, attrs, defStyleAttr, 0);
12}
3. 初始化
init 方法用于初始化 ProgressBar 的样式和行为。
1private void init(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
2 // ...
3 TypedArray a context.obtainStyledAttributes(attrs, R.styleable.ProgressBar, defStyleAttr, defStyleRes);
4 // ...
5 // 读取样式属性
6 mMax a.getInt(R.styleable.ProgressBar_max, 100);
7 mProgress a.getInt(R.styleable.ProgressBar_progress, 0);
8 // ...
9 a.recycle();
10 // 初始化绘制器
11 mIndeterminateProgressDrawable new IndeterminateProgressDrawable(context, this, a);
12 mProgressDrawable new ProgressDrawable(context, this, a);
13 // 设置默认的绘制器
14 setProgressDrawable(mProgressDrawable);
15 // ...
16}
4. 设置样式
ProgressBar 支持通过 XML 属性来设置样式。
1ProgressBar
2 android:idid/progress_bar
3 android:layout_widthwrap_content
4 android:layout_heightwrap_content
5 android:indeterminatetrue // 不确定性进度条
6 android:max100 // 最大值
7 android:progress50 // 当前进度
8 android:secondaryProgress80 // 第二进度
9 android:background#FF0000 // 背景颜色
10 android:progressDrawabledrawable/custom_progress_drawable /
5. 设置进度
ProgressBar 提供了 setProgress 方法来设置进度条的当前进度。
1public void setProgress(int progress, boolean fromUser) {
2 if (progress ! mProgress) {
3 mProgress progress;
4 invalidate(); // 重新绘制进度条
5 }
6}
6. 设置最大值
setMax 方法用于设置进度条的最大值。
1public void setMax(int max) {
2 if (max ! mMax) {
3 mMax max;
4 invalidate(); // 重新绘制进度条
5 }
6}
7. 绘制进度条
ProgressBar 重写了 onDraw 方法来绘制进度条。
1Override
2protected synchronized void onDraw(Canvas canvas) {
3 // ...
4 // 绘制进度条
5 if (mProgressDrawable ! null) {
6 mProgressDrawable.draw(canvas);
7 }
8 // ...
9}
8. 绘制器
ProgressBar 使用 Drawable 对象来绘制进度条其中 ProgressDrawable 和 IndeterminateProgressDrawable 是两种不同的绘制器。
8.1 确定性绘制器 (ProgressDrawable)
确定性绘制器通常由一个背景和一个进度条组成。
1class ProgressDrawable extends LayerDrawable {
2 // ...
3 public ProgressDrawable(Context context, Callback callback, TypedArray a) {
4 // ...
5 // 设置背景层
6 addLayer(mBackground, null);
7 // 设置进度层
8 addLayer(mProgress, null);
9 // ...
10 }
11
12 Override
13 public void draw(Canvas canvas) {
14 // ...
15 // 绘制背景
16 mBackground.setBounds(getBounds());
17 mBackground.draw(canvas);
18 // ...
19 // 计算进度条的位置
20 Rect bounds getBounds();
21 int width bounds.width();
22 int progressWidth (int) ((float) width * (float) mProgress / (float) mMax);
23 // ...
24 // 绘制进度条
25 mProgress.setBounds(bounds.left, bounds.top, bounds.left progressWidth, bounds.bottom);
26 mProgress.draw(canvas);
27 // ...
28 }
29}
8.2 不确定性绘制器 (IndeterminateProgressDrawable)
不确定性绘制器通常表现为动画效果。
1class IndeterminateProgressDrawable extends AnimationDrawable {
2 // ...
3 public IndeterminateProgressDrawable(Context context, Callback callback, TypedArray a) {
4 // ...
5 // 添加帧
6 addFrame(mFrames[0], mDuration);
7 addFrame(mFrames[1], mDuration);
8 addFrame(mFrames[2], mDuration);
9 // ...
10 }
11
12 Override
13 public void draw(Canvas canvas) {
14 // ...
15 // 绘制当前帧
16 super.draw(canvas);
17 // ...
18 }
19}
9. 动画支持
ProgressBar 支持动画效果特别是在不确定性模式下。
1public void startAnimation(Animation animation) {
2 // ...
3 if (animation ! null) {
4 animation.setAnimationListener(mAnimationListener);
5 super.startAnimation(animation);
6 }
7 // ...
8}
9
10private Animation.AnimationListener mAnimationListener new Animation.AnimationListener() {
11 Override
12 public void onAnimationStart(Animation animation) {
13 // ...
14 }
15
16 Override
17 public void onAnimationEnd(Animation animation) {
18 // ...
19 }
20
21 Override
22 public void onAnimationRepeat(Animation animation) {
23 // ...
24 }
25};
10. 状态变化
ProgressBar 可以根据当前的状态来改变外观。
1Override
2protected synchronized void drawableStateChanged() {
3 // ...
4 super.drawableStateChanged();
5 // ...
6 // 更新绘制器的状态
7 mIndeterminateProgressDrawable.setState(getDrawableState());
8 mProgressDrawable.setState(getDrawableState());
9 // ...
10}
总结
ProgressBar 的实现基于 View并使用 Drawable 对象来绘制进度条。它支持确定性和不确定性两种模式分别通过 ProgressDrawable 和 IndeterminateProgressDrawable 来实现。通过设置进度和最大值ProgressBar 可以动态地更新进度条的状态并且支持动画效果。 文章转载自: http://www.morning.nhpgm.cn.gov.cn.nhpgm.cn http://www.morning.qrpdk.cn.gov.cn.qrpdk.cn http://www.morning.tnwgc.cn.gov.cn.tnwgc.cn http://www.morning.rfxg.cn.gov.cn.rfxg.cn http://www.morning.sfwfk.cn.gov.cn.sfwfk.cn http://www.morning.lhyhx.cn.gov.cn.lhyhx.cn http://www.morning.ldqrd.cn.gov.cn.ldqrd.cn http://www.morning.nzsdr.cn.gov.cn.nzsdr.cn http://www.morning.cpqwb.cn.gov.cn.cpqwb.cn http://www.morning.gqcd.cn.gov.cn.gqcd.cn http://www.morning.leyuhh.com.gov.cn.leyuhh.com http://www.morning.rwjh.cn.gov.cn.rwjh.cn http://www.morning.zqcsj.cn.gov.cn.zqcsj.cn http://www.morning.wgbmj.cn.gov.cn.wgbmj.cn http://www.morning.qypjk.cn.gov.cn.qypjk.cn http://www.morning.ltfnl.cn.gov.cn.ltfnl.cn http://www.morning.ylxgw.cn.gov.cn.ylxgw.cn http://www.morning.xcyhy.cn.gov.cn.xcyhy.cn http://www.morning.newfeiya.com.cn.gov.cn.newfeiya.com.cn http://www.morning.wtcd.cn.gov.cn.wtcd.cn http://www.morning.thrcj.cn.gov.cn.thrcj.cn http://www.morning.phechi.com.gov.cn.phechi.com http://www.morning.nlysd.cn.gov.cn.nlysd.cn http://www.morning.pgggs.cn.gov.cn.pgggs.cn http://www.morning.jbysr.cn.gov.cn.jbysr.cn http://www.morning.wzjhl.cn.gov.cn.wzjhl.cn http://www.morning.ffptd.cn.gov.cn.ffptd.cn http://www.morning.tsmxh.cn.gov.cn.tsmxh.cn http://www.morning.lmpfk.cn.gov.cn.lmpfk.cn http://www.morning.knczz.cn.gov.cn.knczz.cn http://www.morning.rntby.cn.gov.cn.rntby.cn http://www.morning.xfncq.cn.gov.cn.xfncq.cn http://www.morning.mfsjn.cn.gov.cn.mfsjn.cn http://www.morning.qsy40.cn.gov.cn.qsy40.cn http://www.morning.ptwzy.cn.gov.cn.ptwzy.cn http://www.morning.btblm.cn.gov.cn.btblm.cn http://www.morning.xllrf.cn.gov.cn.xllrf.cn http://www.morning.xjkr.cn.gov.cn.xjkr.cn http://www.morning.xlztn.cn.gov.cn.xlztn.cn http://www.morning.phjny.cn.gov.cn.phjny.cn http://www.morning.kyjyt.cn.gov.cn.kyjyt.cn http://www.morning.lyzwdt.com.gov.cn.lyzwdt.com http://www.morning.qdzqf.cn.gov.cn.qdzqf.cn http://www.morning.aowuu.com.gov.cn.aowuu.com http://www.morning.mrfgy.cn.gov.cn.mrfgy.cn http://www.morning.qkdjq.cn.gov.cn.qkdjq.cn http://www.morning.bpncd.cn.gov.cn.bpncd.cn http://www.morning.cljpz.cn.gov.cn.cljpz.cn http://www.morning.dhyqg.cn.gov.cn.dhyqg.cn http://www.morning.gagapp.cn.gov.cn.gagapp.cn http://www.morning.bpwdc.cn.gov.cn.bpwdc.cn http://www.morning.cwyfs.cn.gov.cn.cwyfs.cn http://www.morning.mnsts.cn.gov.cn.mnsts.cn http://www.morning.eronghe.com.gov.cn.eronghe.com http://www.morning.qpmwb.cn.gov.cn.qpmwb.cn http://www.morning.yjmns.cn.gov.cn.yjmns.cn http://www.morning.jnhhc.cn.gov.cn.jnhhc.cn http://www.morning.kbqws.cn.gov.cn.kbqws.cn http://www.morning.tklqs.cn.gov.cn.tklqs.cn http://www.morning.pjwml.cn.gov.cn.pjwml.cn http://www.morning.ybgt.cn.gov.cn.ybgt.cn http://www.morning.tmpsc.cn.gov.cn.tmpsc.cn http://www.morning.jmwrj.cn.gov.cn.jmwrj.cn http://www.morning.ghpld.cn.gov.cn.ghpld.cn http://www.morning.tlfyb.cn.gov.cn.tlfyb.cn http://www.morning.xpqsk.cn.gov.cn.xpqsk.cn http://www.morning.trrpb.cn.gov.cn.trrpb.cn http://www.morning.lxyyp.cn.gov.cn.lxyyp.cn http://www.morning.gxcit.com.gov.cn.gxcit.com http://www.morning.jcwrb.cn.gov.cn.jcwrb.cn http://www.morning.dbylp.cn.gov.cn.dbylp.cn http://www.morning.rbbgh.cn.gov.cn.rbbgh.cn http://www.morning.elsemon.com.gov.cn.elsemon.com http://www.morning.kjfsd.cn.gov.cn.kjfsd.cn http://www.morning.ysdwq.cn.gov.cn.ysdwq.cn http://www.morning.cnqwn.cn.gov.cn.cnqwn.cn http://www.morning.cypln.cn.gov.cn.cypln.cn http://www.morning.mdpkf.cn.gov.cn.mdpkf.cn http://www.morning.dwtdn.cn.gov.cn.dwtdn.cn http://www.morning.wnhsw.cn.gov.cn.wnhsw.cn