Kotlin的自定义View,实现带弧形的进度条,查缺补漏

android:id=“@+id/capv_first”

android:layout_width=“0dp”

android:layout_height=“30dp”

android:layout_marginStart=“16dp”

android:layout_marginTop=“16dp”

android:layout_marginEnd=“16dp”

app:capv_background_color=“@color/circular_arc_progress_view_first_background_color”

app:capv_is_show_progress_text=“true”

app:capv_percent=“0.8”

app:capv_progress_color=“@color/circular_arc_progress_view_first_progress_color”

app:layout_constraintEnd_toEndOf=“parent”

app:layout_constraintStart_toStartOf=“parent”

app:layout_constraintTop_toTopOf=“parent” />

Kotlin

findViewById(R.id.capv_first).startAnimator(duration = 2000)

Java

((CircularArcProgressView) findViewById(R.id.capv_first)).startAnimator(2000);

/ 源码分析 /


定义自定义属性,据此写出对应的获取自定义属性的代码,并且暴露一些需要用户设置的方法,代码如下:

/**

  • Set percent to show the progress.

*/

var percent: Float = 0f

set(value) {

var percent = value

if (percent < 0f) {

percent = 0f

} else if (percent > 1f) {

percent = 1f

}

if (percent != field) {

field = percent

invalidate()

}

}

init {

attrs?.let { set ->

context.obtainStyledAttributes(set, R.styleable.CircularArcProgressView).apply {

bgColor =

getColor(R.styleable.CircularArcProgressView_capv_background_color, Color.BLACK)

progressColor =

getColor(R.styleable.CircularArcProgressView_capv_progress_color, Color.RED)

progressTextColor =

getColor(

R.styleable.CircularArcProgressView_capv_progress_text_color,

Color.WHITE

)

getFloat(R.styleable.CircularArcProgressView_capv_percent, 0f).let {

percent = it

}

isShowProgressText =

getBoolean(

R.styleable.CircularArcProgressView_capv_is_show_progress_text,

false

)

recycle()

}

}

}

根据用户设置的宽高去绘制一个半径为高度一半的圆角矩形,注意要对padding属性进行处理,这部分就是背景,代码如下:

val halfHeight = height / 2f

val saveCount = canvas.saveLayer(0f, 0f, width.toFloat(), height.toFloat(), null)

// Draw background.

backgroundRectF.left = paddingStart.toFloat()

backgroundRectF.top = paddingTop.toFloat()

backgroundRectF.right = width - paddingEnd.toFloat()

backgroundRectF.bottom = height - paddingBottom.toFloat()

canvas.drawRoundRect(backgroundRectF, halfHeight, halfHeight, backgroundPaint)

在背景圆角矩形的左边绘制另外一个半径为高度一半的圆角矩形,宽高和背景圆角矩形一样,但是左右坐标会随着percent的增加而增加,绘制完毕后的表现就是往右移动,然后利用PorterDuffXfermode处理重叠部分,这部分就是进度,代码如下:

private val progressTextPaint by lazy {

TextPaint().apply {

isAntiAlias = true

isDither = true

style = Paint.Style.FILL

color = progressTextColor

}

}

// Draw progress.

progressRectF.left = -backgroundRectF.width() + percent * width

progressRectF.top = backgroundRectF.top

progressRectF.right = progressRectF.left + backgroundRectF.width()

progressRectF.bottom = backgroundRectF.bottom

canvas.drawRoundRect(progressRectF, halfHeight, halfHeight, progressPaint)

canvas.restoreToCount(saveCount)

根据用户需要绘制一个百分比文本,左右坐标也是随着percent增加而增加,绘制完毕后的表现也是向右移动,不过是位于进度条弧形的左边,注意要准确测量文字的宽高,代码如下:

if (isShowProgressText && percent >= 0.1f) {

progressTextPaint.run {

textSize = halfHeight

fontMetrics.let {

val progressText = (percent * 100).toInt().toString() + “%”

canvas.drawText(

progressText,

percent * width - progressTextPaint.measureText(progressText) - height / 5f,

halfHeight - it.descent + (it.descent - it.ascent) / 2f,

progressTextPaint

)

}

}

}

暴露一个设置动画的方法。

/**

  • Start animator.

  • @param timeInterpolator the interpolator to be used by this animation. The default value is

  • android.view.animation.AccelerateInterpolator.

  • @param duration the length of the animation.

*/

@JvmOverloads

fun startAnimator(

timeInterpolator: TimeInterpolator? = AccelerateInterpolator(),

duration: Long

) =

with(ObjectAnimator.ofFloat(this, “percent”, 0f, percent)) {

interpolator = timeInterpolator

this.duration = duration

start()

}

/ PorterDuff.Mode /


来源

