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

相关文章

Android中Dialog的使用详解

《Android中Dialog的使用详解》Dialog(对话框)是Android中常用的UI组件,用于临时显示重要信息或获取用户输入,本文给大家介绍Android中Dialog的使用,感兴趣的朋友一起... 目录android中Dialog的使用详解1. 基本Dialog类型1.1 AlertDialog(

使用Sentinel自定义返回和实现区分来源方式

《使用Sentinel自定义返回和实现区分来源方式》:本文主要介绍使用Sentinel自定义返回和实现区分来源方式,具有很好的参考价值,希望对大家有所帮助,如有错误或未考虑完全的地方,望不吝赐教... 目录Sentinel自定义返回和实现区分来源1. 自定义错误返回2. 实现区分来源总结Sentinel自定

Android Kotlin 高阶函数详解及其在协程中的应用小结

《AndroidKotlin高阶函数详解及其在协程中的应用小结》高阶函数是Kotlin中的一个重要特性,它能够将函数作为一等公民(First-ClassCitizen),使得代码更加简洁、灵活和可... 目录1. 引言2. 什么是高阶函数?3. 高阶函数的基础用法3.1 传递函数作为参数3.2 Lambda

如何自定义Nginx JSON日志格式配置

《如何自定义NginxJSON日志格式配置》Nginx作为最流行的Web服务器之一,其灵活的日志配置能力允许我们根据需求定制日志格式,本文将详细介绍如何配置Nginx以JSON格式记录访问日志,这种... 目录前言为什么选择jsON格式日志?配置步骤详解1. 安装Nginx服务2. 自定义JSON日志格式各

Android自定义Scrollbar的两种实现方式

《Android自定义Scrollbar的两种实现方式》本文介绍两种实现自定义滚动条的方法,分别通过ItemDecoration方案和独立View方案实现滚动条定制化,文章通过代码示例讲解的非常详细,... 目录方案一:ItemDecoration实现(推荐用于RecyclerView)实现原理完整代码实现

基于Spring实现自定义错误信息返回详解

《基于Spring实现自定义错误信息返回详解》这篇文章主要为大家详细介绍了如何基于Spring实现自定义错误信息返回效果,文中的示例代码讲解详细,感兴趣的小伙伴可以跟随小编一起学习一下... 目录背景目标实现产出背景Spring 提供了 @RestConChina编程trollerAdvice 用来实现 HTT

Android App安装列表获取方法(实践方案)

《AndroidApp安装列表获取方法(实践方案)》文章介绍了Android11及以上版本获取应用列表的方案调整,包括权限配置、白名单配置和action配置三种方式,并提供了相应的Java和Kotl... 目录前言实现方案         方案概述一、 androidManifest 三种配置方式

SpringSecurity 认证、注销、权限控制功能(注销、记住密码、自定义登入页)

《SpringSecurity认证、注销、权限控制功能(注销、记住密码、自定义登入页)》SpringSecurity是一个强大的Java框架,用于保护应用程序的安全性,它提供了一套全面的安全解决方案... 目录简介认识Spring Security“认证”(Authentication)“授权” (Auth

Android WebView无法加载H5页面的常见问题和解决方法

《AndroidWebView无法加载H5页面的常见问题和解决方法》AndroidWebView是一种视图组件,使得Android应用能够显示网页内容,它基于Chromium,具备现代浏览器的许多功... 目录1. WebView 简介2. 常见问题3. 网络权限设置4. 启用 JavaScript5. D

Android如何获取当前CPU频率和占用率

《Android如何获取当前CPU频率和占用率》最近在优化App的性能,需要获取当前CPU视频频率和占用率,所以本文小编就来和大家总结一下如何在Android中获取当前CPU频率和占用率吧... 最近在优化 App 的性能,需要获取当前 CPU视频频率和占用率,通过查询资料,大致思路如下:目前没有标准的