0
点赞
收藏
分享

微信扫一扫

Android 自定义ProgressBar显示百分比,阿里巴巴android开发手册

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 - mReachedB

Android 自定义ProgressBar显示百分比,阿里巴巴android开发手册

arHeight / 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;

}

public void incrementProgressBy(int by) {

if (by > 0) {

setProgress(getProgress() + by);

}

if (mListener != null) {

mListener.onProgressChange(getProgress(), getMax());

}

}

public void setProgress(int progress) {

if (progress <= getMax() && progress >= 0) {

this.mCurrentProgress = progress;

invalidate();

}

}

@Override

protected Parcelable onSaveInstanceState() {

final Bundle bundle = new Bundle();

bundle.putParcelable(INSTANCE_STATE, super.onSaveInstanceState());

bundle.putInt(INSTANCE_TEXT_COLOR, getTextColor());

bundle.putFloat(INSTANCE_TEXT_SIZE, getProgressTextSize());

bundle.putFloat(INSTANCE_REACHED_BAR_HEIGHT, getReachedBarHeight());

bundle.putFloat(INSTANCE_UNREACHED_BAR_HEIGHT, getUnreachedBarHeight());

bundle.putInt(INSTANCE_REACHED_BAR_COLOR, getReachedBarColor());

bundle.putInt(INSTANCE_UNREACHED_BAR_COLOR, getUnreachedBarColor());

bundle.putInt(INSTANCE_MAX, getMax());

bundle.putInt(INSTANCE_PROGRESS, getProgress());

bundle.putString(INSTANCE_SUFFIX, getSuffix());

bundle.putString(INSTANCE_PREFIX, getPrefix());

bundle.putBoolean(INSTANCE_TEXT_VISIBILITY, getProgressTextVisibility());

return bundle;

}

@Override

protected void onRestoreInstanceState(Parcelable state) {

if (state instanceof Bundle) {

final Bundle bundle = (Bundle) state;

mTextColor = bundle.getInt(INSTANCE_TEXT_COLOR);

mTextSize = bundle.getFloat(INSTANCE_TEXT_SIZE);

mReachedBarHeight = bundle.getFloat(INSTANCE_REACHED_BAR_HEIGHT);

mUnreachedBarHeight = bundle

.getFloat(INSTANCE_UNREACHED_BAR_HEIGHT);

mReachedBarColor = bundle.getInt(INSTANCE_REACHED_BAR_COLOR);

mUnreachedBarColor = bundle.getInt(INSTANCE_UNREACHED_BAR_COLOR);

initializePainters();

setMax(bundle.getInt(INSTANCE_MAX));

setProgress(bundle.getInt(INSTANCE_PROGRESS));

setPrefix(bundle.getString(INSTANCE_PREFIX));

setSuffix(bundle.getString(INSTANCE_SUFFIX));

setProgressTextVisibility(bundle

.getBoolean(INSTANCE_TEXT_VISIBILITY) ? ProgressTextVisibility.Visible

: ProgressTextVisibility.Invisible);

super.onRestoreInstanceState(bundle.getParcelable(INSTANCE_STATE));

return;

}

super.onRestoreInstanceState(state);

}

public float dp2px(float dp) {

final float scale = getResources().getDisplayMetrics().density;

return dp * scale + 0.5f;

}

public float sp2px(float sp) {

final float scale = getResources().getDisplayMetrics().scaledDensity;

return sp * scale;

}

public void setProgressTextVisibility(ProgressTextVisibility visibility) {

mIfDrawText = visibility == ProgressTextVisibility.Visible;

invalidate();

}

public boolean getProgressTextVisibility() {

return mIfDrawText;

}

public void setOnProgressBarListener(OnProgressBarListener listener) {

mListener = listener;

}

}

代码正在上传,稍后更新。
源码下载:

面试复习笔记

这份资料我从春招开始,就会将各博客、论坛。网站上等优质的Android开发中高级面试题收集起来,然后全网寻找最优的解答方案。每一道面试题都是百分百的大厂面经真题+最优解答。包知识脉络 + 诸多细节。
节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习。

《960页Android开发笔记》

Android 自定义ProgressBar显示百分比,阿里巴巴android开发手册

《1307页Android开发面试宝典》

包含了腾讯、百度、小米、阿里、乐视、美团、58、猎豹、360、新浪、搜狐等一线互联网公司面试被问到的题目。熟悉本文中列出的知识点会大大增加通过前两轮技术面试的几率。

Android 自定义ProgressBar显示百分比,阿里巴巴android开发手册

《507页Android开发相关源码解析》

只要是程序员,不管是Java还是Android,如果不去阅读源码,只看API文档,那就只是停留于皮毛,这对我们知识体系的建立和完备以及实战技术的提升都是不利的。

真正最能锻炼能力的便是直接去阅读源码,不仅限于阅读各大系统源码,还包括各种优秀的开源库。

Android 自定义ProgressBar显示百分比,阿里巴巴android开发手册

举报

相关推荐

0 条评论