Android自定义圆环进度条/刻度仪表盘(单环单点带动画)

news/2024/12/29 2:27:31/

效果图:

 

1.自定义HeartDashBoardView

/*** 刻度仪表盘*/
public class HeartDashBoardView extends View {private static final float START_ANGLE = 135f;private static final float MAX_ANGLE = 270f;private float progress = 0;private float centerX;private float centerY;private float width;private float height;private int lineWidth = dp2px(8); // 绘制圆环的线宽private int pointWidth = dp2px(2); // 绘制圆点线宽private SweepGradient sweepGradient;private Paint paint = new Paint(Paint.ANTI_ALIAS_FLAG);private Paint pointPaint = new Paint(Paint.ANTI_ALIAS_FLAG);private ObjectAnimator animator;private int[] colors = {Color.parseColor("#09F5E6"),Color.parseColor("#09F5E6"),//浅色Color.parseColor("#23F2A4"),Color.parseColor("#40EF59"),Color.parseColor("#7EED26"),Color.parseColor("#B7EC15"),Color.parseColor("#FFE300"),Color.parseColor("#FF0B00"),Color.parseColor("#FF0000"),//深色};public HeartDashBoardView(Context context) {this(context, null);}public HeartDashBoardView(Context context, @Nullable AttributeSet attrs) {this(context, attrs, 0);}public HeartDashBoardView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);paint.setColor(Color.GRAY);paint.setStyle(Paint.Style.STROKE);paint.setStrokeCap(Paint.Cap.ROUND);paint.setStrokeWidth(lineWidth);pointPaint.setColor(Color.WHITE);pointPaint.setStyle(Paint.Style.STROKE);pointPaint.setStrokeWidth(pointWidth);}private void setProgress(float progress) {this.progress = progress;invalidate();}public void setHeartProgress(float progress) {animator = ObjectAnimator.ofFloat(this, "progress", 0, progress);animator.setDuration(2000);animator.setInterpolator(new FastOutSlowInInterpolator());animator.start();}@Overrideprotected void onSizeChanged(int w, int h, int oldw, int oldh) {super.onSizeChanged(w, h, oldw, oldh);width = getWidth();height = getHeight();centerX = getWidth() / 2;centerY = getHeight() / 2;sweepGradient = new SweepGradient(centerX, centerY, colors, null);//改变开始渐变的角度(默认从右边开始画圆渐变,旋转90度就从下方开始画圆渐变,旋转之后更好设置颜色渐变值)Matrix matrix = new Matrix();matrix.setRotate(90, centerX, centerY);sweepGradient.setLocalMatrix(matrix);paint.setShader(sweepGradient);}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);// 绘制环形drawArc(canvas);// 绘制末尾圆点drawPoint(canvas);}private void drawArc(Canvas canvas) {int padding = lineWidth + pointWidth;canvas.drawArc(padding, padding, width - padding, height - padding, START_ANGLE, MAX_ANGLE, false, paint);}private void drawPoint(Canvas canvas) {Path path = new Path();float sweepAngle = MAX_ANGLE * progress / 100;int padding = lineWidth + pointWidth;path.addArc(padding, padding, width - padding, height - padding, START_ANGLE, sweepAngle);PathMeasure measure = new PathMeasure(path, false);float[] pos = new float[2];measure.getPosTan(measure.getLength() - 1, pos, null);pointPaint.setColor(Color.parseColor("#FF7968"));pointPaint.setStyle(Paint.Style.FILL);canvas.drawCircle(pos[0], pos[1], lineWidth - pointWidth, pointPaint);pointPaint.setColor(Color.WHITE);pointPaint.setStyle(Paint.Style.STROKE);canvas.drawCircle(pos[0], pos[1], lineWidth - pointWidth, pointPaint);}public int dp2px(final float dpValue) {final float scale = Resources.getSystem().getDisplayMetrics().density;return (int) (dpValue * scale + 0.5f);}
}

