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
0
投稿
猜你喜欢
- 前言工作中使用mybatis时我们需要根据数据表字段创建pojo类、mapper文件以及dao类,并且需要配置它们之间的依赖关系,这样的工作
- android线程消息机制主要由Handler,Looper,Message和MessageQuene四个部分组成。平常在开发中,我们常用来
- FrameLayout 在这个布局中,所有的子元素都不能被指定放置的位置,他们统统防御这块区域的左上角, 并且后面的子元素直接覆盖在前面的子
- 本文实例为大家分享了C#超市收银系统设计的具体代码,供大家参考,具体内容如下1.登录界面代码如下:using System;using Sy
- 一、获取android工程里面的各种资源的id; 1.1 string型 比如下面: << string name=”OK”&g
- 最近研究了一下如何在Android上实现CoverFlow效果的控件,其实早在2010年,就有Neil Davies开发并开源出了这个控件,
- @Value注解读取yml中的map配置网上查了好多资料,都是.properties文件中读取,而且又是几个人抄来抄去,找了半天功夫不负有心
- 环境:VS2019+Qt5.121. CLR库安装 &nb
- 一,FileWritter写入文件FileWritter, 字符流写入字符到文件。默认情况下,它会使用新的内容取代所有现有的内容,然而,当指
- 本文实例为大家分享了java简单实现斗地主发牌的具体代码,供大家参考,具体内容如下问题:参考斗地主的游戏规则,完成一个发牌的功能(54张牌,
- 在并发多线程的情况下,为了保证数据安全性,一般我们会对数据进行加锁,通常使用Synchronized或者ReentrantLock同步锁。S
- 本文实例讲述了Android中断线程的处理方法。分享给大家供大家参考。具体方法如下:我现在对一个用户注册的功能1.用ProgressDial
- 这篇文章主要介绍了Java如何实现自定义异常类,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参
- 开发 Web 应用的思路实现一个简单的 JSP/Servlet。搭建创建 Web 应用工程的环境。创建 Web 应用工程。Web 应用工程的
- 今天讲解一下Fragment的控制,主要是切换View和页面替换等操作。还有就是如何获取Fragment的管理对象,以及与Activity的
- 十六进制字符串与数值类型之间转换(C# 编程指南) 以下示例演示如何执行下列任务: 获取字符串中每个字符的十六进制值。 获取与十六进制字符串
- 一、Flutter代码的启动起点我们在多数的业务场景下,使用的都是FlutterActivity、FlutterFragment。在在背后,
- 一、概述现在大多数的电商APP的详情页长得几乎都差不多,几乎都是上面一个商品的图片,当你滑动的时候,会有Tab悬浮在上面,这样做用户体验确实
- 对接前端后效果展示如图:1、CPU相关信息实体类/** * CPU相关信息 * * @author csp */public class
- 右击有main方法的类===> Run as===> Run Configurations ===>双击java