Android 自定义ProgressBar显示百分比(1)

这篇文章详细描述了一个自定义的AndroidProgressBar组件,包括颜色、尺寸、文本显示、绘制区域的计算以及属性设置。它解释了如何根据进度和最大值动态调整视图,并提供了文本颜色和大小的可配置选项。
摘要由CSDN通过智能技术生成

*/

private float mReachedBarHeight;

/**

  • The height of the unreached area.

*/

private float mUnreachedBarHeight;

/**

  • The suffix of the number.

*/

private String mSuffix = “%”;

/**

  • The prefix.

*/

private String mPrefix = “”;

private final int default_text_color = Color.rgb(66, 145, 241);

private final int default_reached_color = Color.rgb(66, 145, 241);

private final int default_unreached_color = Color.rgb(204, 204, 204);

private final float default_progress_text_offset;

private final float default_text_size;

private final float default_reached_bar_height;

private final float default_unreached_bar_height;

/**

  • For save and restore instance of progressbar.

*/

private static final String INSTANCE_STATE = “saved_instance”;

private static final String INSTANCE_TEXT_COLOR = “text_color”;

private static final String INSTANCE_TEXT_SIZE = “text_size”;

private static final String INSTANCE_REACHED_BAR_HEIGHT = “reached_bar_height”;

private static final String INSTANCE_REACHED_BAR_COLOR = “reached_bar_color”;

private static final String INSTANCE_UNREACHED_BAR_HEIGHT = “unreached_bar_height”;

private static final String INSTANCE_UNREACHED_BAR_COLOR = “unreached_bar_color”;

private static final String INSTANCE_MAX = “max”;

private static final String INSTANCE_PROGRESS = “progress”;

private static final String INSTANCE_SUFFIX = “suffix”;

private static final String INSTANCE_PREFIX = “prefix”;

private static final String INSTANCE_TEXT_VISIBILITY = “text_visibility”;

private static final int PROGRESS_TEXT_VISIBLE = 0;

/**

  • The width of the text that to be drawn.

*/

private float mDrawTextWidth;

/**

  • The drawn text start.

*/

private float mDrawTextStart;

/**

  • The drawn text end.

*/

private float mDrawTextEnd;

/**

  • The text that to be drawn in onDraw().

*/

private String mCurrentDrawText;

/**

  • The Paint of the reached area.

*/

private Paint mReachedBarPaint;

/**

  • The Paint of the unreached area.

*/

private Paint mUnreachedBarPaint;

/**

  • The Paint of the progress text.

*/

private Paint mTextPaint;

/**

  • Unreached bar area to draw rect.

*/

private RectF mUnreachedRectF = new RectF(0, 0, 0, 0);

/**

  • Reached bar area rect.

*/

private RectF mReachedRectF = new RectF(0, 0, 0, 0);

/**

  • The progress text offset.

*/

private float mOffset;

/**

  • Determine if need to draw unreached area.

*/

private boolean mDrawUnreachedBar = true;

private boolean mDrawReachedBar = true;

private boolean mIfDrawText = true;

/**

  • Listener

*/

private OnProgressBarListener mListener;

public enum ProgressTextVisibility {

Visible, Invisible

}

public ProgressBarWithPercent(Context context) {

this(context, null);

}

public ProgressBarWithPercent(Context context, AttributeSet attrs) {

this(context, attrs, 0);

}

public ProgressBarWithPercent(Context context, AttributeSet attrs,

int defStyleAttr) {

super(context, attrs, defStyleAttr);

default_reached_bar_height = dp2px(1.5f);

default_unreached_bar_height = dp2px(1.0f);

default_text_size = sp2px(10);

default_progress_text_offset = dp2px(3.0f);

// load styled attributes.

final TypedArray attributes = context.getTheme()

.obtainStyledAttributes(attrs,

R.styleable.ProgressBarWithPercent, defStyleAttr, 0);

mReachedBarColor = attributes.getColor(

R.styleable.ProgressBarWithPercent_progress_reached_color,

default_reached_color);

mUnreachedBarColor = attributes.getColor(

R.styleable.ProgressBarWithPercent_progress_unreached_color,

default_unreached_color);

mTextColor = attributes.getColor(

R.styleable.ProgressBarWithPercent_progress_text_color,

default_text_color);

mTextSize = attributes.getDimension(

R.styleable.ProgressBarWithPercent_progress_text_size,

default_text_size);

mReachedBarHeight = attributes.getDimension(

R.styleable.ProgressBarWithPercent_progress_reached_bar_height,

default_reached_bar_height);

mUnreachedBarHeight = attributes

.getDimension(

R.styleable.ProgressBarWithPercent_progress_unreached_bar_height,

default_unreached_bar_height);

mOffset = attributes.getDimension(

R.styleable.ProgressBarWithPercent_progress_text_offset,

default_progress_text_offset);

int textVisible = attributes.getInt(

R.styleable.ProgressBarWithPercent_progress_text_visibility,

PROGRESS_TEXT_VISIBLE);

if (textVisible != PROGRESS_TEXT_VISIBLE) {

mIfDrawText = false;

}

setProgress(attributes.getInt(

R.styleable.ProgressBarWithPercent_progress_current, 0));

setMax(attributes.getInt(

R.styleable.ProgressBarWithPercent_progress_max, 100));

attributes.recycle();

initializePainters();

}

