从源码解析MagicProgressWidget:Android进度控件实现原理

📅 2026/7/16 19:02:42 👤 编程新知 🏷️ 技术资讯
从源码解析MagicProgressWidget:Android进度控件实现原理 从源码解析MagicProgressWidgetAndroid进度控件实现原理【免费下载链接】MagicProgressWidgetMagicProgressCircle MagicProgressBar项目地址: https://gitcode.com/gh_mirrors/ma/MagicProgressWidgetMagicProgressWidget是一个专为Android平台设计的轻量级进度控件库包含MagicProgressCircle圆形进度条和MagicProgressBar线性进度条两种核心组件。本文将从源码角度深入剖析这两个控件的实现原理帮助开发者理解其内部工作机制和自定义方法。控件初始化与属性定义MagicProgressWidget通过自定义属性实现灵活配置所有可配置属性都在library/src/main/res/values/attrs.xml文件中定义declare-styleable nameMagicProgressCircle attr namempc_percent formatfloat / attr namempc_stroke_width formatdimension / attr namempc_start_color formatcolor / attr namempc_end_color formatcolor / attr namempc_default_color formatcolor / attr namempc_foot_over_head formatboolean/ /declare-styleable declare-styleable nameMagicProgressBar attr namempb_percent formatfloat / attr namempb_fill_color formatcolor / attr namempb_background_color formatcolor / attr namempb_flat formatboolean / /declare-styleable在Java代码中控件通过构造函数解析这些属性。以MagicProgressCircle为例其构造函数实现如下public MagicProgressCircle(Context context, AttributeSet attrs) { this(context, attrs, 0); } public MagicProgressCircle(Context context, AttributeSet attrs, int defStyleAttr) { this(context, attrs, defStyleAttr, 0); } public MagicProgressCircle(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) { super(context, attrs, defStyleAttr, defStyleRes); // 初始化代码 typedArray context.obtainStyledAttributes(attrs, R.styleable.MagicProgressCircle); percent typedArray.getFloat(R.styleable.MagicProgressCircle_mpc_percent, defaultPercent); strokeWidth (int) typedArray.getDimension(R.styleable.MagicProgressCircle_mpc_stroke_width, strokeWdithDefaultValue); // 其他属性解析 }MagicProgressCircle实现原理核心绘制逻辑圆形进度条的绘制主要在onDraw方法中完成通过Canvas绘制背景圆环和进度圆弧protected void onDraw(Canvas canvas) { super.onDraw(canvas); final int restore canvas.save(); // 计算中心点和半径 float cx getWidth() / 2; float cy getHeight() / 2; float radius Math.min(cx, cy) - strokeWidth / 2; // 绘制背景圆环 paint.setColor(defaultColor); canvas.drawCircle(cx, cy, radius, paint); // 绘制进度圆弧 if (drawPercent 0) { // 绘制前半圆 canvas.save(); canvas.rotate(90f, cx, cy); canvas.drawArc(rectF, 0f, 180f * drawPercent, true, startPaint); canvas.restore(); // 绘制后半圆当进度超过50%时 if (drawPercent 0.5f) { canvas.save(); canvas.rotate(270f, cx, cy); canvas.drawArc(rectF, 0f, 180f * (drawPercent - 0.5f), true, endPaint); canvas.restore(); } } canvas.restoreToCount(restore); }颜色渐变实现MagicProgressCircle支持从开始颜色到结束颜色的渐变效果通过calculatePercentEndColor方法动态计算中间颜色值private void calculatePercentEndColor(float percent) { if (percent 0.5f) { // 前半段使用开始颜色 startPaint.setColor(startColor); endPaint.setColor(startColor); } else { // 后半段计算渐变色 float colorPercent (percent - 0.5f) / 0.5f; int red (int) (Color.red(startColor) (Color.red(endColor) - Color.red(startColor)) * colorPercent); int green (int) (Color.green(startColor) (Color.green(endColor) - Color.green(startColor)) * colorPercent); int blue (int) (Color.blue(startColor) (Color.blue(endColor) - Color.blue(startColor)) * colorPercent); endPaint.setColor(Color.rgb(red, green, blue)); } }MagicProgressBar实现原理线性进度绘制线性进度条的绘制同样在onDraw方法中实现支持两种样式圆角矩形和扁平样式protected void onDraw(Canvas canvas) { super.onDraw(canvas); float drawPercent percent; canvas.save(); // 计算进度条宽度 float fillWidth drawPercent * width; // 绘制背景 backgroundPaint.setColor(backgroundColor); canvas.drawRoundRect(rectF, radius, radius, backgroundPaint); // 绘制进度 fillPaint.setColor(fillColor); if (fillWidth radius * 2) { // 进度较小时绘制左半圆 canvas.drawArc(rectF, 90, 180, true, fillPaint); } else if (fillWidth width - radius * 2) { // 进度较大时绘制完整进度条和右半圆 canvas.drawRoundRect(rectF, radius, radius, fillPaint); } else { // 中间进度绘制矩形 canvas.drawRect(0, 0, fillWidth, height, fillPaint); } canvas.restore(); }平滑动画实现两个控件都实现了ISmoothTarget接口支持进度的平滑过渡动画Override public void setSmoothPercent(float percent, int duration) { ValueAnimator animator ValueAnimator.ofFloat(getPercent(), percent); animator.setDuration(duration); animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() { Override public void onAnimationUpdate(ValueAnimator animation) { setPercent((float) animation.getAnimatedValue()); } }); animator.start(); }实际应用示例在Demo项目中MainActivity展示了如何在布局文件中声明并在代码中控制这些控件// 初始化控件 demoMpc (MagicProgressCircle) findViewById(R.id.demo_mpc); demo1Mpb (MagicProgressBar) findViewById(R.id.demo_1_mpb); // 设置进度 demoMpc.setPercent(0.75f); demo1Mpb.setSmoothPercent(0.6f, 1000);自定义与扩展开发者可以通过以下方式扩展MagicProgressWidget的功能添加新属性在attrs.xml中定义新的自定义属性重写绘制方法继承MagicProgressCircle或MagicProgressBar并重写onDraw方法扩展动画效果实现ISmoothTarget接口添加自定义动画通过这些扩展点开发者可以轻松定制出符合自己应用风格的进度控件。总结MagicProgressWidget通过简洁的代码实现了功能丰富的进度控件其核心原理是利用Android的Canvas绘制API结合属性动画实现平滑过渡效果。理解其实现原理不仅有助于更好地使用这个库也为开发自定义Android控件提供了参考思路。无论是圆形进度条还是线性进度条MagicProgressWidget都提供了足够的自定义选项满足不同场景的需求。【免费下载链接】MagicProgressWidgetMagicProgressCircle MagicProgressBar项目地址: https://gitcode.com/gh_mirrors/ma/MagicProgressWidget创作声明:本文部分内容由AI辅助生成(AIGC),仅供参考