Android技术分享|【自定义View】实现Material Design的Loading效果
预期效果
实现思路
分析一下这个动画,效果应该是通过两个动画来实现的。 * 一个不停变速伸缩的扇形动画 * 一个固定速度的旋转动画
扇形可以通过canvas#drawArc
来实现
旋转动画可以用setMatrix
实现
圆角背景可以通过canvas#drawRoundRect
实现
还需要一个计时器来实现动画效果
这个View最好能够更方便的修改样式,所以需要定义一个declare-styleable,方便通过布局来修改属性。 这些元素应该包括: * 最底层的卡片颜色 * 卡片内变局 * 内部长条的颜色 * 长条的粗细 * 长条的距离中心的半径 * 字体大小 * 字体颜色
因为用到动画,避免掉帧,最好离屏绘制到缓冲帧上,再通知view绘制缓冲帧。
代码实现
-
定义一下styleable
xml <declare-styleable name="MaterialLoadingProgress"> <attr name="loadingProgress_circleRadius" format="dimension" /> <attr name="loadingProgress_cardColor" format="color" /> <attr name="loadingProgress_cardPadding" format="dimension" /> <attr name="loadingProgress_strokeWidth" format="dimension" /> <attr name="loadingProgress_strokeColor" format="color" /> <attr name="loadingProgress_text" format="string" /> <attr name="loadingProgress_textSize" format="dimension" /> <attr name="loadingProgress_textColor" format="color" /> </declare-styleable>
-
在代码中解析styleable
Kotlin init { val defCircleRadius = context.resources.getDimension(R.dimen.dp24) val defCardColor = Color.WHITE val defCardPadding = context.resources.getDimension(R.dimen.dp12) val defStrokeWidth = context.resources.getDimension(R.dimen.dp5) val defStrokeColor = ContextCompat.getColor(context, R.color.teal_200) val defTextSize = context.resources.getDimension(R.dimen.sp14) val defTextColor = Color.parseColor("#333333") if (attrs != null) { val attrSet = context.resources.obtainAttributes(attrs, R.styleable.MaterialLoadingProgress) circleRadius = attrSet.getDimension(R.styleable.MaterialLoadingProgress_loadingProgress_circleRadius, defCircleRadius) cardColor = attrSet.getColor(R.styleable.MaterialLoadingProgress_loadingProgress_cardColor, defCardColor) cardPadding = attrSet.getDimension(R.styleable.MaterialLoadingProgress_loadingProgress_cardPadding, defCardPadding) strokeWidth = attrSet.getDimension(R.styleable.MaterialLoadingProgress_loadingProgress_strokeWidth, defStrokeWidth) strokeColor = attrSet.getColor(R.styleable.MaterialLoadingProgress_loadingProgress_strokeColor, defStrokeColor) text = attrSet.getString(R.styleable.MaterialLoadingProgress_loadingProgress_text) ?: "" textSize = attrSet.getDimension(R.styleable.MaterialLoadingProgress_loadingProgress_textSize, defTextSize) textColor = attrSet.getColor(R.styleable.MaterialLoadingProgress_loadingProgress_textColor, defTextColor) attrSet.recycle() } else { circleRadius = defCircleRadius cardColor = defCardColor cardPadding = defCardPadding strokeWidth = defStrokeWidth strokeColor = defStrokeColor textSize = defTextSize textColor = defTextColor } paint.textSize = textSize if (text.isNotBlank()) textWidth = paint.measureText(text) }
-
实现一个计时器,再定义一个数据类型来存储动画相关数据,还有一个动画插值器
Timer定时器 ```Kotlin private fun startTimerTask() { val t = Timer() t.schedule(object : TimerTask() { override fun run() { if (taskList.isEmpty()) return
val taskIterator = taskList.iterator() while (taskIterator.hasNext()) { val task = taskIterator.next()
task.progress += 17 if (task.progress > task.duration) { task.progress = task.duration } if (task.progress == task.duration) { if (!task.convert) { task.startAngle -= 40 if (task.startAngle < 0) task.startAngle += 360 } task.progress = 0 task.convert = !task.convert } task.progressFloat = task.progress / task.duration.toFloat() task.interpolatorProgress = interpolator(task.progress / task.duration.toFloat()) task.currentAngle = (320 * task.interpolatorProgress).toInt() post { task.onProgress(task) }
} } }, 0, 16) timer = t } ```
定义一个数据模型
Kotlin private data class AnimTask( var startAngle: Int = 0,// 扇形绘制起点 val duration: Int = 700,// 动画时间 var progress: Int = 0,// 动画已执行时间 var interpolatorProgress: Float = 0f,// 插值器计算后的值,取值0.0f ~ 1.0f var progressFloat: Float = 0f,// 取值0.0f ~ 1.0f var convert: Boolean = false,// 判断扇形的绘制进程,为true时反向绘制 var currentAngle: Int = 0,// 绘制扇形使用 val onProgress: (AnimTask) -> Unit// 计算完当前帧数据后的回调 )
动画插值器Kotlin private fun interpolator(x: Float) = x * x * (3 - 2 * 2)
4. 定义初始化缓冲帧 此方法在外部调用显示loading时调用即可,调用前需判断是否已经初始化Kotlin private fun initCanvas() { bufferBitmap = Bitmap.createBitmap(measuredWidth, measuredHeight, Bitmap.Config.ARGB_8888) bufferCanvas = Canvas(bufferBitmap) }
5. 实现扇形的绘制 ```Kotlin private fun drawFrame(task: AnimTask) { bufferBitmap.eraseColor(Color.TRANSPARENT)
val centerX = measuredWidth.shr(1) val centerY = measuredHeight.shr(1) rectF.set( centerX - circleRadius, centerY - circleRadius, centerX + circleRadius, centerY + circleRadius ) paint.strokeWidth = strokeWidth paint.color = strokeColor paint.strokeCap = Paint.Cap.ROUND paint.style = Paint.Style.STROKE
// 这里的判断,对应扇形逐渐延长、及逐渐缩短
if (task.convert) {
bufferCanvas.drawArc(
rectF, task.startAngle.toFloat(), -(320.0f - task.currentAngle.toFloat()), false, paint
)
} else {
bufferCanvas.drawArc(
rectF, task.startAngle.toFloat(), task.currentAngle.toFloat(), false, paint
)
}
invalidate()
}
6. 实现扇形整体缓慢转圈
Kotlin
private fun drawRotation(task: AnimTask) {
val centerX = measuredWidth.shr(1)
val centerY = measuredHeight.shr(1)
bufferMatrix.reset()
bufferMatrix.postRotate(task.progressFloat * 360f, centerX.toFloat(), centerY.toFloat())
bufferCanvas.setMatrix(bufferMatrix)
}
```
一定要记得调用
matrix#reset
否则效果就会像这样 XD:
到这里,核心功能基本就完成了。
- 定义一个
showProgress
方法以及dismissProgress
方法,方便外部使用展示 ```Kotlin fun showProgress() { if (showing) return
if (!this::bufferBitmap.isInitialized) { initCanvas() }
taskList.add(AnimTask { drawFrame(it) }) taskList.add(AnimTask(duration = 5000) { drawRotation(it) }) startTimerTask() showing = true visibility = VISIBLE } ```
关闭 ```Kotlin fun dismissProgress() { if (!showing) return
purgeTimer() showing = false visibility = GONE } ```
最后看一下View#onDraw
的实现:
```Kotlin
override fun onDraw(canvas: Canvas) {
val centerX = measuredWidth.shr(1)
val centerY = measuredHeight.shr(1)
val rectHalfDimension = if (circleRadius > textWidth / 2f) circleRadius + cardPadding else textWidth / 2f + cardPadding rectF.set( centerX - rectHalfDimension, centerY - rectHalfDimension, centerX + rectHalfDimension, if (text.isNotBlank()) centerY + paint.textSize + rectHalfDimension else centerY + rectHalfDimension )
paint.color = cardColor paint.style = Paint.Style.FILL canvas.drawRoundRect(rectF, 12f, 12f, paint)
if (text.isNotBlank()) { val dx = measuredWidth.shr(1) - textWidth / 2 paint.color = textColor canvas.drawText(text, dx, rectF.bottom - paint.textSize, paint) }
if (this::bufferBitmap.isInitialized) canvas.drawBitmap(bufferBitmap, bufferMatrix, paint) } ```
源代码请移步:ARCallPlus。
- Android技术分享| ViewPager2离屏加载,实现抖音上下视频滑动
- Android技术分享| Activity 过渡动画 — 让切换更加炫酷
- Linux下玩转nginx系列(七)---nginx如何实现限流功能
- 技术分享| 如何部署安装分布式序列号生成器系统
- web技术分享| 【地图】实现自定义的轨迹回放
- 解决方案| 快对讲综合调度系统
- 实时消息RTM| 多活架构中的数据一致性问题
- Android技术分享| Context浅析
- Android技术分享| Context浅析
- 屏幕共享的实现与应用
- 技术分析| 即时通讯和实时通讯的区别
- IOS技术分享| ARCallPlus 开源项目(二)
- Android技术分享| Android 中部分内存泄漏示例及解决方案
- Android技术分享| 安卓3行代码,实现整套音视频通话功能
- 行业分析| 快对讲Poc方案的优势
- Android技术分享|【自定义View】实现Material Design的Loading效果
- IOS技术分享| ARCallPlus 开源项目(一)
- web技术分享| WebRTC控制摄像机平移、倾斜和缩放
- Android技术分享| anyLive 开源项目
- Android技术分享| 【Android 自定义View】多人视频通话控件