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 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自定

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现