@Override

protected int getSuggestedMinimumWidth() {

return (int) mTextSize;

}

@Override

protected int getSuggestedMinimumHeight() {

return Math.max((int) mTextSize,

Math.max((int) mReachedBarHeight, (int) mUnreachedBarHeight));

}

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

setMeasuredDimension(measure(widthMeasureSpec, true),

measure(heightMeasureSpec, false));

}

private int measure(int measureSpec, boolean isWidth) {

int result;

int mode = MeasureSpec.getMode(measureSpec);

int size = MeasureSpec.getSize(measureSpec);

int padding = isWidth ? getPaddingLeft() + getPaddingRight()
getPaddingTop() + getPaddingBottom();

if (mode == MeasureSpec.EXACTLY) {

result = size;

} else {

result = isWidth ? getSuggestedMinimumWidth()
getSuggestedMinimumHeight();

result += padding;

if (mode == MeasureSpec.AT_MOST) {

if (isWidth) {

result = Math.max(result, size);

} else {

result = Math.min(result, size);

}

}

}

return result;

}

@Override

protected void onDraw(Canvas canvas) {

if (mIfDrawText) {

calculateDrawRectF();

} else {

calculateDrawRectFWithoutProgressText();

}

if (mDrawReachedBar) {

canvas.drawRect(mReachedRectF, mReachedBarPaint);

}

if (mDrawUnreachedBar) {

canvas.drawRect(mUnreachedRectF, mUnreachedBarPaint);

}

if (mIfDrawText)

canvas.drawText(mCurrentDrawText, mDrawTextStart, mDrawTextEnd,

mTextPaint);

}

private void initializePainters() {

mReachedBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

mReachedBarPaint.setColor(mReachedBarColor);

mUnreachedBarPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

mUnreachedBarPaint.setColor(mUnreachedBarColor);

mTextPaint = new Paint(Paint.ANTI_ALIAS_FLAG);

mTextPaint.setColor(mTextColor);

mTextPaint.setTextSize(mTextSize);

}

private void calculateDrawRectFWithoutProgressText() {

mReachedRectF.left = getPaddingLeft();

mReachedRectF.top = getHeight() / 2.0f - mReachedBarHeight / 2.0f;

mReachedRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight())

/ (getMax() * 1.0f) * getProgress() + getPaddingLeft();

mReachedRectF.bottom = getHeight() / 2.0f + mReachedBarHeight / 2.0f;

mUnreachedRectF.left = mReachedRectF.right;

mUnreachedRectF.right = getWidth() - getPaddingRight();

mUnreachedRectF.top = getHeight() / 2.0f + -mUnreachedBarHeight / 2.0f;

mUnreachedRectF.bottom = getHeight() / 2.0f + mUnreachedBarHeight

/ 2.0f;

}

