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

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

34资源网2022-07-28378

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

分享给朋友:

相关文章

自动回复的经典句子,这11句值得收藏

自动回复的经典句子,这11句值得收藏

1、什么风把您吹来了,是timi赢了么。…

让长寿花开花有什么窍门?长寿花开花的几个条件分享

让长寿花开花有什么窍门?长寿花开花的几个条件分享

长寿花虽然说是一种多肉植物,但是它却和其他的很多多肉植物有很多不同的地方,长寿花由肥大、光亮的叶片形成的低矮株丛,终年翠绿。这种植物比较好看,也容易养,是送亲朋好友最好的礼物。马上要12月了,现在市面上卖花卉的很多。很多人不会养长寿花,尤其…

适合普通人做的小本创业点子

适合普通人做的小本创业点子

适合普通人做的小本创业生意有什么??随着零售行业的兴起,小型超市便利店生意成为创业者首选的项目之一,主要原因在于:投入资金小、回笼快,不需要太大的现金流来支撑、一年半左右就能回本。这对于拥有一部分闲置资金,想创业的投资者来说简直是很好的创业…

koko卡卡拖地机器人家用全自动擦地机推荐

koko卡卡拖地机器人家用全自动擦地机推荐

koko卡卡智能拖地机是东莞市宝联电子科技有限公司旗下产品,隶属于香港概念数码科技有限公司,其主要研发机器人吸尘器等高科技领域家居产品,想知道卡卡智能拖地机好用吗,看看下面是网友使用koko卡卡智能拖地机的相关介绍,希望对大家有所帮助。1、…

引流文案微信推广(微商引流推广文案模板)

引流文案微信推广(微商引流推广文案模板)

大家好啊!今天又跟大家分享小技巧啦~往下看↓↓↓ 首先说一下什么样的文案是引流型的?实际上,一句话是将公共域流量定向到您的私有域流量池。其目的是先引流然后慢慢进行信任激活变现。 在标题方面,通常有以下几种类型,今天为大家详细描述一下。 一…

华为小米革了康佳长虹们的命,海信怎么办?

华为小米革了康佳长虹们的命,海信怎么办?

编者按:本文来自陆玖财经,创业邦经授权发布。 电视行业真正需要面对的不是“大屏好还是小屏好”,用激光、OLED还是Mini LED之类的技术路线之争,而是一旦被视为“智能终端”,从产品形态到竞争模式的翻天覆地。 近日,海信子品牌Vidda…