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

Android新建水平节点进度条示例

34资源网2022年06月27日 09:43183
站长推荐》》》》》摆摊地推这两款软件,一个月3000元-3万元轻松实现《《《《《站长推荐

前言

效果图

前几天在网上没有找到合适的横向节点进度条,自己动手写了一个,先来看看效果图

圆圈和文字状态

我们看到小圆圈和文字有几种状态呢?

  • 第一个空心的小圆圈是处理完成的状态
  • 第二个实心的小圆圈是处理中的状态
  • 第三个实心的小圆圈是待处理的状态
    没错,我们看到了小圆圈和文字有三种处理状态

文字居中

我们写一个类继承自appcompattextview,通过onmeasure方法得到控件的宽高,通过paint的gettextbounds()也可以知道文字的宽高,我们看到有5个节点需要处理,我们把屏幕划分成5个等份,每个等份都相等,这里用itemwidth 表示每个相同的等份。文字居中的写法很简单,

itemwidth / 2 - textwidth / 2

代码

package cn.wwj.customview.widget
import android.content.context
import android.graphics.canvas
import android.graphics.color
import android.graphics.paint
import android.graphics.rect
import android.text.textpaint
import android.util.attributeset
import android.util.log
import androidx.annotation.nullable
import androidx.appcompat.widget.appcompattextview
import androidx.core.content.contextcompat
import androidx.core.view.margintop
import cn.wwj.customview.r
import cn.wwj.customview.dp2px
import cn.wwj.customview.sp2px
/**
 * 节点进度条
 */
