android ——自定义计步器

2023-12-21 19:12
文章标签 android 自定义 计步器

本文主要是介绍android ——自定义计步器,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

一、运行效果展示

在这里插入图片描述

二、代码解析:

1、res — values下新建attrs.xml文件:

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="QQStepView">
<!--       定义两个圆弧的颜色--><attr name="outerColor" format="color"/> <attr name="innerColor" format="color"/>
<!--       圆弧边框大小,两个边框尺寸一样,只定义一个即可--><attr name="borderWidth" format="dimension"/>
<!--       圆弧内字体的大小和颜色--><attr name="stepTextSize" format="dimension"/><attr name="stepTextColor" format="color"/></declare-styleable>
</resources>

2、新建QQStepView类继承View

public class QQStepView extends View {private int mOuterColor= Color.RED;private int mInnerColor=Color.BLUE;private int mBorderWidth=20;private int mStepTextSize;private int mStepTextColor;//    画外圆的画笔private Paint mOutPaint;
//    画内圆的画笔private Paint mInterPaint;
//    文字画笔private Paint mTextPaint;//   总共的private int mStepMax=0;
//    当前的步数private int mCurrentStep=0;public QQStepView(Context context) {this(context,null);}public QQStepView(Context context, @Nullable AttributeSet attrs) {this(context, attrs,0);}public QQStepView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);
//        1、分析效果
//        2、确定自定义属性,编写attrs.xml文件
//        3、在布局文件中使用
//        4、在自定义view中获取自定义属性TypedArray array= context.obtainStyledAttributes(attrs, R.styleable.QQStepView);mOuterColor=array.getColor(R.styleable.QQStepView_outerColor,mOuterColor);mInnerColor=array.getColor(R.styleable.QQStepView_innerColor,mInnerColor);mBorderWidth=(int) array.getDimension(R.styleable.QQStepView_borderWidth,mBorderWidth);mStepTextColor=array.getColor(R.styleable.QQStepView_stepTextColor,mStepTextColor);mStepTextSize=array.getDimensionPixelSize(R.styleable.QQStepView_stepTextSize,mStepTextSize);array.recycle();mOutPaint=new Paint();mOutPaint.setColor(mOuterColor);mOutPaint.setStrokeWidth(mBorderWidth); //画笔宽度mOutPaint.setAntiAlias(true); //抗锯齿mOutPaint.setStrokeCap(Paint.Cap.ROUND); // 圆弧末尾圆角mOutPaint.setStyle(Paint.Style.STROKE); //设置画笔空心mInterPaint=new Paint();mInterPaint.setColor(mInnerColor);mInterPaint.setStrokeWidth(mBorderWidth); //画笔宽度mInterPaint.setAntiAlias(true); //抗锯齿mInterPaint.setStrokeCap(Paint.Cap.ROUND); // 圆弧末尾圆角mInterPaint.setStyle(Paint.Style.STROKE); //设置画笔空心mTextPaint=new Paint();mTextPaint.setColor(mInnerColor);mTextPaint.setAntiAlias(true); //抗锯齿mTextPaint.setTextSize(mStepTextSize);
//        5、onMeasure
//        6、画外圆弧、内圆弧和文字
//        7、其他}//5、onMeasure@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//        调用者在布局文件可能是wrap_content
//        宽高不一致取最小值,确保是一个正方型int width = MeasureSpec.getSize(widthMeasureSpec);int height = MeasureSpec.getSize(heightMeasureSpec);setMeasuredDimension(Math.min(width, height), Math.min(width, height));}//6、画外圆弧、内圆弧和文字@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);//        6.1、画外圆弧
//        中心点int center=getWidth()/2;int radius=getWidth()/2-mBorderWidth/2;
//        RectF rectF=new RectF(mBorderWidth/2,mBorderWidth/2,getWidth()-mBorderWidth/2,
//                getHeight()-mBorderWidth/2);RectF rectF=new RectF(center-radius,center-radius,center+radius,center+radius);canvas.drawArc(rectF,135,270,false,mOutPaint);
//        6.2、画内圆弧 ,值从外面传进来if (mStepMax == 0) return;float sweepAngle=(float) mCurrentStep/mStepMax;canvas.drawArc(rectF,135,sweepAngle*270,false,mInterPaint);
//        6.3、画文字String stepText=mCurrentStep+"";Rect textBounds=new Rect();mTextPaint.getTextBounds(stepText,0,stepText.length(),textBounds);int dx= getWidth()/2-textBounds.width()/2;
//        基线Paint.FontMetricsInt fontMetricsInt=mTextPaint.getFontMetricsInt();int dy=(fontMetricsInt.bottom - fontMetricsInt.top)/2-fontMetricsInt.bottom;int baseLine=getHeight()/2+dy;canvas.drawText(stepText,dx,baseLine,mTextPaint);}
//    7、其它,添加动画public void setStepMax(int stepMax){this.mStepMax=stepMax;}public synchronized void setCurrentStep(int currentStep){this.mCurrentStep=currentStep;invalidate(); //不断重绘}
}

3、页面xml文件中引入:

<?xml version="1.0" encoding="utf-8"?>
<layout xmlns:android="http://schemas.android.com/apk/res/android"xmlns:app="http://schemas.android.com/apk/res-auto"xmlns:tools="http://schemas.android.com/tools"><data></data><androidx.constraintlayout.widget.ConstraintLayoutandroid:layout_width="match_parent"android:layout_height="match_parent"tools:context=".CustomViewActivity"><com.lxd.androiduidemo.view.QQStepViewandroid:id="@+id/step_view"app:outerColor="@color/purple_700"app:innerColor="@color/teal_700"app:borderWidth="16dp"app:stepTextColor="@color/teal_700"app:stepTextSize="36sp"android:layout_width="200dp"android:layout_height="200dp"app:layout_constraintLeft_toLeftOf="parent"app:layout_constraintTop_toTopOf="parent"app:layout_constraintRight_toRightOf="parent"app:layout_constraintBottom_toBottomOf="parent" /></androidx.constraintlayout.widget.ConstraintLayout>
</layout>

四、页面中设置最大值,运动步数和动画:

    private ActivityCustomViewBinding customViewBinding;@Overrideprotected void onCreate(Bundle savedInstanceState) {super.onCreate(savedInstanceState);customViewBinding = DataBindingUtil.setContentView(this, R.layout.activity_custom_view);QQStepView stepView = customViewBinding.stepView;stepView.setStepMax(4000); //设置最大值stepView.setCurrentStep(3000); // 最大步数//        添加属性动画ValueAnimator animator=ObjectAnimator.ofFloat(0,3000);animator.setDuration(2000);animator.setInterpolator(new DecelerateInterpolator());//添加插值器(动画执行先快后慢)animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(@NonNull ValueAnimator animation) {float currentStep = (float)animation.getAnimatedValue();stepView.setCurrentStep((int) currentStep);}});animator.start();}

这篇关于android ——自定义计步器的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/521161

相关文章

Vite 打包目录结构自定义配置小结

《Vite打包目录结构自定义配置小结》在Vite工程开发中,默认打包后的dist目录资源常集中在asset目录下,不利于资源管理,本文基于Rollup配置原理,本文就来介绍一下通过Vite配置自定义... 目录一、实现原理二、具体配置步骤1. 基础配置文件2. 配置说明(1)js 资源分离(2)非 JS 资

Android协程高级用法大全

《Android协程高级用法大全》这篇文章给大家介绍Android协程高级用法大全,本文结合实例代码给大家介绍的非常详细,对大家的学习或工作具有一定的参考借鉴价值,需要的朋友跟随小编一起学习吧... 目录1️⃣ 协程作用域(CoroutineScope)与生命周期绑定Activity/Fragment 中手

聊聊springboot中如何自定义消息转换器

《聊聊springboot中如何自定义消息转换器》SpringBoot通过HttpMessageConverter处理HTTP数据转换,支持多种媒体类型,接下来通过本文给大家介绍springboot中... 目录核心接口springboot默认提供的转换器如何自定义消息转换器Spring Boot 中的消息

Android 缓存日志Logcat导出与分析最佳实践

《Android缓存日志Logcat导出与分析最佳实践》本文全面介绍AndroidLogcat缓存日志的导出与分析方法,涵盖按进程、缓冲区类型及日志级别过滤,自动化工具使用,常见问题解决方案和最佳实... 目录android 缓存日志(Logcat)导出与分析全攻略为什么要导出缓存日志?按需过滤导出1. 按

Python自定义异常的全面指南(入门到实践)

《Python自定义异常的全面指南(入门到实践)》想象你正在开发一个银行系统,用户转账时余额不足,如果直接抛出ValueError,调用方很难区分是金额格式错误还是余额不足,这正是Python自定义异... 目录引言:为什么需要自定义异常一、异常基础:先搞懂python的异常体系1.1 异常是什么?1.2

Linux中的自定义协议+序列反序列化用法

《Linux中的自定义协议+序列反序列化用法》文章探讨网络程序在应用层的实现,涉及TCP协议的数据传输机制、结构化数据的序列化与反序列化方法,以及通过JSON和自定义协议构建网络计算器的思路,强调分层... 目录一,再次理解协议二,序列化和反序列化三,实现网络计算器3.1 日志文件3.2Socket.hpp

C语言自定义类型之联合和枚举解读

《C语言自定义类型之联合和枚举解读》联合体共享内存,大小由最大成员决定,遵循对齐规则;枚举类型列举可能值,提升可读性和类型安全性,两者在C语言中用于优化内存和程序效率... 目录一、联合体1.1 联合体类型的声明1.2 联合体的特点1.2.1 特点11.2.2 特点21.2.3 特点31.3 联合体的大小1

Android Paging 分页加载库使用实践

《AndroidPaging分页加载库使用实践》AndroidPaging库是Jetpack组件的一部分,它提供了一套完整的解决方案来处理大型数据集的分页加载,本文将深入探讨Paging库... 目录前言一、Paging 库概述二、Paging 3 核心组件1. PagingSource2. Pager3.

springboot自定义注解RateLimiter限流注解技术文档详解

《springboot自定义注解RateLimiter限流注解技术文档详解》文章介绍了限流技术的概念、作用及实现方式,通过SpringAOP拦截方法、缓存存储计数器,结合注解、枚举、异常类等核心组件,... 目录什么是限流系统架构核心组件详解1. 限流注解 (@RateLimiter)2. 限流类型枚举 (

SpringBoot 异常处理/自定义格式校验的问题实例详解

《SpringBoot异常处理/自定义格式校验的问题实例详解》文章探讨SpringBoot中自定义注解校验问题,区分参数级与类级约束触发的异常类型,建议通过@RestControllerAdvice... 目录1. 问题简要描述2. 异常触发1) 参数级别约束2) 类级别约束3. 异常处理1) 字段级别约束