Android开发--ProgressBar应用显示测点数量

1.自定义ProgressBarView

java 复制代码
public class ProgressBarView extends View {
    private Paint mPaintBack;
    private Paint mPaint;
    private Paint mPaintText;
    private float process;
    int strokeWidth = 3;//圈宽度
    int textSize= 17;//字大小
    private long duration = 1000;
    private float startDegree = 0;
    private float endDegree = 360;
    private float total = 16; // 默认总数为16
    private int color = Color.GRAY; // 默认颜色为灰色
    private String name = "在线测点";
    private String number = "0";


    public ProgressBarView(Context context) {
        super(context);
        init();
    }

    public ProgressBarView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public ProgressBarView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    private void init() {
        mPaintBack = new Paint();
        mPaintBack.setColor(Color.GRAY);
        mPaintBack.setStyle(Paint.Style.STROKE);
        mPaintBack.setAntiAlias(true);
        mPaintBack.setStrokeCap(Paint.Cap.ROUND);
        mPaintBack.setStrokeWidth(strokeWidth);

        mPaint = new Paint();
        mPaint.setColor(color);
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);
        mPaint.setStrokeWidth(strokeWidth);

        mPaintText = new Paint();
        mPaintText.setAntiAlias(true);
        mPaintText.setStyle(Paint.Style.FILL_AND_STROKE);
        mPaintText.setColor(color);
        mPaintText.setStrokeCap(Paint.Cap.ROUND);
        mPaintText.setTextSize(sp2px((int)textSize));
    }

    public void setStrokeWidth(int width) {
        strokeWidth = width;
    }

    public void setTextSize(int textSize) {
        this.textSize = textSize;
    }

    public void setDuration(long duration) {
        this.duration = duration;
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        // 创建圆环矩形
        RectF rectF = new RectF(strokeWidth, strokeWidth, getWidth() - strokeWidth, getHeight() - strokeWidth);
        // 画出背景进度条
        canvas.drawArc(rectF, 0, 360, false, mPaintBack);
        // 画进度条
        canvas.drawArc(rectF, 0, process, false, mPaint);
        // 设置文字在canvas中的位置
        Paint.FontMetrics fm = mPaintText.getFontMetrics();
        int mTxtWidth = (int) mPaintText.measureText(number, 0, number.length());
        int mTxtHeight = (int) Math.ceil(fm.descent - fm.ascent);
        int x = getWidth() / 2 - mTxtWidth / 2;
        int y = getHeight() / 2;
        canvas.drawText(number, x, y, mPaintText);
        canvas.drawText(name, x-(int) (mPaintText.measureText(name, 0, name.length())/3), y + mTxtHeight, mPaintText);
    }

    /**
     * 设置传入总测点数
     */
    public void setTotal(float total) {
        this.total = total;
    }
    /**
     * 设置传入name
     */
    public void setName(String name) {
        this.name = name;
    }
    /**
     * 设置传入颜色
     */
    public void setColor(int color) {
        this.color = color;
        mPaint.setColor(color);
        mPaintText.setColor(color);
    }

    /**
     * 设置传入测点数
     */
    public void setProgress(float progress) {
        float angle = (progress / total) * 360;
        startDegree = 0;
        endDegree = angle;
        // 更新number的值
        number = String.valueOf((int)progress);
        invalidate();
    }

    /**
     * 设置动画效果
     */
    public void start() {
        ValueAnimator valueAnimator = ValueAnimator.ofFloat(startDegree, endDegree);
        valueAnimator.setDuration(duration);
        valueAnimator.setInterpolator(new DecelerateInterpolator());
        valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
            @Override
            public void onAnimationUpdate(ValueAnimator animation) {
                float value = (float) animation.getAnimatedValue();
                process = (int)value;
                invalidate();
            }
        });
        valueAnimator.start();
    }

    private int sp2px(int sp) {
        return (int) TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, sp,
                getResources().getDisplayMetrics());
    }
}

2.XML引用

XML 复制代码
<com.afison.newfault.view.ProgressBarView
     android:id="@+id/pb_ele"
     android:layout_width="108dp"
     android:layout_height="108dp"
     android:layout_marginEnd="@dimen/margin_big"
     android:layout_marginTop="@dimen/margin_medium"
     android:layout_marginBottom="@dimen/margin_medium">

</com.afison.newfault.view.ProgressBarView>

3.更新UI

java 复制代码
pbEle.setColor(getResources().getColor(R.color.pbBlue));
        pbEle.setName("电流测点");
        pbEle.setTotal(16);
        pbEle.setProgress(14);
        pbEle.start();

4.效果展示

相关推荐
JMchen12316 分钟前
Android后台服务与网络保活:WorkManager的实战应用
android·java·网络·kotlin·php·android-studio
crmscs1 小时前
剪映永久解锁版/电脑版永久会员VIP/安卓SVIP手机永久版下载
android·智能手机·电脑
localbob1 小时前
杀戮尖塔 v6 MOD整合版(Slay the Spire)安卓+PC端免安装中文版分享 卡牌肉鸽神作!杀戮尖塔中文版,电脑和手机都能玩!杀戮尖塔.exe 杀戮尖塔.apk
android·杀戮尖塔apk·杀戮尖塔exe·游戏分享
机建狂魔1 小时前
手机秒变电影机:Blackmagic Camera + LUT滤镜包的专业级视频解决方案
android·拍照·摄影·lut滤镜·拍摄·摄像·录像
hudawei9961 小时前
flutter和Android动画的对比
android·flutter·动画
lxysbly3 小时前
md模拟器安卓版带金手指2026
android
儿歌八万首4 小时前
硬核春节:用 Compose 打造“赛博鞭炮”
android·kotlin·compose·春节
消失的旧时光-19436 小时前
从 Kotlin 到 Dart:为什么 sealed 是处理「多种返回结果」的最佳方式?
android·开发语言·flutter·架构·kotlin·sealed
Jinkxs6 小时前
Gradle - 与Groovy/Kotlin DSL对比 构建脚本语言选择指南
android·开发语言·kotlin
&有梦想的咸鱼&6 小时前
Kotlin委托机制的底层实现深度解析(74)
android·开发语言·kotlin