当前位置:首页 > 谈天说地 > 正文内容

Android实现简单实用的垂直进度条

34资源网2022年07月28日 11:00278

本文实例为大家分享了android实现简单垂直进度条的具体代码,供大家参考,具体内容如下

代码注释特别清晰,就不多解释了

支持属性:

  • progress_radius     进度条圆角大小
  • progress_border_enable  进度条是否有边框
  • progress_gradient_enable 进度条颜色是否渐变
  • progress_start_color     从上到下进度条开始的渐变色
  • progress_end_color    从上到下进度条结束的渐变色
  • progress_solid_color    带边框进度条的背景填充色
  • progress_border_color    进度条边框的颜色
  • progress_border_width     进度条边框的宽度

有需要定义其他属性的,可以进行扩充下面是效果图

上代码

verticalprogress

public class verticalprogress extends view {

    //进度条圆角
    private int mradius;
    //进度条是否有边框
    private boolean mborderenable;
    //是否有渐变色
    private boolean mgradientenable;
    //渐变色
    private int mstartresid;
    private int mendresid;
    //边框的颜色
    private int mbordercolorresid;
    //进度条背景填充色
    private int mprogressbgcolorid;
    //边框宽度
    private int mborderwidth;

    private int mprogress = 10;
    private int max = 100;

    private int mwidth;
    private int mheight;

    private rectf mrectf;
    private paint mpaint;

    public verticalprogress(context context) {
        super(context);
        init(context, null);
    }

    public verticalprogress(context context, @nullable attributeset attrs) {
        super(context, attrs);
        init(context, attrs);
    }

    @override
    protected void onmeasure(int widthmeasurespec, int heightmeasurespec) {
        super.onmeasure(widthmeasurespec, heightmeasurespec);
        mwidth = getmeasuredwidth() - 1;// 宽度值
        mheight = getmeasuredheight() - 1;// 高度值
    }

    private void init(context context, attributeset attrs) {
        typedarray typedarray = null;
        if (attrs != null) {
            typedarray = context.obtainstyledattributes(attrs, r.styleable.verticalprogress);

            mradius = typedarray.getint(r.styleable.verticalprogress_progress_radius, 0);
            mborderenable = typedarray.getboolean(r.styleable.verticalprogress_progress_border_enable, false);
            mgradientenable = typedarray.getboolean(r.styleable.verticalprogress_progress_gradient_enable, true);
            mstartresid = typedarray.getresourceid(r.styleable.verticalprogress_progress_start_color, r.color.colorprimary);
            mprogressbgcolorid = typedarray.getresourceid(r.styleable.verticalprogress_progress_solid_color, r.color.white);
            mendresid = typedarray.getresourceid(r.styleable.verticalprogress_progress_end_color, r.color.color_4ea6fd);
            mbordercolorresid = typedarray.getresourceid(r.styleable.verticalprogress_progress_border_color, r.color.color_4ea6fd);
            mborderwidth = typedarray.getresourceid(r.styleable.verticalprogress_progress_border_width, 10);
        }

        if (typedarray != null) {
            typedarray.recycle();
        }

        mrectf = new rectf();
        mpaint = new paint();
        mpaint.setantialias(true);
    }

    @suppresslint("drawallocation")
    @override
    protected void ondraw(canvas canvas) {
        super.ondraw(canvas);
        if (mradius == 0) {
            //弧度为高度的一半
            mradius = mwidth / 2;
        }

        if (mborderenable) {
            //第一层矩形(描边层)
            mrectf.set(0, 0, mwidth, mheight);
            //第一层矩形颜色(进度条描边的颜色)
            mpaint.setcolor(getresources().getcolor(mbordercolorresid));
            //画第一层圆角矩形
            canvas.drawroundrect(mrectf, mradius, mradius, mpaint);
            //第二层矩形颜色(背景层颜色)
            mpaint.setcolor(getresources().getcolor(mprogressbgcolorid));
            //第二层矩形(背景层)
            mrectf.set(mborderwidth, mborderwidth, mwidth - mborderwidth, mheight - mborderwidth);
            //画背景层圆角矩形(盖在描边层之上)
            canvas.drawroundrect(mrectf, mradius, mradius, mpaint);
        }

        if (mprogress == 0)//进度为 0不画进度
            return;

        float section = mprogress / max;

        //进度层底层
        mrectf.set(+8, mheight - mprogress / 100f * mheight + 10, mwidth - 8, mheight - 8);

        if (mgradientenable) {
            //渐变器
            lineargradient shader = new lineargradient(0, 0, mwidth * section, mheight,
                    getresources().getcolor(mstartresid), getresources().getcolor(mendresid), shader.tilemode.clamp);

            //第三层矩形颜色(进度渐变色)
            mpaint.setshader(shader);
        }

        //画第三层(进度层)圆角矩形(盖在背景层之上)
        canvas.drawroundrect(mrectf, mradius, mradius, mpaint);

        //清除之前传递的shader
        mpaint.setshader(null);
    }