class nodepointprocessbar : appcompattextview {
    /**
     * 文字画笔
     */
    private lateinit var mtextpaint: textpaint
    /**
     * 圆画笔
     */
    private lateinit var mcirclepaint: paint
    private var isdebug = false
    /**
     * 已完成文字颜色
     */
    private var mcompletetextcolor: int = contextcompat.getcolor(context, android.r.color.black)
    /**
     * 处理中文字颜色
     */
    private var mprocesstextcolor: int = contextcompat.getcolor(context, r.color.purple)
    /**
     * 待处理的文字颜色
     */
    private var mwaitprocesstextcolor: int = contextcompat.getcolor(context, r.color.gray_text)
    /**
     * 绘制的节点个数,由底部节点标题数量控制
     */
    private var mcirclecount = 0
    private var tag = "nodepointprocessbar"
    /**
     * 圆的半径
     */
    private var mcircleradius = 5f.dp2px()
    /**
     * 圆圈的边框线
     */
    private var mcircleborder = 1f.dp2px()
    /**
     * 线的宽度
     */
    private var mlinewidth = 1f.dp2px()
    /**
     * 线之间的左右边距
     */
    private var mlinemargin = 4f.dp2px()
    /**
     * 文字和圆圈之间的距离
     */
    var mtextcirclemargin = 7f.dp2px()
    /**
     * 文字的水平边距
     */
    private var mtextleftrightmargin = 8f.dp2px()
    /**
     * 计算内容的高度 和 宽度
     */
    private var mcontentheight = 0f
    private var mcontentwidth = 0f
    /**
     * 节点底部的文字列表
     */
    private var mtextlist: list<string> = mutablelistof()
    /**
     * 选中项集合
     */
    private var mprocessindexset: set<int> = mutablesetof()
    /**
     * 文字同宽高的矩形,用来测量文字
     */
    private var mtextboundlist: mutablelist<rect> = mutablelistof()
    /**
     * 计算文字宽高的矩形
     */
    private val mrect = rect()
    constructor(context: context) : this(context, null)
    constructor(context: context, @nullable attrs: attributeset?) : this(context, attrs, 0)
    constructor(
        context: context,
        @nullable attrs: attributeset?,
        defstyleattr: int
    ) : super(context, attrs, defstyleattr) {
        val appearance = context.obtainstyledattributes(attrs, r.styleable.nodepointprocessbar)
        mcompletetextcolor = appearance.getcolor(
            r.styleable.nodepointprocessbar_completedtextcolor, mcompletetextcolor
        )
        mwaitprocesstextcolor = appearance.getcolor(
            r.styleable.nodepointprocessbar_processtextcolor, mwaitprocesstextcolor
        )
        mprocesstextcolor =
            appearance.getcolor(
                r.styleable.nodepointprocessbar_waitprocesstextcolor,
                mprocesstextcolor
            )
        isdebug = appearance.getboolean(
            r.styleable.nodepointprocessbar_isdebug, isdebug
        )
        mtextcirclemargin = appearance.getdimension(
            r.styleable.nodepointprocessbar_textcirclemargin, mtextcirclemargin
        )
        mtextleftrightmargin = appearance.getdimension(
            r.styleable.nodepointprocessbar_textleftrightmargin, mtextleftrightmargin
        )
        mcircleradius = appearance.getdimension(
            r.styleable.nodepointprocessbar_npbcircleradius, mcircleradius
        )
        mcircleborder = appearance.getdimension(
            r.styleable.nodepointprocessbar_circleborder, mcircleborder
        )
        mlinewidth = appearance.getdimension(
            r.styleable.nodepointprocessbar_linewidth, mlinewidth
        )
        mlinemargin = appearance.getdimension(
            r.styleable.nodepointprocessbar_linemargin, mlinemargin
        )
        initpaint()
        show(mtextlist, mprocessindexset)
    }
    /**
     * 初始化画笔属性
     */
    private fun initpaint() {
        // 设置文字画笔
        mtextpaint = textpaint()
        mtextpaint.isantialias = true
        mtextpaint.textsize = textsize
        mtextpaint.color = mwaitprocesstextcolor
        // 设置圆圈画笔
        mcirclepaint = paint()
        mcirclepaint.isantialias = true
        mcirclepaint.color = mprocesstextcolor
        mcirclepaint.style = paint.style.stroke
        mcirclepaint.strokewidth = mcircleborder
    }
    override fun onmeasure(widthmeasurespec: int, heightmeasurespec: int) {
        super.onmeasure(widthmeasurespec, heightmeasurespec)
        val widthmode = measurespec.getmode(widthmeasurespec)
        val heightmode = measurespec.getmode(heightmeasurespec)
        log.d(tag, "---------------onmeasure()")
        measuretext()
        val widthsize = if (measurespec.exactly == widthmode) {
            measurespec.getsize(widthmeasurespec)
        } else {
            mcontentwidth.toint()
        }
        val heightsize = if (measurespec.exactly == heightmode) {
            measurespec.getsize(heightmeasurespec)
        } else {
            mcontentheight.toint()
        }
        /**
         * 设置控件的宽高
         */
        setmeasureddimension(widthsize, heightsize)
        calccontentwidthheight()
    }
    /**
     * 测量文字的长宽,将文字视为rect矩形
     */
    private fun measuretext() {
        log.d(tag, "---------------measuretext()")
        mtextboundlist.clear()
        for (name in mtextlist) {
            mrect.setempty()
            mtextpaint.gettextbounds(name, 0, name.length, mrect)
            mtextboundlist.add(mrect)
        }
    }
    /**
     * 获取内容的高度,如果控件的宽度小于内容的宽度,意味着一行放不下了,文字的大小减小1sp,重新测量文字的宽高,重新
     */
    private fun calccontentwidthheight() {
        // 一开始没有传递文字的
        mcontentheight = if (mtextboundlist.isnotempty()) {
            mcircleradius * 2 + mtextcirclemargin + mrect.height() + getbaseline(mtextpaint)
        } else {
            mtextpaint.gettextbounds("中", 0, 1, mrect)
            mcircleradius * 2 + mtextcirclemargin + mrect.height() + getbaseline(mtextpaint)
        }
        if (measuredwidth == 0 || mtextboundlist.isempty()) {
            return
        }
        mcontentwidth = 0f
        for (rect in mtextboundlist) {
            mcontentwidth += rect.width()
        }
        log.d(tag, "---------------measuredwidth=$measuredwidth,mcontentwidth=$mcontentwidth")
        // 如果控件的宽度小于内容的宽度加文本的边距,意味着一行放不下了,文字的大小减小1sp,重新测量文字的宽高后,设置控件得高度
        // 如果控件的宽度大于内容的宽度加文本的边距,意味着一行放得下,设置控件得高度
        if (measuredwidth - mcontentwidth < (mtextleftrightmargin * (mtextlist.size - 1))) {
            mtextpaint.textsize = mtextpaint.textsize - 1f.sp2px()
            measuretext()
            calccontentwidthheight()
            return
        }
        setmeasureddimension(measuredwidth, mcontentheight.toint())
    }
    override fun ondraw(canvas: canvas) {
        //若未设置节点标题或者选中项的列表,则取消绘制
        if (mtextlist.isempty() || mtextboundlist.isempty()) {
            return
        }
        //画灰色圆圈的个数
        mcirclecount = mtextlist.size
        // 每一段文字的y坐标
        val texty = getbaseline(mtextpaint) + height / 2 + margintop / 2
        mcirclepaint.strokewidth = mcircleborder
        //绘制文字和圆形
        for (i in 0 until mcirclecount) {
            if (mprocessindexset.contains(i)) {
                // 正在处理中
                if (mprocessindexset.size == i + 1) {
                    mcirclepaint.style = paint.style.fill
                    // 正在处理中的文字颜色
                    mtextpaint.color = mprocesstextcolor
                    mcirclepaint.color = mprocesstextcolor
                } else {
                    //处理完成圆圈空心
                    mcirclepaint.style = paint.style.stroke
                    //处理完成文字颜色
                    mtextpaint.color = mcompletetextcolor
                    mcirclepaint.color = mprocesstextcolor
                }
            } else {
                //待处理
                mcirclepaint.color = mwaitprocesstextcolor
                mcirclepaint.style = paint.style.fill
                mtextpaint.color = mwaitprocesstextcolor
            }
            //每一段文字宽度
            val textwidth = mtextboundlist[i].width()
            // 每一段宽度
            val itemwidth = width * 1f / mcirclecount
            // 每一段文字居中
            // |----text----|----text----|
            //    一段文字       一段文字
            //每一段文字起始的x坐标
            val textx = itemwidth / 2f - textwidth / 2f + i * itemwidth
            canvas.drawtext(mtextlist[i], textx, texty, mtextpaint)
            //每一个圆圈的y坐标
            val circley = height / 2f - mcircleradius - mtextcirclemargin / 2
            //每一个圆圈的x坐标
            val circlex = itemwidth / 2 + i * itemwidth
            canvas.drawcircle(
                circlex,
                circley,
                mcircleradius,
                mcirclepaint
            )
            // 画线,两个圆圈之间一条线段
            mcirclepaint.strokewidth = mlinewidth
            if (i < mcirclecount - 1) {
                //已经处理过的线颜色
                if (mprocessindexset.contains(i + 1)) {
                    mcirclepaint.color = mprocesstextcolor
                } else {
                    // 待处理的线段颜色
                    mcirclepaint.color = mwaitprocesstextcolor
                }
                // 线段起始 x 坐标
                val linestartx = itemwidth * i + itemwidth / 2f + mcircleradius + mlinemargin
                // 线段结束 x 坐标
                val lineendx =
                    itemwidth * i + itemwidth + itemwidth / 2f - mcircleradius - mlinemargin
                canvas.drawline(
                    linestartx,
                    circley,
                    lineendx,
                    circley,
                    mcirclepaint
                )
            }
            log.d("tag", "--------itemwidth=$itemwidth")
        }
        if (isdebug) {
            mcirclepaint.color = color.red
            canvas.drawline(
                0f,
                height / 2f - 1f.dp2px() / 2,
                width * 1f,
                height / 2f + 1f.dp2px() / 2,
                mcirclepaint
            )
        }
    }
    /**
     * 供外部调用,展示内容
     * @param titles 要展示的内容列表
     * @param progressindexset 节点选中项集合
     */
    fun setnodedata(titles: list<string>, progressindexset: set<int>) {
        mtextlist = titles
        mprocessindexset = progressindexset
        measuretext()
        calccontentwidthheight()
        invalidate()
    }
    /**
     * 获取文字的基线
     */
    private fun getbaseline(p: paint): float {
        val fontmetrics: paint.fontmetrics = p.fontmetrics
        return (fontmetrics.bottom - fontmetrics.top) - fontmetrics.descent
    }
}