为什么叫PorterDuff呢?其实是两个人名来的,一个叫Thomas Porter,另一个叫Tom Duff,他们在1984年7月发表了Compositing Digital Images,描述了12个合成运算符。它们控制着要渲染的图像和渲染目标的内容组成的颜色,然后这个类还提供了除了那12种的其他几种混合模式,但是这些不是由这两人定义的,只是为了方便才在此类中,所以总共有18种。

源码

我们可以看下PorterDuff这个类,里面有个枚举Mode,代码如下:

public enum Mode {

CLEAR (0),

SRC (1),

DST (2),

SRC_OVER (3),

DST_OVER (4),

SRC_IN (5),

DST_IN (6),

SRC_OUT (7),

DST_OUT (8),

SRC_ATOP (9),

DST_ATOP (10),

XOR (11),

DARKEN (16),

LIGHTEN (17),

MULTIPLY (13),

SCREEN (14),

ADD (12),

OVERLAY (15);

Mode(int nativeInt) {

this.nativeInt = nativeInt;

}

/**

  • @hide

*/

@UnsupportedAppUsage

public final int nativeInt;

}

PorterDuff总共有18种模式,以下展示了这些模式对应的名字、图片和描述,可以点开图片查看,图片如下:

/ 延迟属性Lazy /


这个控件的代码也用上了延迟属性Lazy,代码如下:

private val progressTextPaint by lazy {

TextPaint().apply {

isAntiAlias = true

isDither = true

style = Paint.Style.FILL

color = progressTextColor

}

}

我们可以看到,lazy函数是接受一个Lambda表达式,如果函数最后一个参数是Lambda表达式的话,可以提到小括号外边,并且小括号也可以省略;调用延迟属性有这样的特征,第一次拿到属性的值(调用get方法)会执行已传递给函数的Lambda表达式并且记录结果,后续调用get()只是返回记录的结果。我们可以看下源码,提供了三个函数。

lazy(initializer: () -> T)

public actual fun lazy(initializer: () -> T): Lazy = SynchronizedLazyImpl(initializer)

这个函数接受一个Lambda表达式,并且返回Lazy,并且调用SynchronizedLazyImpl函数,而且我们可以得知多个线程去调用这个lazy函数是安全的,代码如下:

, Serializable {

private var initializer: (() -> T)? = initializer

@Volatile private var _value: Any? = UNINITIALIZED_VALUE

// final field is required to enable safe publication of constructed instance

private val lock = lock ?: this

override val value: T

get() {

val _v1 = _value
自我介绍一下,小编13年上海交大毕业,曾经在小公司待过,也去过华为、OPPO等大厂,18年进入阿里一直到现在。

深知大多数初中级Android工程师,想要提升技能,往往是自己摸索成长或者是报班学习,但对于培训机构动则近万的学费,着实压力不小。自己不成体系的自学效果低效又漫长,而且极易碰到天花板技术停滞不前!

因此收集整理了一份《2024年Android移动开发全套学习资料》,初衷也很简单,就是希望能够帮助到想自学提升又不知道该从何学起的朋友,同时减轻大家的负担。

img

img

img

img

既有适合小白学习的零基础资料,也有适合3年以上经验的小伙伴深入学习提升的进阶课程,基本涵盖了95%以上Android开发知识点,真正体系化!

由于文件比较大,这里只是将部分目录截图出来,每个节点里面都包含大厂面经、学习笔记、源码讲义、实战项目、讲解视频,并且会持续更新!

如果你觉得这些内容对你有帮助,可以扫码获取!!(备注:Android)

小结

有了这么多优秀的开发工具,可以做出更高质量的Android应用。

当然了,“打铁还需自身硬”,想要写出优秀的代码,最重要的一点还是自身的技术水平,不然用再好的工具也不能发挥出它的全部实力。

在这里我也分享一份大佬自己收录整理的Android学习PDF+架构视频+面试文档+源码笔记,还有高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料这些都是我闲暇还会反复翻阅的精品资料。在脑图中,每个知识点专题都配有相对应的实战项目,可以有效的帮助大家掌握知识点。

总之也是在这里帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

助,可以扫码获取!!(备注:Android)**

小结

有了这么多优秀的开发工具,可以做出更高质量的Android应用。

当然了,“打铁还需自身硬”,想要写出优秀的代码,最重要的一点还是自身的技术水平,不然用再好的工具也不能发挥出它的全部实力。

在这里我也分享一份大佬自己收录整理的Android学习PDF+架构视频+面试文档+源码笔记,还有高级架构技术进阶脑图、Android开发面试专题资料,高级进阶架构资料这些都是我闲暇还会反复翻阅的精品资料。在脑图中,每个知识点专题都配有相对应的实战项目,可以有效的帮助大家掌握知识点。

总之也是在这里帮助大家学习提升进阶,也节省大家在网上搜索资料的时间来学习,也可以分享给身边好友一起学习

《互联网大厂面试真题解析、进阶开发核心学习笔记、全套讲解视频、实战项目源码讲义》点击传送门即可获取!

  • 24
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值