2.布局中使用

            <com.hwd.flowfit.ui.widget.HeartDashBoardViewandroid:id="@+id/heartChart"android:layout_width="130dp"android:layout_height="130dp"android:layout_marginStart="30dp"app:layout_constraintBottom_toBottomOf="parent"app:layout_constraintEnd_toEndOf="parent"app:layout_constraintStart_toStartOf="parent"app:layout_constraintTop_toTopOf="parent" />

3.设置进度

heartChart.setHeartProgress(30F)

http://www.ppmy.cn/news/872214.html

相关文章

记录--你知道Vue中的Scoped css原理么?

这里给大家分享我在网上总结出来的一些知识&#xff0c;希望对大家有所帮助 追忆Scoped 偶然想起了一次面试&#xff0c;二面整体都聊完了&#xff0c;该做的算法题都做出来了&#xff0c;该背的八股文也背的差不多了&#xff0c;面试官频频点头&#xff0c;似乎对我的基础和项…

计算联合体大小的测试demo

两个规则&#xff1a; 1.联合体大小必须能容纳联合体中最大的成员变量2.通过规则1 计算出的联合体大小必须是联合体中占内存大小最大的成员类型的整数倍 #include<stdio.h>typedef union {char a[7];int b; }test_union_1;typedef union {char a[6];char b; }test_un…

关于Windows 11 docker desktop 运行doris 容器时vm.max_map_count=2000000的设置问题

需要一个简单的测试环境&#xff0c;于是准备用docker启动一个1fe 1be的简单玩一下 如果be容器启动后再去修改 /etc/sysctl.conf sysctl -w vm.max_map_count2000000 这个参数是没用的&#xff0c;be仍然会启动失败 这时可以打开cmd wsl --list C:\Users\pc>wsl --list …

USound与AT S凭借基于MEMS的全集成音频模块革新真无线耳机市场

奥地利格拉茨--(美国商业资讯)--USound宣布与AT&S Austria Technologie & Systemtechnik扩大合作&#xff0c;制造用于真无线耳机(TWS)的USound新型全集成音频模块。USound是用于个人设备和可穿戴技术的微机电系统(MEMS)扬声器的全球开发商和制造商&#xff0c;而AT&am…

南卡骨传导耳机获得缪斯国际设计大奖,展现中国品牌设计美学的新高度

近日&#xff0c;全球创意设计领域最具影响力——国际缪斯(Muse Design Awards)公布获奖名单&#xff0c;中国知名骨传导耳机品牌南卡旗下的NANK-NEO产品&#xff0c;凭借着坚持自主创新设计理念以及卓越超前的产品设计&#xff0c;获得国际缪斯大奖&#xff0c;从众多参赛作品…

南卡骨传导耳机斩获缪斯国际设计大奖,用设计美学开创新视界,展现中国品牌实力!

美国MUSE设计奖&#xff0c;历来是全球设计领域最具影响力的国际奖项之一&#xff0c;近日公布了获奖名单&#xff0c;中国最具影响力知名骨传导耳机品牌南卡获得了此次的国际缪斯大奖&#xff0c;而此次的获奖也意味着&#xff0c;南卡品牌在骨传导耳机领域里的实力得到国际权…

LASMEX勒姆森耳机2018年度春季新品发布会在北京举行

德国耳机品牌LASMEX勒姆森于2018年1月12日在北京民生现代美术馆盛大举办2018年度春季新品发布会。LASMEX勒姆森在现场发布了两款蓝牙耳机。 LASMEX勒姆森CEO叶民&#xff0c;LASMEX勒姆森COO冯若昊&#xff0c;京东影音生活部门经理杨彬彬以及众多合作伙伴到场与媒体朋友一同参…

上海电子陶瓷展|2023上海国际电子陶瓷及压电晶体展览会【官网】

上海电子陶瓷展&#xff0c;电子陶瓷设备展&#xff0c;电子陶瓷展&#xff0c;上海压电晶体展&#xff0c;压电晶体展&#xff0c;电子陶瓷材料展&#xff0c;上海电子陶瓷材料展&#xff0c;电子陶瓷材料展览会&#xff0c;上海电子陶瓷设备展 2023上海国际电子陶瓷及压电晶…