这里的show()方法用于展示内容,第一个参数要展示的内容列表,第二个参数代表节点选中项集合,紧接着测量文字的宽高,调用这个方法calccontentwidthheight()获取文字的高度,然后设置文字的宽高,代码中的注释写的很详细,我们就不再细说了

声明下style

attrs.xml

 <declare-styleable name="nodepointprocessbar">
        <attr name="completedtextcolor" format="color" />
        <attr name="processtextcolor" format="color" />
        <attr name="waitprocesstextcolor" format="color" />
        <attr name="textcirclemargin" format="dimension" />
        <attr name="textleftrightmargin" format="dimension" />
        <attr name="npbcircleradius" format="dimension" />
        <attr name="circleborder" format="dimension" />
        <attr name="linewidth" format="dimension" />
        <attr name="linemargin" format="dimension" />
        <attr name="isdebug" format="boolean" />
    </declare-styleable>

新建一个extendutil.kt文件

fun int.sp2px(): int {
    val displaymetrics = resources.getsystem().displaymetrics
    return typedvalue.applydimension(typedvalue.complex_unit_sp, this.tofloat(), displaymetrics)
        .toint()
}
fun float.sp2px(): float {
    val displaymetrics = resources.getsystem().displaymetrics
    return typedvalue.applydimension(typedvalue.complex_unit_sp, this, displaymetrics)
}
fun int.dp2px(): int {
    val displaymetrics = resources.getsystem().displaymetrics
    return typedvalue.applydimension(typedvalue.complex_unit_dip, this.tofloat(), displaymetrics)
        .toint()
}
fun float.dp2px(): float {
    val displaymetrics = resources.getsystem().displaymetrics
    return typedvalue.applydimension(typedvalue.complex_unit_dip, this, displaymetrics)
}

接着创建布局文件

