当前位置:首页 > 谈天说地

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

34资源网2022-07-28436

本文实例为大家分享了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" />

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

看完文章,还可以扫描下面的二维码下载快手极速版领4元红包

快手极速版二维码

快手极速版新人见面礼

除了扫码领红包之外,大家还可以在快手极速版做签到,看视频,做任务,参与抽奖,邀请好友赚钱)。

邀请两个好友奖最高196元,如下图所示:

快手极速版邀请好友奖励

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

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

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

分享给朋友:

相关文章

哪家的云主机好(国内五大云主机服务商)

哪家的云主机好(国内五大云主机服务商)

导言:博睿数据(股票代码688229)十余年专注APM领域,已为超过2000余家大型企业提供专业数据服务。依托先进的测评技术及丰富的行业经验,博睿宏远倾力打造了一个公开透明的性能测评栏目——【Bonree指数】。该栏目致力于呈现各行业的整体…

逍遥手机模拟器怎么用(逍遥安卓模拟器详细使用教程)

逍遥手机模拟器怎么用(逍遥安卓模拟器详细使用教程)

真正的5V5公平竞技对战,传承端游纯正体验。人气英雄,经典还原;公平竞技,实力至上;峡谷传说,掌心再现。策略、战术、意识、配合,在移动端重现峡谷战场乐趣。 为了庆祝大家期待已久的中国区开服,官方也带来了五大福利活动,用户可免费参与,并获得十…

itunes备份路径在哪里(教你查看itunes备份路径)

itunes备份路径在哪里(教你查看itunes备份路径)

用itunes备份的都知道,备份到C盘的,日积月累,C盘越来越小了,上网教程参差不齐,本教程本人亲测,因为我看到我的64G SSD空间越来越小,伤不起!!!操作步骤1、下载Junction,将Junction.exe拷贝到C:Windows…

mac地址的作用是什么(理解mac地址的作用实验报告)

mac地址的作用是什么(理解mac地址的作用实验报告)

我们上网时会发现电子设备同时有IP地址和MAC地址,为什么上网会需要两个地址呢?…

华为、苹果,为何“盯”上二手机?

华为、苹果,为何“盯”上二手机?

图源:摄图网 编者按:本文来自微信公众号松果财经(ID:songguocaijing1),创业邦经授权转载 继苹果后,华为近日也正式官宣开启二手手机业务,并表示此举是为了提升电子产品的循环再利用。华为官方承诺所售二手手机均为正品且经过严格把…

苏宁易付宝在哪里找(苏宁易付宝解绑流程)

苏宁易付宝在哪里找(苏宁易付宝解绑流程)

天眼查App显示,近日,南京苏宁易付宝网络科技有限公司发生工商变更,尚姬娟退出法定代表人、执行董事、总经理一职,由卢世栋接任。 南京苏宁易付宝网络科技有限公司成立于2011年1月,注册资本为10亿人民币,经营范围包含互联网支付;计算机网络…