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

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

34资源网2022-07-28482

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

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

版权声明:34楼发布

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

分享给朋友:

相关文章

抱怨是一种毒药,比较有深度的好文

抱怨是一种毒药,比较有深度的好文

问:怎么每天 都能免费收到这种 好文章呢?…

抖音长视频怎么开通?抖音开通长视频的权限分享

抖音长视频怎么开通?抖音开通长视频的权限分享

经常玩抖音的朋友来说开通抖音1分钟长视频已经不是一件难事了。大家都知道抖音默认视频长度为15秒。只有达到一定要求才可以获得长视频权限。但是现在我们发现能发抖音长视频的朋友越来越多了。他们是怎么做到的呢?抖音怎么发长1分钟视频呢?抖音长视频是…

抖音上热门技巧有哪些(dou+投粉丝量还是点赞量)

抖音上热门技巧有哪些(dou+投粉丝量还是点赞量)

前不久,“越来和锅锅”发布了自己的第一条抖音视频,结果该视频一天的播放量达到了2581万,至今获赞205.7万。可见,选择适合自己的视频表现方式比努力更重要。 (图片来源:抖音APP) 抖音如何上热门,一直是小伙伴们想要了解的答案。 如果…

软件解压后怎么安装(手机解压软件app免费软件)

软件解压后怎么安装(手机解压软件app免费软件)

在PC端宅男、极客们会碰到需要解压缩文件的情况,这个时候大多会用到360压缩或者好压等第三方软件,然而到了移动手机端,由于自带的文件管理器大多不支持输入密码解压缩等复杂操作,让不少人对于手机解压缩文件失望,但是有时候电脑不在身边,又不得不在…