private void calculateDrawRectF() {

mCurrentDrawText = String.format(“%d”, getProgress() * 100 / getMax());

mCurrentDrawText = mPrefix + mCurrentDrawText + mSuffix;

mDrawTextWidth = mTextPaint.measureText(mCurrentDrawText);

if (getProgress() == 0) {

mDrawReachedBar = false;

mDrawTextStart = getPaddingLeft();

} else {

mDrawReachedBar = true;

mReachedRectF.left = getPaddingLeft();

mReachedRectF.top = getHeight() / 2.0f - mReachedBarHeight / 2.0f;

mReachedRectF.right = (getWidth() - getPaddingLeft() - getPaddingRight())

/ (getMax() * 1.0f)

  • getProgress()
  • mOffset
  • getPaddingLeft();

mReachedRectF.bottom = getHeight() / 2.0f + mReachedBarHeight

/ 2.0f;

mDrawTextStart = (mReachedRectF.right + mOffset);

}

mDrawTextEnd = (int) ((getHeight() / 2.0f) - ((mTextPaint.descent() + mTextPaint

.ascent()) / 2.0f));

if ((mDrawTextStart + mDrawTextWidth) >= getWidth() - getPaddingRight()) {

mDrawTextStart = getWidth() - getPaddingRight() - mDrawTextWidth;

mReachedRectF.right = mDrawTextStart - mOffset;

}

float unreachedBarStart = mDrawTextStart + mDrawTextWidth + mOffset;

if (unreachedBarStart >= getWidth() - getPaddingRight()) {

mDrawUnreachedBar = false;

} else {

mDrawUnreachedBar = true;

mUnreachedRectF.left = unreachedBarStart;

mUnreachedRectF.right = getWidth() - getPaddingRight();

mUnreachedRectF.top = getHeight() / 2.0f + -mUnreachedBarHeight

/ 2.0f;

mUnreachedRectF.bottom = getHeight() / 2.0f + mUnreachedBarHeight

/ 2.0f;

}

}

/**

  • Get progress text color.

  • @return progress text color.

*/

public int getTextColor() {

return mTextColor;

}

/**

  • Get progress text size.

  • @return progress text size.

*/

public float getProgressTextSize() {

return mTextSize;

}

public int getUnreachedBarColor() {

return mUnreachedBarColor;

}

public int getReachedBarColor() {

return mReachedBarColor;

}

public int getProgress() {

return mCurrentProgress;

}

public int getMax() {

return mMaxProgress;

}

public float getReachedBarHeight() {

return mReachedBarHeight;

}

public float getUnreachedBarHeight() {

return mUnreachedBarHeight;

}

public void setProgressTextSize(float textSize) {

this.mTextSize = textSize;

mTextPaint.setTextSize(mTextSize);

invalidate();

}

public void setProgressTextColor(int textColor) {

this.mTextColor = textColor;

mTextPaint.setColor(mTextColor);

invalidate();

}

public void setUnreachedBarColor(int barColor) {

this.mUnreachedBarColor = barColor;

mUnreachedBarPaint.setColor(mUnreachedBarColor);

invalidate();

}

public void setReachedBarColor(int progressColor) {

this.mReachedBarColor = progressColor;

mReachedBarPaint.setColor(mReachedBarColor);

invalidate();

}

public void setReachedBarHeight(float height) {

mReachedBarHeight = height;

}

public void setUnreachedBarHeight(float height) {

mUnreachedBarHeight = height;

自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

学习分享

在当下这个信息共享的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了

很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘

如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。

2021最新上万页的大厂面试真题

七大模块学习资料:如NDK模块开发、Android框架体系架构…

只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。

这份体系学习笔记,适应人群:
**第一,**学习知识比较碎片化,没有合理的学习路线与进阶方向。
**第二,**开发几年,不知道如何进阶更进一步,比较迷茫。
**第三,**到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。

由于文章内容比较多,篇幅不允许,部分未展示内容以截图方式展示 。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!**

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

学习分享

在当下这个信息共享的时代,很多资源都可以在网络上找到,只取决于你愿不愿意找或是找的方法对不对了

很多朋友不是没有资料,大多都是有几十上百个G,但是杂乱无章,不知道怎么看从哪看起,甚至是看后就忘

如果大家觉得自己在网上找的资料非常杂乱、不成体系的话,我也分享一套给大家,比较系统,我平常自己也会经常研读。

2021最新上万页的大厂面试真题

[外链图片转存中…(img-BzfEC8BQ-1713196232530)]

七大模块学习资料:如NDK模块开发、Android框架体系架构…

[外链图片转存中…(img-rmfVT6Ev-1713196232530)]

只有系统,有方向的学习,才能在段时间内迅速提高自己的技术。

这份体系学习笔记,适应人群:
**第一,**学习知识比较碎片化,没有合理的学习路线与进阶方向。
**第二,**开发几年,不知道如何进阶更进一步,比较迷茫。
**第三,**到了合适的年纪,后续不知道该如何发展,转型管理,还是加强技术研究。

由于文章内容比较多,篇幅不允许,部分未展示内容以截图方式展示 。

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值