欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页  >  移动技术

Android自定义圆环式进度条

程序员文章站 2022-04-03 18:49:09
安卓自定义圆环式进度条,供大家参考,具体内容如下需求是实现一个圆环式中间带有进度的进度条,自己动手实现一个package com.djt.aienglish.widget;import android...

安卓自定义圆环式进度条,供大家参考,具体内容如下

需求是实现一个圆环式中间带有进度的进度条,自己动手实现一个

package com.djt.aienglish.widget;

import android.content.context;
import android.content.res.typedarray;
import android.graphics.canvas;
import android.graphics.paint;
import android.graphics.rectf;
import android.text.textutils;
import android.util.attributeset;
import android.view.view;

import com.djt.aienglish.r;


/**
 * @author qiu
 * @date 2020/3/12 13:51
 */
public class circlepgbar extends view {
    private int mheight = 0;
    private int mwidth = 0;

    // 画圆环的画笔
    private paint mringpaint;
    // 画圆环的画笔背景色
    private paint mringpaintbg;
    // 画字体的画笔
    private paint mtextpaint;
    // 圆环颜色
    private int mringcolor;
    // 圆环背景颜色
    private int mringbgcolor;
    // 半径
    private float mradius;
    // 圆环半径
    private float mringradius;
    // 圆环宽度
    private float mstrokewidth;
    // 圆心x坐标
    private int mxcenter;
    // 圆心y坐标
    private int mycenter;
    // 字的长度
    private float mtxtwidth;
    // 字的高度
    private float mtxtheight;
    // 总进度
    private int max = 100;
    // 当前进度
    private int progress;
    private string text;

    public circlepgbar(context context, attributeset attrs) {
        super(context, attrs);
        // 获取自定义的属性
        initattrs(context, attrs);
        initvariable();
    }

    /**
     * 属性
     */
    private void initattrs(context context, attributeset attrs) {
        typedarray typearray = context.gettheme().obtainstyledattributes(attrs,
                r.styleable.taskscompletedview, 0, 0);
        mstrokewidth = typearray.getdimension(r.styleable.taskscompletedview_circlewidth, 0);
        mringcolor = typearray.getcolor(r.styleable.taskscompletedview_ringcolor, 0xffffffff);
        mringbgcolor = typearray.getcolor(r.styleable.taskscompletedview_ringbgcolor, 0xffffffff);
        text = typearray.getstring(r.styleable.taskscompletedview_text);
        max = typearray.getinteger(r.styleable.taskscompletedview_max, 0);
        progress = typearray.getinteger(r.styleable.taskscompletedview_progress, 0);
    }

    /**
     * 初始化画笔
     */
    private void initvariable() {
        //外圆弧背景
        mringpaintbg = new paint();
        mringpaintbg.setantialias(true);
        mringpaintbg.setcolor(mringbgcolor);
        mringpaintbg.setstyle(paint.style.stroke);
        mringpaintbg.setstrokewidth(mstrokewidth);
        //外圆弧
        mringpaint = new paint();
        mringpaint.setantialias(true);
        mringpaint.setcolor(mringcolor);
        mringpaint.setstyle(paint.style.stroke);
        mringpaint.setstrokewidth(mstrokewidth);
        //mringpaint.setstrokecap(paint.cap.round);//设置线冒样式,有圆 有方
        //中间字
        mtextpaint = new paint();
        mtextpaint.setantialias(true);
        mtextpaint.setstyle(paint.style.fill);
        mtextpaint.setcolor(mringcolor);
        invalidate();
    }

    //测量
    @override
    protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
        super.onmeasure(widthmeasurespec, heightmeasurespec);
        //实际测量宽高
        mheight = getmeasuredheight();
        mwidth = getmeasuredwidth();
        if (mwidth > mheight) {
            mradius = mheight / 2;
        } else {
            mradius = mwidth / 2;
        }
        //半径
        mringradius = mradius - mstrokewidth / 2;
        //文字宽高测量
        mtextpaint.settextsize(mradius / 2);
        paint.fontmetrics fm = mtextpaint.getfontmetrics();
        mtxtheight = (int) math.ceil(fm.descent - fm.ascent);
    }

    /**
     * 画图
     */
    @override
    protected void ondraw(canvas canvas) {
        mxcenter = mwidth / 2;
        mycenter = mheight / 2;
        //外圆弧背景
        rectf rectbg = new rectf(mxcenter - mringradius, mycenter - mringradius, mxcenter + mringradius, mycenter + mringradius);
        canvas.drawarc(rectbg, 0, 360, false, mringpaintbg);
        //外圆弧//进度
        if (progress > 0) {
            rectf oval = new rectf(mxcenter - mringradius, mycenter - mringradius, mxcenter + mringradius, mycenter + mringradius);
            canvas.drawarc(oval, -90, ((float) progress / max) * 360, false, mringpaint);
        }
        //字体
        if(!textutils.isempty(text)) {
            mtxtwidth = mtextpaint.measuretext(text, 0, text.length());
            canvas.drawtext(text, mxcenter - mtxtwidth / 2, mycenter + mtxtheight / 4, mtextpaint);
        }
    }

    /**
     * 设置进度
     *
     * @param progress
     */
    public void setprogress(int progress) {
        this.progress = progress;
        postinvalidate();//重绘
    }

    /**
     * 设置最大值
     *
     * @param max
     */
    public void setmax(int max) {
        this.max = max;
        postinvalidate();
    }

    /**
     * 设置文字内容
     *
     * @param text
     */
    public void settext(string text) {
        this.text = text;
        postinvalidate();
    }
}

别忘记在value下的attr.xml中加入默认配置属性

<!--圆弧进度条-->
    <declare-styleable name="taskscompletedview">
        <attr name="circlewidth" format="dimension" />
        <attr name="ringcolor" format="color" />
        <attr name="ringbgcolor" format="color" />
        <attr name="text" format="string" />
        <attr name="progress" format="integer" />
     <attr name="max" format="integer" />
</declare-styleable>

以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持。

相关标签: Android 进度条