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;
}
public void setMax(int maxProgress) {
if (maxProgress > 0) {
this.mMaxProgress = maxProgress;
invalidate();
}
}
public void setSuffix(String suffix) {
if (suffix == null) {
mSuffix = “”;
} else {
mSuffix = suffix;
}
}
public String getSuffix() {
return mSuffix;
}
public void setPrefix(String prefix) {
if (prefix == null)
mPrefix = “”;
else {
mPrefix = prefix;
}
}
public String getPrefix() {
return mPrefix;
}
最后
一线互联网Android面试题含详解(初级到高级专题)
这些题目是今年群友去腾讯、百度、小米、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率
如果设置门槛,很多开发者朋友会因此错过这套高级架构资料,错过提升成为架构师的可能。这就失去了我们的初衷;让更多人都能通过高效高质量的学习,提升自己的技术和格局,升职加薪。
最后送给大家一句话,望共勉,永远不要放弃自己的梦想和追求;
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!
x(String suffix) {
if (suffix == null) {
mSuffix = “”;
} else {
mSuffix = suffix;
}
}
public String getSuffix() {
return mSuffix;
}
public void setPrefix(String prefix) {
if (prefix == null)
mPrefix = “”;
else {
mPrefix = prefix;
}
}
public String getPrefix() {
return mPrefix;
}
最后
一线互联网Android面试题含详解(初级到高级专题)
这些题目是今年群友去腾讯、百度、小米、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。并且大多数都整理了答案,熟悉这些知识点会大大增加通过前两轮技术面试的几率
[外链图片转存中…(img-HSVGo0Lr-1714691173072)]
如果设置门槛,很多开发者朋友会因此错过这套高级架构资料,错过提升成为架构师的可能。这就失去了我们的初衷;让更多人都能通过高效高质量的学习,提升自己的技术和格局,升职加薪。
最后送给大家一句话,望共勉,永远不要放弃自己的梦想和追求;
《Android学习笔记总结+移动架构视频+大厂面试真题+项目实战源码》,点击传送门,即可获取!