本篇文章为大家展示了Android中实现圆形进度条的方法,内容简明扼要并且容易理解,绝对能使你眼前一亮,通过这篇文章的详细介绍希望你能有所收获。
主要思路是写一个继承ProgressBar的自定义View
代码:
package com.fun.progressbarwithnumber;import android.content.Context;import android.content.res.TypedArray;import android.graphics.Canvas;import android.graphics.Paint;import android.graphics.RectF;import android.util.AttributeSet;import android.util.TypedValue;import android.widget.ProgressBar;public class HorizontalProgressBarWithNumber extends ProgressBar { private static final int DEFAULT_TEXT_SIZE = 10; private static final int DEFAULT_TEXT_COLOR = 0XFFFC00D1; private static final int DEFAULT_COLOR_UNREACHED_COLOR = 0xFFd3d6da; private static final int DEFAULT_HEIGHT_REACHED_PROGRESS_BAR = 2; private static final int DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR = 2; private static final int DEFAULT_CIRCLE_COLOR = 0XFF3F51B5; protected Paint mPaint = new Paint(); // 字体颜色 protected int mTextColor = DEFAULT_TEXT_COLOR; // 字体大小 protected int mTextSize = sp2px(DEFAULT_TEXT_SIZE); // 覆盖进度高度 protected int mReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_REACHED_PROGRESS_BAR); // 覆盖进度颜色 protected int mReachedBarColor = DEFAULT_TEXT_COLOR; // 未覆盖进度高度 protected int mUnReachedProgressBarHeight = dp2px(DEFAULT_HEIGHT_UNREACHED_PROGRESS_BAR); // 未覆盖进度颜色 protected int mUnReachedBarColor = DEFAULT_COLOR_UNREACHED_COLOR; // 圆的颜色 protected int mCircleColor = DEFAULT_CIRCLE_COLOR; protected int mRealWidth; protected boolean mIfDrawText = true; protected boolean mIfDrawCircle = true; protected static final int VISIBLE = 0; public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs) { this(context, attrs, 0); } public HorizontalProgressBarWithNumber(Context context, AttributeSet attrs, int defStyle) { super(context, attrs, defStyle); obtainStyledAttributes(attrs); mPaint.setTextSize(mTextSize); mPaint.setColor(mTextColor); mPaint.setAntiAlias(true); } private void obtainStyledAttributes(AttributeSet attrs) { // 获取自定义属性 final TypedArray attributes = getContext().obtainStyledAttributes(attrs, R.styleable.HorizontalProgressBarWithNumber); mTextColor = attributes.getColor(R.styleable.HorizontalProgressBarWithNumber_progress_text_color, DEFAULT_TEXT_COLOR); mTextSize = (int) attributes.getDimension(R.styleable.HorizontalProgressBarWithNumber_progress_text_size, mTextSize); mCircleColor = attributes.getColor(R.styleable.HorizontalProgressBarWithNumber_progress_circle_color, DEFAULT_CIRCLE_COLOR); mReachedBarColor = attributes.getColor(R.styleable.HorizontalProgressBarWithNumber_progress_reached_color, mTextColor); mUnReachedBarColor = attributes.getColor(R.styleable.HorizontalProgressBarWithNumber_progress_unreached_color, DEFAULT_COLOR_UNREACHED_COLOR); mReachedProgressBarHeight = (int) attributes.getDimension(R.styleable.HorizontalProgressBarWithNumber_progress_reached_bar_height, mReachedProgressBarHeight); mUnReachedProgressBarHeight = (int) attributes.getDimension(R.styleable.HorizontalProgressBarWithNumber_progress_unreached_bar_height, mUnReachedProgressBarHeight); int textVisible = attributes.getInt(R.styleable.HorizontalProgressBarWithNumber_progress_text_visibility, VISIBLE); if (textVisible != VISIBLE) { mIfDrawText = false; } attributes.recycle(); int left = (int) (mReachedProgressBarHeight * 0.8), right = (int) (mReachedProgressBarHeight * 0.8); int top = (int) (mReachedProgressBarHeight * 0.3 + dp2px(1)), bottom = (int) (mReachedProgressBarHeight * 0.3 + dp2px(1)); setPadding(left, top, right, bottom); } @Override protected synchronized void onMeasure(int widthMeasureSpec, int heightMeasureSpec) { int width = MeasureSpec.getSize(widthMeasureSpec); int height = measureHeight(heightMeasureSpec); setMeasuredDimension(width, height); mRealWidth = getMeasuredWidth() - getPaddingRight() - getPaddingLeft(); } private int measureHeight(int measureSpec) { int result; int specMode = MeasureSpec.getMode(measureSpec); int specSize = MeasureSpec.getSize(measureSpec); if (specMode == MeasureSpec.EXACTLY) { result = specSize; } else { float textHeight = (mPaint.descent() - mPaint.ascent()); result = (int) (getPaddingTop() + getPaddingBottom() + Math.max( Math.max(mReachedProgressBarHeight, mUnReachedProgressBarHeight), Math.abs(textHeight))); if (specMode == MeasureSpec.AT_MOST) { result = Math.min(result, specSize); } } return result; } @Override protected synchronized void onDraw(Canvas canvas) { canvas.save(); canvas.translate(getPaddingLeft(), getHeight() / 2); boolean noNeedBg = false; float radio = getProgress() * 1.0f / getMax(); float progressPosX = (int) (mRealWidth * radio); String text = getProgress() + "%"; float textWidth = mPaint.measureText(text); float textHeight = (mPaint.descent() + mPaint.ascent()) / 2; float radius = (mReachedProgressBarHeight + getPaddingBottom() + getPaddingTop()) / 2; // 覆盖的进度 float endX = progressPosX; if (endX > -1) { mPaint.setColor(mReachedBarColor); RectF rectF = new RectF(0, 0 - getPaddingTop() - getPaddingBottom(), endX, mReachedProgressBarHeight - getPaddingBottom()); canvas.drawRoundRect(rectF, 25, 25, mPaint); } // 未覆盖的进度 if (!noNeedBg) { float start = progressPosX; mPaint.setColor(mUnReachedBarColor); RectF rectF = new RectF(start, 0 - getPaddingTop() - getPaddingBottom(), mRealWidth + getPaddingRight() - radius, mReachedProgressBarHeight - getPaddingBottom()); canvas.drawRoundRect(rectF, 25, 25, mPaint); } // 圆 if (mIfDrawCircle) { mPaint.setColor(mCircleColor); canvas.drawCircle(progressPosX, 0, radius, mPaint); } // 文本 if (mIfDrawText) { mPaint.setColor(mTextColor); canvas.drawText(text, progressPosX - textWidth / 2, -textHeight, mPaint); } canvas.restore(); } protected int dp2px(int dpVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, dpVal, getResources().getDisplayMetrics()); } protected int sp2px(int spVal) { return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, spVal, getResources().getDisplayMetrics()); }}
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
软考中级精品资料免费领
- 历年真题答案解析
- 备考技巧名师总结
- 高频考点精准押题
- 资料下载
- 历年真题
193.9 KB下载数265
191.63 KB下载数245
143.91 KB下载数1148
183.71 KB下载数642
644.84 KB下载数2756
相关文章
发现更多好内容猜你喜欢
AI推送时光机Android中实现圆形进度条的方法
后端开发2023-05-31
Android View实现圆形进度条
后端开发2024-04-02
android圆形进度条怎么实现
后端开发2023-08-30
android如何实现圆形进度条
后端开发2023-08-20
Android实现自定义圆形进度条
后端开发2022-06-06
Android实现带数字的圆形进度条(自定义进度条)
后端开发2022-06-06
Android自定义View实现圆形进度条
后端开发2024-04-02
怎么用Android View实现圆形进度条
后端开发2023-06-20
Android 实现自定义圆形进度条的三种常用方法
后端开发2023-03-01
Android studio中怎么实现一个圆形进度条
后端开发2023-05-30
Android自定义View实现圆形加载进度条效果的方法
后端开发2023-05-30
Android 实现自定义圆形进度条的功能
后端开发2022-06-06
Android实现自定义圆形进度条的常用方法有哪些
后端开发2023-07-05
Android自定义控件实现圆形进度条
后端开发2022-06-06
Android三种方式实现ProgressBar自定义圆形进度条
后端开发2022-06-06
Android自定义View圆形进度条控件的方法
后端开发2023-05-31
Android 实现自定义圆形进度条的实例代码
后端开发2022-06-06
Android自定义View实现圆形加载进度条
后端开发2024-04-02
Android怎么自定义View实现圆形进度条
后端开发2023-07-02
咦!没有更多了?去看看其它编程学习网 内容吧