Android自定义View实现圆环进度条
作者:汪星没有熊 时间:2023-09-20 00:19:13
本文实例为大家分享了Android自定义View实现圆环进度条的具体代码,供大家参考,具体内容如下
效果展示
动画效果
View实现
1.底层圆环是灰色背景
2.上层圆环是红色背景
3.使用动画画一条弧线
View
/**
* 圆环进度条
*/
public class RoundProgressBar extends View {
//绘制矩形区域
private RectF rectF;
//起始角度
private float startAngle;
//扫过角度
private float sweepAngle;
//画笔
private Paint paint;
//默认控件大小
private int defoutSize;
//默认线条宽度
private int defoutLine;
private int strokeWidth;
private PointF pointF = new PointF();
public RoundProgressBar(Context context) {
super(context);
initData();
}
public RoundProgressBar(Context context, AttributeSet attrs) {
super(context, attrs);
initData();
}
/**
* 参数初始化
*/
private void initData() {
startAngle = 0;
sweepAngle = 0;
defoutSize = 400;
defoutLine = 20;
strokeWidth = 20;
rectF = new RectF();
//抗锯齿画笔
paint = new Paint(Paint.ANTI_ALIAS_FLAG);
paint.setColor(Color.GRAY);
paint.setStrokeWidth(defoutLine);
//笔帽样式
paint.setStrokeCap(Paint.Cap.ROUND);
paint.setStyle(Paint.Style.STROKE);
}
/**
* xml -----> 提供可绘制位置
*
* @param widthMeasureSpec 宽
* @param heightMeasureSpec 高
*/
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension(defoutSize, defoutSize);
}
/**
* 当大小时改变回调
*
* @param w
* @param h
* @param oldw
* @param oldh
*/
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
pointF.x = w >> 1;
pointF.y = h >> 1;
rectF.top = strokeWidth >> 1;
rectF.bottom = h - (strokeWidth >> 1);
rectF.left = strokeWidth >> 1;
rectF.right = w - (strokeWidth >> 1);
}
/**
* 绘制
*
* @param canvas
*/
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
//画布旋转
paint.setColor(Color.GRAY);
canvas.rotate(135, pointF.x, pointF.y);
//绘制圆环
canvas.drawArc(rectF, startAngle, 270, false, paint);
paint.setColor(Color.RED);
canvas.drawArc(rectF, startAngle, sweepAngle, false, paint);
}
public void setProgress(float index) {
//防止数值越界
if (index > 1 || index < 0) {
return;
}
ValueAnimator valueAnimator = ValueAnimator.ofFloat(0, index);
valueAnimator.setDuration(3000);
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
sweepAngle = (float) animation.getAnimatedValue() * 270;
//重写绘制
invalidate();
}
});
valueAnimator.start();
}
}
最后在Activity中使用setProgress方法赋值进度条的进度来实现效果
progressView.setProgress(0.8f);
来源:https://blog.csdn.net/weixin_45697390/article/details/106364039
标签:Android,View,进度条
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
Android ToggleButton 详解及实例代码
2022-09-11 03:58:02
![](https://img.aspxhome.com/file/2023/5/125355_0s.png)
Java 蒙特卡洛算法求圆周率近似值实例详解
2023-10-19 23:32:10
C#二维数组与多维数组的具体使用
2023-07-30 23:53:49
![](https://img.aspxhome.com/file/2023/5/82185_0s.jpg)
Spring Cloud Gateway 如何修改HTTP响应信息
2021-07-20 13:33:40
简单了解JavaBean作用及常用操作
2023-09-08 19:59:29
![](https://img.aspxhome.com/file/2023/3/72643_0s.png)
Android视频悬浮窗口实现的示例代码
2022-08-01 06:50:33
![](https://img.aspxhome.com/file/2023/6/138386_0s.gif)
mybatis的动态SQL和模糊查询实例详解
2022-03-10 09:47:03
![](https://img.aspxhome.com/file/2023/1/99851_0s.png)
android当前apn的状态以及获取方法
2022-06-20 14:52:25
Android多点触控实现图片自由缩放
2023-12-15 20:48:40
详谈java 堆区、方法区和栈区
2023-11-23 18:35:22
java获取ip地址示例
2021-12-25 07:04:22
Java中i++与++i的区别和使用
2022-03-20 18:08:18
![](https://img.aspxhome.com/file/2023/9/101999_0s.png)
IDEA的Swing可视化插件JFormDesigner详解
2023-09-23 08:02:54
![](https://img.aspxhome.com/file/2023/6/62996_0s.jpg)
SpringBoot设置编码UTF-8的两种方法
2022-05-04 00:09:08
java实现二叉树遍历的三种方式
2021-12-03 06:11:51
![](https://img.aspxhome.com/file/2023/9/84179_0s.jpg)
c#的sortedlist使用方法
2023-09-22 04:58:13
Java对xls文件进行读写操作示例代码
2023-08-04 17:55:31
Java排序的那些事之sort方法的使用详解
2023-01-22 00:18:35
![](https://img.aspxhome.com/file/2023/4/61014_0s.png)
Java之 TreeSet的详细使用说明
2021-08-22 14:52:22
![](https://img.aspxhome.com/file/2023/8/77078_0s.jpg)
Android开发实现简单计算器功能
2022-10-17 09:18:16
![](https://img.aspxhome.com/file/2023/9/139599_0s.jpg)