赞
踩
android:max:进度条的最大值
android:progress:进度条已完成进度值
android:progressDrawable:设置轨道对应的Drawable对象
android:indeterminate:如果设置成true,则进度条不精确显示进度
android:indeterminateDrawable:设置不显示进度的进度条的Drawable对象
android:indeterminateDuration:设置不精确显示进度的持续时间
android:secondaryProgress:二级进度条,类似于视频播放的一条是当前播放进度,一条是缓冲进度,前者通过progress属性进行设置!
getMax():返回这个进度条的范围的上限
getProgress():返回进度
getSecondaryProgress():返回次要进度
incrementProgressBy(int diff):指定增加的进度
isIndeterminate():指示进度条是否在不确定模式下
setIndeterminate(boolean indeterminate):设置不确定模式下
系统系统了四种进度条:分别是Small,Inverse,Large,Horizontal,前三种是圆形,最后一种是横线形,直接style引用即可
三种圆形的进度条无法显示进度
<ProgressBar
style="@android:style/Widget.ProgressBar.Small"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerInParent="true" />
布局文件
<ProgressBar
android:id="@+id/prog"
style="@android:style/Widget.ProgressBar.Horizontal"
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:max="100"
android:progress="0" />
java文件
下面的代码中,首先做了一个定时器模拟事务返回进度,每秒钟返回一次当前事务的进度,第二个定时器搭配hander每秒钟更新一次UI上显示的进度条
hander里面:
第一步获取以下当前进度条显示的进度,
第二步通过事务返回的进度计算在进度条上应该显示的进度,
第三步用应该显示的最新进度减去当前已经显示的进度获取当前进度条应该增加的进度
第四步调用进度条的incrementProgressBy方法更新进度条显示
public class MainActivity extends AppCompatActivity { ProgressBar prog; int a; //事务执行的总进度 int b = 0; //事务执行的初始进度 int c; //进度条的总进度 int h; //进度条的当前进度 Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 111) { //获取进度条当前进度 h = prog.getProgress(); //计算当前实际进度 double e = (double) b / a; double f = c * e; int g = (int) f; //计算要增加的进度 int m = g - h; prog.incrementProgressBy(m); //增加进度显示 } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); prog = (ProgressBar) findViewById(R.id.prog); c = prog.getMax(); //执行事务,每秒钟返回一次事务执行进度 new Timer().schedule(new TimerTask() { @Override public void run() { a = 500; b++; if (b > a) { cancel(); } } }, 0, 1000); //每秒钟将事务返回的进度值通过hander更新到UI显示中 new Timer().schedule(new TimerTask() { @Override public void run() { if (b > a) { cancel(); }else{ //定时循环执行的内容 myHandler.sendEmptyMessage(111); } } }, 0, 1000); }
自定义一个类继承View绘制圆形进度条,然后引用,下面的代码可直接复制使用,代码中我暴露出了五个参数可以在使用时修改,java中调用先使用setmTargetProgress()方法修改进度值,然后调用invalidate()重新绘制view即可。
CustomPgBar.java
public class CustomPgBar extends View { private Paint mBackPaint; private Paint mFrontPaint; private Paint mTextPaint; private RectF mRect; private int mProgress = 0; private int mWidth; private int mHeight; private int mMax = 100; //总进度值 //以下五个参数已暴露出去,使用时可修改 private int mTargetProgress = 0; //进度值 private float mRadius = 200; //圆形大小 private int mTextPaintColor = Color.GREEN; //绘制文本字体颜色 private int mFrontPaintColor = Color.GREEN; //绘制进度颜色 private int mBackPaintColor = Color.BLUE; //绘制总进度颜色 private float pTextSize = (float) (mRadius * 0.6); //绘制文本字体大小 private float mStrokeWidth = (float) (mRadius * 0.25); //进度粗细 private float mHalfStrokeWidth = mStrokeWidth / 2; public CustomPgBar(Context context) { super(context); init(); } public CustomPgBar(Context context, AttributeSet attrs) { super(context, attrs); init(); } public CustomPgBar(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(); } //完成相关参数初始化 private void init() { //总进度画笔 mBackPaint = new Paint(); mBackPaint.setColor(getmBackPaintColor()); mBackPaint.setAntiAlias(true); mBackPaint.setStyle(Paint.Style.STROKE); mBackPaint.setStrokeWidth(mStrokeWidth); //进度画笔 mFrontPaint = new Paint(); mFrontPaint.setColor(getmFrontPaintColor()); mFrontPaint.setAntiAlias(true); mFrontPaint.setStyle(Paint.Style.STROKE); mFrontPaint.setStrokeWidth(mStrokeWidth); //文本画笔 mTextPaint = new Paint(); mTextPaint.setColor(getmTextPaintColor()); mTextPaint.setAntiAlias(true); mTextPaint.setTextSize(pTextSize); mTextPaint.setTextAlign(Paint.Align.CENTER); } //重写测量大小的onMeasure方法和绘制View的核心方法onDraw() @Override protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { super.onMeasure(widthMeasureSpec, heightMeasureSpec); mWidth = getRealSize(widthMeasureSpec); mHeight = getRealSize(heightMeasureSpec); setMeasuredDimension(mWidth, mHeight); } @Override protected void onDraw(Canvas canvas) { initRect(); float angle = mProgress / (float) mMax * 360; canvas.drawCircle(mWidth / 2, mHeight / 2, getmRadius(), mBackPaint); canvas.drawArc(mRect, -90, angle, false, mFrontPaint); canvas.drawText(mProgress + "%", mWidth / 2 + mHalfStrokeWidth, mHeight / 2 + mHalfStrokeWidth, mTextPaint); if (mProgress < getmTargetProgress()) { mProgress += 1; invalidate(); } } public int getRealSize(int measureSpec) { int result = 1; int mode = MeasureSpec.getMode(measureSpec); int size = MeasureSpec.getSize(measureSpec); if (mode == MeasureSpec.AT_MOST || mode == MeasureSpec.UNSPECIFIED) { //自己计算 result = (int) (getmRadius() * 2 + mStrokeWidth); } else { result = size; } return result; } private void initRect() { if (mRect == null) { mRect = new RectF(); int viewSize = (int) (getmRadius() * 2); int left = (mWidth - viewSize) / 2; int top = (mHeight - viewSize) / 2; int right = left + viewSize; int bottom = top + viewSize; mRect.set(left, top, right, bottom); } } public int getmTargetProgress() { return mTargetProgress; } public void setmTargetProgress(int mTargetProgress) { this.mTargetProgress = mTargetProgress; } public float getmRadius() { return mRadius; } public void setmRadius(float mRadius) { if (mRadius != 0) { this.mRadius = mRadius; } } public int getmTextPaintColor() { return mTextPaintColor; } public void setmTextPaintColor(int mTextPaintColor) { if (mTextPaintColor != 0) { this.mTextPaintColor = mTextPaintColor; } } public int getmFrontPaintColor() { return mFrontPaintColor; } public void setmFrontPaintColor(int mFrontPaintColor) { if (mFrontPaintColor != 0) { this.mFrontPaintColor = mFrontPaintColor; } } public int getmBackPaintColor() { return mBackPaintColor; } public void setmBackPaintColor(int mBackPaintColor) { if (mBackPaintColor != 0) { this.mBackPaintColor = mBackPaintColor; } } public int getmMax() { return mMax; } }
activity_main.xml引用
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_weight="1">
<com.example.progressbardemo.CustomPgBar
android:id="@+id/cusbar"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</RelativeLayout>
MainActivity.java
public class MainActivity extends AppCompatActivity { CustomPgBar cusbar; int a; //事务执行的总进度 int b = 0; //事务执行的初始进度 int c; //进度条的总进度 Handler myHandler = new Handler() { @Override public void handleMessage(Message msg) { if (msg.what == 111) { //计算当前实际进度 double e = (double) b / a; double f = c * e; int g = (int) f; cusbar.setmTargetProgress(g); //修改进度值 cusbar.invalidate(); //重新绘制viwe } } }; @Override protected void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.activity_main); cusbar = (CustomPgBar) findViewById(R.id.cusbar); c = cusbar.getmMax(); //执行事务,每秒钟返回一次事务执行进度 new Timer().schedule(new TimerTask() { @Override public void run() { a = 200; b++; if (b >= a) { cancel(); } } }, 0, 1000); //每秒钟将事务返回的进度值通过hander更新到UI显示中 new Timer().schedule(new TimerTask() { @Override public void run() { if (b > a) { cancel(); } else { //定时循环执行的内容 myHandler.sendEmptyMessage(111); } } }, 0, 1000); } }
以上就是今天要讲的内容。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。