Android自定义带圆点的半圆形进度条

2024-01-22 05:58

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

自定义带圆点的半圆形进度条

仅限用于半圆形,如须要带圆点的圆形进度条,圆点会出现错位现象,此代码仅供,带圆点的圆形进度条有空研究一下!图片效果在下方,
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;/*** 自定义带圆点的进度条*/
public class HalfProgressBar extends View{private int maxProgress = 100;//设置进度条背景宽度private float progressStrokeWidth = 3;//设置进度条进度宽度private float marxArcStorkeWidth = 6;//设置进度条圆点的宽度private float circularDotWidth=15;/*** 画笔对象的引用*/private Paint paint;public synchronized int getProgress() {return progress;}/*** Android提供了Invalidate方法实现界面刷新,但是Invalidate不能直接在线程中调用,因为他是违背了单线程模型:Android UI操作并不是线程安全的,并且这些操作必须在UI线程中调用。*  而postInvalidate()在工作者线程中被调用 使用postInvalidate则比较简单,不需要handler,直接在线程中调用postInvalidate即可。 * @param progress 传过来的进度*/public void setProgress(int progress) {if (progress < 0) {progress = 0;}if (progress > maxProgress) {progress = maxProgress;}if (progress <= maxProgress) {this.progress = progress;postInvalidate();}}/*** 当前进度*/private int progress = 99;private RectF oval;private int roundProgressColor;private int roundColor;private int circularDotColor;public HalfProgressBar(Context context) {super(context);}public HalfProgressBar(Context context, AttributeSet attrs) {super(context, attrs);paint = new Paint();oval = new RectF();//这是自定义view 必须要写的TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.HalfProgressBar);roundProgressColor = mTypedArray.getColor(R.styleable.HalfProgressBar_roundProgressColor1, Color.YELLOW);roundColor=mTypedArray.getColor(R.styleable.HalfProgressBar_roundColor1, Color.YELLOW);circularDotColor=mTypedArray.getColor(R.styleable.HalfProgressBar_circularDotColor1, Color.YELLOW);}public HalfProgressBar(Context context, AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);paint = new Paint();oval = new RectF();TypedArray mTypedArray = context.obtainStyledAttributes(attrs, R.styleable.HalfProgressBar);roundProgressColor = mTypedArray.getColor(R.styleable.HalfProgressBar_roundProgressColor1, Color.YELLOW);roundColor=mTypedArray.getColor(R.styleable.HalfProgressBar_roundColor1, Color.YELLOW);}@Overrideprotected void onDraw(Canvas canvas) {// TODO 自动生成的方法存根super.onDraw(canvas);float width = getWidth();float height = getHeight();paint.setAntiAlias(false); // 设置画笔为抗锯齿paint.setColor(roundColor); // 设置画笔颜色paint.setStrokeWidth(progressStrokeWidth); // 线宽paint.setStyle(Paint.Style.STROKE);oval.left = marxArcStorkeWidth / 2; // 左上角xoval.top = circularDotWidth; // 左上角yoval.right = width - circularDotWidth / 2; // 左下角xoval.bottom = width - circularDotWidth / 2; // 右下角yfloat bangjing = ((width - circularDotWidth/2) / 2);//半径//调整圆背景的大小canvas.drawArc(oval, 180, 180, false, paint); // 绘制红丝圆圈,即进度条背景//进度条颜色paint.setColor(roundProgressColor);paint.setStrokeWidth(marxArcStorkeWidth);canvas.drawArc(oval, 180, 180 * ((float) progress / (float) maxProgress), false, paint); // 绘制进度圆弧,这里是蓝色//画圆点paint.setColor(circularDotColor);paint.setAntiAlias(true); // 设置画笔为抗锯齿paint.setStyle(Paint.Style.FILL);paint.setStrokeWidth(circularDotWidth);//当画笔样式为STROKE或FILL_OR_STROKE时,设置笔刷的图形样式,如圆形样式Cap.ROUND,或方形样式Cap.SQUAREpaint.setStrokeCap(Paint.Cap.ROUND);float jindu = ((float) progress * 1.8f);canvas.drawPoint(bangjing - ((float) (Math.sin((Math.PI / (double) 180) * (jindu <= 90 ? 90 - (jindu) : -jindu + 90))) * bangjing),bangjing+circularDotWidth - ((float) (Math.cos((Math.PI / (double) 180) * (double) (jindu <= 90 ? 90 - jindu : -jindu + 90))) * bangjing), paint);}}
attrs.xml
<?xml version="1.0" encoding="utf-8"?>
<resources><!--自定义半圆形加载进度条--><declare-styleable name="HalfProgressBar"><attr name="roundColor1" format="color"/><attr name="roundProgressColor1" format="color"/><attr name="circularDotColor1" format="color"/></declare-styleable>
</resources>xml中<com.jyc99.demo.HalfProgressBar
        android:layout_width="match_parent"android:layout_height="wrap_content"android:id="@+id/view"android:layout_centerHorizontal="true"android:layout_marginTop="42dp"android_custom:roundColor1="#fc422b"android_custom:roundProgressColor1="#fa432e"android_custom:circularDotColor1="#246223"/>

由于截图的原因可能看不到圆点 , 大家自己试试调调颜色 调整一下高度宽度
效果图不算太 需要自己试试调调颜色已经高度宽度

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



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

相关文章

Android使用java实现网络连通性检查详解

《Android使用java实现网络连通性检查详解》这篇文章主要为大家详细介绍了Android使用java实现网络连通性检查的相关知识,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录NetCheck.Java(可直接拷贝)使用示例(Activity/Fragment 内)权限要求

2025最新版Android Studio安装及组件配置教程(SDK、JDK、Gradle)

《2025最新版AndroidStudio安装及组件配置教程(SDK、JDK、Gradle)》:本文主要介绍2025最新版AndroidStudio安装及组件配置(SDK、JDK、Gradle... 目录原生 android 简介Android Studio必备组件一、Android Studio安装二、A

kafka自定义分区器使用详解

《kafka自定义分区器使用详解》本文介绍了如何根据企业需求自定义Kafka分区器,只需实现Partitioner接口并重写partition()方法,示例中,包含cuihaida的数据发送到0号分区... 目录kafka自定义分区器假设现在有一个需求使用分区器的方法总结kafka自定义分区器根据企业需求

C#中通过Response.Headers设置自定义参数的代码示例

《C#中通过Response.Headers设置自定义参数的代码示例》:本文主要介绍C#中通过Response.Headers设置自定义响应头的方法,涵盖基础添加、安全校验、生产实践及调试技巧,强... 目录一、基础设置方法1. 直接添加自定义头2. 批量设置模式二、高级配置技巧1. 安全校验机制2. 类型

Android实现图片浏览功能的示例详解(附带源码)

《Android实现图片浏览功能的示例详解(附带源码)》在许多应用中,都需要展示图片并支持用户进行浏览,本文主要为大家介绍了如何通过Android实现图片浏览功能,感兴趣的小伙伴可以跟随小编一起学习一... 目录一、项目背景详细介绍二、项目需求详细介绍三、相关技术详细介绍四、实现思路详细介绍五、完整实现代码

SpringBoot AspectJ切面配合自定义注解实现权限校验的示例详解

《SpringBootAspectJ切面配合自定义注解实现权限校验的示例详解》本文章介绍了如何通过创建自定义的权限校验注解,配合AspectJ切面拦截注解实现权限校验,本文结合实例代码给大家介绍的非... 目录1. 创建权限校验注解2. 创建ASPectJ切面拦截注解校验权限3. 用法示例A. 参考文章本文

在Android中使用WebView在线查看PDF文件的方法示例

《在Android中使用WebView在线查看PDF文件的方法示例》在Android应用开发中,有时我们需要在客户端展示PDF文件,以便用户可以阅读或交互,:本文主要介绍在Android中使用We... 目录简介:1. WebView组件介绍2. 在androidManifest.XML中添加Interne

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息