【自定义View】Android圆饼进度条

2024-06-07 20:36

本文主要是介绍【自定义View】Android圆饼进度条,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

源码

自定义属性

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="ArcProgressView"><attr name="android:textSize" /><attr name="bgBorderWidth" format="dimension" /><attr name="defaultContentColor" format="color" /><attr name="bgBorderColor" format="color" /><attr name="progressColor" format="color" /><attr name="android:textColor" /></declare-styleable>
</resources>

View(kotlin)源码

package com.example.testimport android.content.Context
import android.content.res.Resources
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Rect
import android.util.AttributeSet
import android.util.TypedValue
import android.view.View
import kotlin.math.roundToIntclass ArcProgressView : View {constructor(context: Context?) : super(context)constructor(context: Context?, attrs: AttributeSet?) : this(context, attrs, 0)constructor(context: Context?, attrs: AttributeSet?, defStyleAttr: Int) : super(context,attrs,defStyleAttr) {val obtainStyledAttributes =context?.obtainStyledAttributes(attrs, R.styleable.ArcProgressView)obtainStyledAttributes?.let {textSize = it.getDimensionPixelSize(R.styleable.ArcProgressView_android_textSize,textSize)strokeWidth = it.getDimensionPixelSize(R.styleable.ArcProgressView_bgBorderWidth,strokeWidth.roundToInt()).toFloat()defaultContentColor = it.getColor(R.styleable.ArcProgressView_defaultContentColor,defaultContentColor)bgBorderColor =it.getColor(R.styleable.ArcProgressView_bgBorderColor, bgBorderColor)progressColor =it.getColor(R.styleable.ArcProgressView_progressColor, progressColor)textColor =it.getColor(R.styleable.ArcProgressView_android_textColor, textColor)}obtainStyledAttributes?.recycle()}val paint = Paint(Paint.ANTI_ALIAS_FLAG)var strokeWidth = dpToPx(0.5f).toFloat();var textSize = spToPx(10f).roundToInt()var defaultContentColor = Color.parseColor("#FFF7ED")var bgBorderColor = Color.parseColor("#FAD29D")var progressColor = Color.parseColor("#FAD29D")var textColor = Color.parseColor("#FA940F")private var progress: Int = 0set(value) {field = valueinvalidate()}override fun onDraw(canvas: Canvas) {super.onDraw(canvas)drawBackground(canvas)drawProgress(canvas)}private fun dpToPx(dp: Float): Int {return (Resources.getSystem().displayMetrics.density * dp + 0.5f).roundToInt()}private fun spToPx(sp: Float): Float {return TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP,sp,Resources.getSystem().displayMetrics)}private fun drawBackground(canvas: Canvas) {paint.strokeWidth = strokeWidthpaint.style = Paint.Style.FILLpaint.color = defaultContentColorcanvas.drawOval(0f + strokeWidth / 2,0f + strokeWidth / 2,width.toFloat() - strokeWidth / 2,height.toFloat() - strokeWidth / 2,paint)paint.style = Paint.Style.STROKEpaint.color = bgBorderColorcanvas.drawOval(0f + strokeWidth / 2,0f + strokeWidth / 2,width.toFloat() - strokeWidth / 2,height.toFloat() - strokeWidth / 2,paint)}val textBound = Rect()private fun drawProgress(canvas: Canvas) {paint.style = Paint.Style.FILLpaint.color = progressColorpaint.textSize = textSize.toFloat()val progressStr = "$progress%"paint.getTextBounds(progressStr, 0, progressStr.length, textBound)val halfTextWidth = textBound.width() / 2fval halfTextHeight = textBound.height() / 2fval progressAngle = progress * 3.6fval halfWidth = width.toFloat() / 2fval halfHeight = height.toFloat() / 2fcanvas.drawArc(0f,0f,width.toFloat(),height.toFloat(),270f,progressAngle,true,paint)paint.isFakeBoldText = truepaint.color = textColorpaint.textAlign = Paint.Align.LEFTcanvas.drawText(progressStr,halfWidth - halfTextWidth,halfHeight - halfTextHeight + textBound.height(),paint)}fun updateProgress(progress: Int) {this.progress = progress}}

Activity布局

  <com.example.test.ArcProgressViewandroid:layout_margin="12dp"android:id="@+id/progress"android:textSize="24sp"android:textColor="@color/black"android:layout_width="100dp"android:layout_height="100dp"/>

Activity更新进度

progress ++
runOnUiThread({progressView.updateProgress(progress)
})

效果图

在这里插入图片描述

这篇关于【自定义View】Android圆饼进度条的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/1040232

相关文章

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32

Android实现打开本地pdf文件的两种方式

《Android实现打开本地pdf文件的两种方式》在现代应用中,PDF格式因其跨平台、稳定性好、展示内容一致等特点,在Android平台上,如何高效地打开本地PDF文件,不仅关系到用户体验,也直接影响... 目录一、项目概述二、相关知识2.1 PDF文件基本概述2.2 android 文件访问与存储权限2.

Android Studio 配置国内镜像源的实现步骤

《AndroidStudio配置国内镜像源的实现步骤》本文主要介绍了AndroidStudio配置国内镜像源的实现步骤,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,... 目录一、修改 hosts,解决 SDK 下载失败的问题二、修改 gradle 地址,解决 gradle

在Android平台上实现消息推送功能

《在Android平台上实现消息推送功能》随着移动互联网应用的飞速发展,消息推送已成为移动应用中不可或缺的功能,在Android平台上,实现消息推送涉及到服务端的消息发送、客户端的消息接收、通知渠道(... 目录一、项目概述二、相关知识介绍2.1 消息推送的基本原理2.2 Firebase Cloud Me

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定