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

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

34资源网2022-07-28408

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

分享给朋友:

相关文章

苏格拉底的经典语录有哪些?分享52句苏格拉底经典语录

苏格拉底的经典语录有哪些?分享52句苏格拉底经典语录

很多人问苏格拉底的经典语录有哪些?今天小编就针对大家的疑问,给大家分享52句苏格拉底经典语录,希望对大家有所帮助。…

小型电冰箱什么牌子好?现在口碑最好的冰箱推荐

小型电冰箱什么牌子好?现在口碑最好的冰箱推荐

海尔冰箱全球公认的质量很不错的冰箱,海尔冰箱优点是:微霜,制冷效果不错;节能省电,保湿效果不错;海尔冰箱是十大品牌之首,全球销量的第一梯队的佼佼者。海尔冰箱外观十分好看,海尔冰箱款式几乎都很新颖,质量好,制冷情况好,用了一段时间,制冷情况稳…

联想新手机什么时候上市(联想2021即将上市新款笔记本)

联想新手机什么时候上市(联想2021即将上市新款笔记本)

11月8日,联想中国区手机业务部总经理发布了一则新机预告:摩托罗拉edge X的发布已进入倒计时阶段,在骁龙898处理器即将发布之际预告新机,很大可能预示着首发权已到手。…

这4类项目,投资人打死都不想投

这4类项目,投资人打死都不想投

很多创业者常感慨:明明自己的项目很好,但聊了很多投资人,却没人愿意投,这是为什么呢? 我们知道融资成功的关键,在于要让投资人看到项目的价值。这个价值,不仅仅是你的项目好就行,除了项目本身,投资人还有很多关注点,如果这些地方不“达标”,投资人…

psd缩略图查看器插件(可以查看psd的看图软件推荐)

psd缩略图查看器插件(可以查看psd的看图软件推荐)

作为一名设计师每天都会和PS打交道,毕竟每天的工作都需要用到PS。还有很多喜欢摄影拍照的小伙伴,也会经常用到PS。 设计师PS开挂神器粉丝免费领: 1、评论区吱一声就好 2、私信我:MZ 不过,对于每天都需要用到PS的设计师而言,也会遇见…