    public void setprogress(int currentcount) {

        this.mprogress = currentcount > max ? max : currentcount;

        postinvalidate();

    }
}

attr.xml样式

<declare-styleable name="verticalprogress">
        <attr name="progress_radius" format="dimension" />
        <attr name="progress_border_width" format="dimension" />
        <attr name="progress_gradient_enable" format="boolean" />
        <attr name="progress_border_enable" format="boolean" />
        <attr name="progress_start_color" format="color" />
        <attr name="progress_solid_color" format="color" />
        <attr name="progress_end_color" format="color" />
        <attr name="progress_border_color" format="boolean" />
</declare-styleable>

最后调用示例

<com.widget.verticalprogress
            android:id="@+id/vp_progress"
            android:layout_width="20dp"
            android:layout_height="match_parent"
            app:progress_border_enable="true"
            app:progress_solid_color="@color/white"
            android:layout_centerinparent="true" />

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

看完文章,还可以用支付宝扫描下面的二维码领取一个支付宝红包,目前可领1-88元不等

支付宝红包二维码

除了扫码可以领取之外,大家还可以(复制 720087999 打开✔支付宝✔去搜索, h`o`n.g.包哪里来,动动手指就能领)。

看下图所示是好多参与这次活动领取红包的朋友:

支付宝红包

扫描二维码推送至手机访问。

版权声明:本文由34楼发布,如需转载请注明出处。

本文链接:https://www.34l.com/post/19936.html

分享给朋友:

相关文章

分享10句来自农村的老话名言,句句都很经典
分享10句来自农村的老话名言,句句都很经典

1、家有万贯带毛的不算家里无论都多少畜禽,在未换成钱之前都不叫钱,一场疫情或较大价格波动便成了空。2、富在深山有远亲,穷在闹市无人问人富了,躲到深山老林都有八竿子打不到的亲戚上门拜访;人穷了,离的再近的亲戚都不愿搭理你。3、再穷不卖看家狗,...

情浓时,海誓山盟;情淡时,形同陌路
情浓时,海誓山盟;情淡时,形同陌路

1、如果一个人影响到了你的情绪,你的焦点应该放在控制自己的情绪上,而不是影响你情绪的人身上。只有这样,才能真正自信起来。2、重要的不是你从哪里来,而是你到哪里去,找准方向,继续努力。3、所有的节日都不是为了礼物,而是提醒大家别忘了爱与被爱。...

​分享5句用薪尽火灭成语造句
​分享5句用薪尽火灭成语造句

1、入涅盘又称入灭、薪尽火灭(薪喻佛身或机缘,火喻智慧或佛身)。2、否则,咱师徒情分从此刻开始薪尽火灭。3、铲除这些非正常因素,降温楼市才能“薪尽火灭”。4、薪尽火灭:咱们的观点既是云云不同,那麼从今以后你别再来见我,从这个时候起薪尽火灭好...

打工人是什么梗出自哪里?打工人的梗为什么这么火?
打工人是什么梗出自哪里?打工人的梗为什么这么火?

近日,看到打工人这词很火,很多小伙伴肯定和我一样,都想知道“打工人”这个词为什么这么火吧?那么,接下来我就跟大家说说打工人是什么梗出自哪里?这词出自哪里无从考证,不过肯定是某些有头脑的网友发明的,至于出自哪位网友?我们一起来了解相关情况吧。...

失控玩家怎么样?好看吗?值不值得看?
失控玩家怎么样?好看吗?值不值得看?

每部电影出来都会有人说好看,有人说不好看。只是有些电影拍出来会更加符合大众口味,有些则只适合小部分人看。那么,最近热播的这部失控玩家怎么样?好看吗?值不值得看?今天小编就和大家说说这部适合大众口味的电影。不同于《头号玩家》那种通过彩蛋引爆观...

短视频文案素材哪里找(上热门的短视频素材)
短视频文案素材哪里找(上热门的短视频素材)

抖音怎么写文案?整理了100条抖音抖音爆款文案,直接套用就能火不看后悔系列!!!抖音上爆火的文案。情感、励志、共鸣、实用、反转、提问等6大类型不管你是拍摄什么类型视频 ,都可以直接套用。不多说直接上干货!!!一定要看到最后!!!01—励志类...