Android自定义带圆点的半圆形进度条
作者:songyan_love 发布时间:2023-08-05 07:47:15
标签:Android,进度条
本文实例为大家分享了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);
}
@Override
protected 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; // 左上角x
oval.top = circularDotWidth; // 左上角y
oval.right = width - circularDotWidth / 2; // 左下角x
oval.bottom = width - circularDotWidth / 2; // 右下角y
float 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.SQUARE
paint.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"/>
由于截图的原因可能看不到圆点 , 大家自己试试调调颜色 调整一下高度宽度
来源:https://blog.csdn.net/songyan_love/article/details/51570973


猜你喜欢
- 经过一番的探索,终于搞清楚关键字async/await 在.net4.5之后可用的巧妙之处,在这里记录一下也与大家分享一下个人的心得体会as
- 前言在上一章节Spring和Mybatis整合的原理详解中有写到Spring和MyBatis整合时用到的Bean扫描是Spring本身提供的
- 本文实例为大家分享了Android自定义View画天气预报折线图的具体代码,供大家参考,具体内容如下效果图如下:刚开始尝试用第三方画曲线的框
- 本文通过解决老王经常搞错借书人的问题,来引出行为型模式中的命令模式。为了在案例之上理解的更加透彻,我们需要了解命令模式在源码中的应用。最后指
- 本文实例讲述了C++求四个正整数最大公约数的方法。分享给大家供大家参考,具体如下:/** 作 者: 刘同宾* 完成日期:2012 年 11
- 本文实例讲述了Android编程基于重力传感器实现横竖屏放向切换功能。分享给大家供大家参考,具体如下:最近项目中用到了vr视频播放,因为自己
- 标准函数with与run和apply with函数with函数接收两个参数:第一个参数可以是任意类型的对象,第二个参数是一个Lambda表达
- 先看效果图:你可以定义成你项目的logo图片,可以设置水波颜色、波长、波宽、字体大小、颜色、进度条的最大值,当前进度值,还可以设置波纹震动的
- 前面两篇文章,分别简述了多线程的使用和发展历程,但是使用多线程无法避免的一个问题就是多线程安全。那什么是多线程安全?如何解决多线程安全?本文
- 将自然语言编写的测试用例转换为可执行的测试,可以大大降低需求与开发之间的沟通成本,这是BDD(行为驱动开发)希望达到的效果。SpecFlow
- 废话不多说了,直接给大家贴java代码了。 import java.io.IOException;import sun.net.Telnet
- 本文通过是 * 实现的AOP功能的封装与配置的小框架.加深对 * 和AOP编程的理解设计根据配置文件的键xxx对应的值(类全名)创建相应
- 本文实例为大家分享了C#实现图片切割、切图的具体代码,供大家参考,具体内容如下前台准备两个Image控件。上面是显示原图,下面显示切割后的效
- Spring注解AspectJ操作AOP一、被增强类新建一个被增强的类 User,下面有个 add() 方法。package com.pin
- 本文实例为大家分享了SpringBoot使用POI进行Excel下载的具体代码,供大家参考,具体内容如下使用poi处理Excel特别方便,此
- 本文实例讲述了C#类中static变量用法。分享给大家供大家参考。具体分析如下:先来看一段代码:using System; namespac
- 曾几何时,我们写代码的时候,每次写Bean的时候都会使用快捷键生成get/set方法,有时候我经常会想,既然每一个Bean我们都会给其提供g
- 本文实例为大家分享了C#实现多个计时器记录不同定时时间的具体代码,供大家参考,具体内容如下1.定义Timer类、定义委托//定义Timer类
- 创建一个类,在该类的主方法中创建标准输入流的扫描器对象,提示用户输入一个整数,并通过扫描器的方法来接受这个整数,然后通过三元运算符判断该数字
- SpringBoot2.x过后static下的静态资源无法访问package com.example.thymeleaf.commons;i