activity_node_progress_bar.xml

<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.constraintlayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    <cn.wwj.customview.widget.nodepointprocessbar
        android:id="@+id/nodepointpb"
        android:layout_width="0dp"
        android:layout_height="wrap_content"
        android:textsize="18sp"
        android:layout_marginhorizontal="10dp"
        app:layout_constraintbottom_tobottomof="parent"
        app:layout_constraintleft_toleftof="parent"
        app:isdebug="false"
        app:linewidth="1dp"
        app:linemargin="5dp"
        app:layout_constraintright_torightof="parent"
        app:layout_constrainttop_totopof="parent" />
</androidx.constraintlayout.widget.constraintlayout>

再activity中使用它

package cn.wwj.customview
import android.os.bundle
import android.os.handler
import android.os.looper
import androidx.appcompat.app.appcompatactivity
import cn.wwj.customview.widget.nodepointprocessbar
/**
 * 节点进度activity
 */
class nodeprogressbaractivity : appcompatactivity() {
    /**
     * 数据结合
     */
    private val mtextlist: list<string> = mutablelistof("提交申请", "商家处理", "寄回商品", "商家退款", "退款成功")
    /**
     * 正在处理的节点索引结合
     */
    private var mprogressindexset: set<int> = mutablesetof(0, 1,2,3,4,6)
    override fun oncreate(savedinstancestate: bundle?) {
        super.oncreate(savedinstancestate)
        setcontentview(r.layout.activity_node_progress_bar)
        val nodepointpb: nodepointprocessbar = findviewbyid(r.id.nodepointpb)
        handler(looper.getmainlooper()).postdelayed({
            nodepointpb.setnodedata(mtextlist, mprogressindexset)
        }, 1000)
    }
}

mtextlist数据集合

mprogressindexset正在处理的节点索引结合,创建handler对象模拟调用网络接口,1秒后返回数据

节点全部处理完成.png

项目地址,在customview这模块下

https://github.com/githubwwj/myandroid

以上就是android新建水平节点进度条示例的详细内容,更多关于android水平节点进度条的资料请关注萬仟网其它相关文章!

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

支付宝红包二维码

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

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

支付宝红包

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

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

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

分享给朋友:

相关文章

50句非常励志的短句,正能量的励志句子
50句非常励志的短句,正能量的励志句子

1、人非要经历一番不同平时的劫难才能脱胎换骨,成为真正能解决问题的人。2、务须咬牙厉志,蓄其气而长其志,切不可恭然自馁也3、生活比电影狠多了,从来不给弱者安排大逆转的情节。4、即使赚得了全世界,却失去了自己,又有什么意义呢?5、不要把时间、...

低成本创业好项目,这个可日赚几千元
低成本创业好项目,这个可日赚几千元

这几年创业项目也变得越来越多了,大家都知道,现在靠打工是挣不了什么钱的,所以,很多人宁愿自己创业不想打工。那么,低成本创业项目有哪些呢?下面小编马上为大家推荐一个低成本创业项目,如果你有资源的话,也可以免费去推广操作,做好了日赚几千也是很容...

智能电视和普通电视的区别,智能电视好还是普通电视好?
智能电视和普通电视的区别,智能电视好还是普通电视好?

好多人对智能电视和普通的区别还分不大清楚,今天小编就将智能电视和和普通电视做个简单明了的介绍,希望对大家有所帮助。简单的讲,就是智能电视可以看直播电视,也可以点播一些网络电视来看,这个就是最大的区别。当然,有些智能电视还有储存功能,比如,可...

用白面书生造句,看看这七句有哪句适合你?
用白面书生造句,看看这七句有哪句适合你?

1、他是个手无缚鸡之力的白面书生。2、别看他文绉绉的像个白面书生,一上足球场勇猛非凡,踢起球来可狠了。3、那个白面书生经不起严刑拷打,向敌人投降了。4、我一个白面书生,又手无寸铁,怎敢和那几个黑脸大汉较真呢!5、新时代的大学生不是白面书生,...

分享29句励志上进的名言名句
分享29句励志上进的名言名句

1、一个能思虑的人,才实是一个力量无边的人。2、以微笑面对人生。3、一个人最伤心的事情无过于良心的死灭。4、障碍与失败,是通往成功最稳靠的踏脚石,肯研究利用它们,便能从失败中培养出成功。5、金钱损失了还能挽回,一旦失去信誉就很难挽回。6、一...

古罗马大诗人贺拉斯的两句至理名言分享
古罗马大诗人贺拉斯的两句至理名言分享

1、苦难显才华,好运隐天资。2、人人皆有弱点,谁若想要寻个没有缺点的朋友,就永远找不着他所要追寻的。...

发表评论

访客

◎欢迎参与讨论,请在这里发表您的看法和观点。