本文主要是介绍Android基础控件——TextView的自定义,实现圈圈进度条的倒计时,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!
前言
在开发中,正常的进度条都是用ProgressBar实现的,但是遇到需要文本的进度条和光滑动画的进度条时,用ProgressBar实现起来就有点吃力,这里可以通过TextView+ValueAnimator
的方式来实现
本例子中实现效果如下
实现思路
- 继承AppCompatTextView
- 通过drawRoundRect的方式画内圈椭圆
- 通过drawPath+PathMeasure+ValueAnimator的方式画外圈的倒计时椭圆
实现分析
1、快速使用
在xml直接使用
<com.example.uitest.RoundRectCountDown.RoundRectCountDownandroid:id="@+id/pb"android:layout_width="80dp"android:layout_height="36dp"android:layout_centerInParent="true"android:gravity="center"android:text="0.0s"android:textColor="#04B4E3"android:textSize="12sp" />
在代码启动倒计时
val pb = findViewById<RoundRectCountDown>(R.id.pb)
pb.startAnimation(20, object : AnimatorListenerAdapter() {override fun onAnimationEnd(animation: Animator?) {}
})
2、初始化属性
定义想要的属性值,并初始化画笔
//圆角
private val ROUND = 20f
//倒计时外框宽度
private val STROKE_WIDTH = 4f//动画相关
private var mValueAnimator: ValueAnimator? = null
private var mAnimatorValue = 0f//内圈用Rect绘制椭圆,外圈用Path来绘制椭圆
private var mInSizeRectF = RectF()
private var mOutSizePath = Path()//相当于辅助外圈框用的工具类
private val mOutSizeTempPath = Path()
private val mOutSizePathMeasure = PathMeasure()
private var mOutSizePathLength = 0f//外圈和内圈的画笔
private val mOutSizePaint = Paint()
private val mInSizePaint = Paint()constructor(context: Context) : super(context)constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)init {initPaint()
}private fun initPaint() {mOutSizePaint.style = Paint.Style.STROKEmOutSizePaint.isAntiAlias = truemOutSizePaint.strokeWidth = STROKE_WIDTHmOutSizePaint.strokeCap = Paint.Cap.ROUNDmOutSizePaint.color = Color.parseColor("#04B4E3")mInSizePaint.style = Paint.Style.FILLmInSizePaint.isAntiAlias = truemInSizePaint.color = Color.parseColor("#0B101F")
}
在onLayout回调中能拿到宽高,从而去初始化对应的外圈椭圆,主要是定义外圈椭圆的长度和Path
override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {super.onLayout(changed, left, top, right, bottom)buildRectPath()
}private fun buildRectPath() {//定一个矩形,四个顶点分别在自身大小(0,0,width,height)的范围内往内缩一个框框的大小mInSizeRectF.set(STROKE_WIDTH, STROKE_WIDTH, width - STROKE_WIDTH, height - STROKE_WIDTH)//定义一个Path来形容椭圆mOutSizeTempPath.addRoundRect(mInSizeRectF, ROUND, ROUND, Path.Direction.CW)//定义一个PathMeasure来加载PathmOutSizePathMeasure.setPath(mOutSizeTempPath, true)//获取Path的长度mOutSizePathLength = mOutSizePathMeasure.length
}
3、绘制内圈和外圈
通过复写onDraw
方法,绘制内圈椭圆和外圈椭圆,这里就是让外圈椭圆的起点不断接近终点,就完成了倒计时
override fun onDraw(canvas: Canvas?) {drawRoundRect(canvas)drawRoundRectStroke(canvas)super.onDraw(canvas)
}/*** 绘制椭圆内圈背景*/
private fun drawRoundRect(canvas: Canvas?) {canvas?.drawRoundRect(mInSizeRectF, ROUND, ROUND, mInSizePaint)
}/*** 绘制椭圆外圈条框*/
private fun drawRoundRectStroke(canvas: Canvas?) {mOutSizePath.reset()//获取当前外圈椭圆的起点,终点是整个外圈椭圆的长度val start = mOutSizePathLength * mAnimatorValue//通过起点和终点的连线,绘制出外圈椭圆的路径mOutSizePathMeasure.getSegment(start, mOutSizePathLength, mOutSizePath, true)canvas?.drawPath(mOutSizePath, mOutSizePaint)
}
4、开始和结束动画
启动动画后,获取0->1的动画的动画值,从而刷新界面
/*** 开始动画** 0->1 的动画*/
fun startAnimation(time: Int, listener: AnimatorListenerAdapter) {mValueAnimator = ValueAnimator.ofFloat(0f, 1f)mValueAnimator?.interpolator = LinearInterpolator()mValueAnimator?.addUpdateListener { it ->mAnimatorValue = it.animatedValue as Floatthis.text = "${time - (time * mAnimatorValue).toInt()}s"invalidate()}mValueAnimator?.addListener(listener)mValueAnimator?.duration = (time * 1000).toLong()mValueAnimator?.start()
}fun stopAnimation() {mValueAnimator?.cancel()
}
5、源码
package com.example.uitest.RoundRectCountDownimport android.animation.AnimatorListenerAdapter
import android.animation.ValueAnimator
import android.content.Context
import android.graphics.*
import android.util.AttributeSet
import android.view.animation.LinearInterpolatorclass RoundRectCountDown : androidx.appcompat.widget.AppCompatTextView {//圆角private val ROUND = 20f//倒计时外框宽度private val STROKE_WIDTH = 4f//动画相关private var mValueAnimator: ValueAnimator? = nullprivate var mAnimatorValue = 0f//内圈用Rect绘制椭圆,外圈用Path来绘制椭圆private var mInSizeRectF = RectF()private var mOutSizePath = Path()//相当于辅助外圈框用的工具类private val mOutSizeTempPath = Path()private val mOutSizePathMeasure = PathMeasure()private var mOutSizePathLength = 0f//外圈和内圈的画笔private val mOutSizePaint = Paint()private val mInSizePaint = Paint()constructor(context: Context) : super(context)constructor(context: Context, attrs: AttributeSet?) : super(context, attrs)init {initPaint()}private fun initPaint() {mOutSizePaint.style = Paint.Style.STROKEmOutSizePaint.isAntiAlias = truemOutSizePaint.strokeWidth = STROKE_WIDTHmOutSizePaint.strokeCap = Paint.Cap.ROUNDmOutSizePaint.color = Color.parseColor("#04B4E3")mInSizePaint.style = Paint.Style.FILLmInSizePaint.isAntiAlias = truemInSizePaint.color = Color.parseColor("#0B101F")}override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) {super.onLayout(changed, left, top, right, bottom)buildRectPath()}private fun buildRectPath() {//定一个矩形,四个顶点分别在自身大小(0,0,width,height)的范围内往内缩一个框框的大小mInSizeRectF.set(STROKE_WIDTH, STROKE_WIDTH, width - STROKE_WIDTH, height - STROKE_WIDTH)//定义一个Path来形容椭圆mOutSizeTempPath.addRoundRect(mInSizeRectF, ROUND, ROUND, Path.Direction.CW)//定义一个PathMeasure来加载PathmOutSizePathMeasure.setPath(mOutSizeTempPath, true)//获取Path的长度mOutSizePathLength = mOutSizePathMeasure.length}override fun onDraw(canvas: Canvas?) {drawRoundRect(canvas)drawRoundRectStroke(canvas)super.onDraw(canvas)}/*** 绘制椭圆内圈背景*/private fun drawRoundRect(canvas: Canvas?) {canvas?.drawRoundRect(mInSizeRectF, ROUND, ROUND, mInSizePaint)}/*** 绘制椭圆外圈条框*/private fun drawRoundRectStroke(canvas: Canvas?) {mOutSizePath.reset()//获取当前外圈椭圆的起点,终点是整个外圈椭圆的长度val start = mOutSizePathLength * mAnimatorValue//通过起点和终点的连线,绘制出外圈椭圆的路径mOutSizePathMeasure.getSegment(start, mOutSizePathLength, mOutSizePath, true)canvas?.drawPath(mOutSizePath, mOutSizePaint)}/*** 开始动画** 0->1 的动画*/fun startAnimation(time: Int, listener: AnimatorListenerAdapter) {mValueAnimator = ValueAnimator.ofFloat(0f, 1f)mValueAnimator?.interpolator = LinearInterpolator()mValueAnimator?.addUpdateListener { it ->mAnimatorValue = it.animatedValue as Floatthis.text = "${time - (time * mAnimatorValue).toInt()}s"invalidate()}mValueAnimator?.addListener(listener)mValueAnimator?.duration = (time * 1000).toLong()mValueAnimator?.start()}fun stopAnimation() {mValueAnimator?.cancel()}
}
这篇关于Android基础控件——TextView的自定义,实现圈圈进度条的倒计时的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!