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 12解决push framework.jar无法开机的方法小结

《Android12解决pushframework.jar无法开机的方法小结》:本文主要介绍在Android12中解决pushframework.jar无法开机的方法,包括编译指令、框架层和s... 目录1. android 编译指令1.1 framework层的编译指令1.2 替换framework.ja

Android开发环境配置避坑指南

《Android开发环境配置避坑指南》本文主要介绍了Android开发环境配置过程中遇到的问题及解决方案,包括VPN注意事项、工具版本统一、Gerrit邮箱配置、Git拉取和提交代码、MergevsR... 目录网络环境:VPN 注意事项工具版本统一:android Studio & JDKGerrit的邮

Android实现定时任务的几种方式汇总(附源码)

《Android实现定时任务的几种方式汇总(附源码)》在Android应用中,定时任务(ScheduledTask)的需求几乎无处不在:从定时刷新数据、定时备份、定时推送通知,到夜间静默下载、循环执行... 目录一、项目介绍1. 背景与意义二、相关基础知识与系统约束三、方案一:Handler.postDel

Spring Security自定义身份认证的实现方法

《SpringSecurity自定义身份认证的实现方法》:本文主要介绍SpringSecurity自定义身份认证的实现方法,下面对SpringSecurity的这三种自定义身份认证进行详细讲解,... 目录1.内存身份认证(1)创建配置类(2)验证内存身份认证2.JDBC身份认证(1)数据准备 (2)配置依

Android使用ImageView.ScaleType实现图片的缩放与裁剪功能

《Android使用ImageView.ScaleType实现图片的缩放与裁剪功能》ImageView是最常用的控件之一,它用于展示各种类型的图片,为了能够根据需求调整图片的显示效果,Android提... 目录什么是 ImageView.ScaleType?FIT_XYFIT_STARTFIT_CENTE

Android实现在线预览office文档的示例详解

《Android实现在线预览office文档的示例详解》在移动端展示在线Office文档(如Word、Excel、PPT)是一项常见需求,这篇文章为大家重点介绍了两种方案的实现方法,希望对大家有一定的... 目录一、项目概述二、相关技术知识三、实现思路3.1 方案一:WebView + Office Onl

Android实现两台手机屏幕共享和远程控制功能

《Android实现两台手机屏幕共享和远程控制功能》在远程协助、在线教学、技术支持等多种场景下,实时获得另一部移动设备的屏幕画面,并对其进行操作,具有极高的应用价值,本项目旨在实现两台Android手... 目录一、项目概述二、相关知识2.1 MediaProjection API2.2 Socket 网络

Android实现悬浮按钮功能

《Android实现悬浮按钮功能》在很多场景中,我们希望在应用或系统任意界面上都能看到一个小的“悬浮按钮”(FloatingButton),用来快速启动工具、展示未读信息或快捷操作,所以本文给大家介绍... 目录一、项目概述二、相关技术知识三、实现思路四、整合代码4.1 Java 代码(MainActivi

Android Mainline基础简介

《AndroidMainline基础简介》AndroidMainline是通过模块化更新Android核心组件的框架,可能提高安全性,本文给大家介绍AndroidMainline基础简介,感兴趣的朋... 目录关键要点什么是 android Mainline?Android Mainline 的工作原理关键

如何解决idea的Module:‘:app‘platform‘android-32‘not found.问题

《如何解决idea的Module:‘:app‘platform‘android-32‘notfound.问题》:本文主要介绍如何解决idea的Module:‘:app‘platform‘andr... 目录idea的Module:‘:app‘pwww.chinasem.cnlatform‘android-32