先看效果图:

自定义属性

<declare-styleable name="QQSteps"><attr name="roundWidth" format="dimension" /><attr name="roundColor" format="color" /><attr name="progressColor" format="color" /><attr name="progressStep" format="integer" /><attr name="maxStep" format="integer" /><attr name="textColor" format="color" /><attr name="textSize" format="dimension" /></declare-styleable>

自定义view 仿qq步数

package com.demo;import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.view.View;import androidx.annotation.Nullable;import com.demo.myapp.R;/*** 仿qq步数 半圆弧** @author gestwr* @date 2023/2/8*/
public class QQSteps extends View {private int mRoundWidth;private int mRoundColor;private int mProgressColor;private int mProgressStep;private int mMaxStep;private int mTextSize;private int mTextColor;private static final Paint mPaint = new Paint();private int mViewWidth;private int mViewHeight;private int colorPrimary;private int colorAccent;private float percent;private RectF oval;public QQSteps(Context context) {super(context);}public QQSteps(Context context, @Nullable AttributeSet attrs) {super(context, attrs);initView(context, attrs, 0);}public QQSteps(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);initView(context, attrs, defStyleAttr);}private void initView(Context context, AttributeSet attrs, int defStyleAttr) {//默认圆弧颜色:底色 强调色colorPrimary = 0x33559bff;colorAccent = 0xFFFF4081;// 获取TypedArrayTypedArray typedArray = context.obtainStyledAttributes(attrs, R.styleable.QQSteps);mRoundWidth = typedArray.getDimensionPixelSize(R.styleable.QQSteps_roundWidth, 20);mRoundColor = typedArray.getColor(R.styleable.QQSteps_roundColor, colorPrimary);mProgressColor = typedArray.getColor(R.styleable.QQSteps_progressColor, colorAccent);mProgressStep = typedArray.getInt(R.styleable.QQSteps_progressStep, 0);mMaxStep = typedArray.getInt(R.styleable.QQSteps_progressStep, 100);mTextColor = R.color.colorAAA;mTextSize = typedArray.getDimensionPixelSize(R.styleable.QQSteps_progressStep, 40);// 回收typedArray.recycle();}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {super.onMeasure(widthMeasureSpec, heightMeasureSpec);mViewWidth = getMeasuredWidth();mViewHeight = getMeasuredHeight();}@Overrideprotected void onDraw(Canvas canvas) {// 1.画背景大圆弧int centerX = mViewWidth / 2;int centerY = mViewHeight / 2;// 设置圆弧画笔的宽度mPaint.setStrokeWidth(mRoundWidth);// 设置为 ROUNDmPaint.setStrokeCap(Paint.Cap.ROUND);mPaint.setStrokeJoin(Paint.Join.ROUND);// 设置画笔颜色mPaint.setColor(mRoundColor);mPaint.setStyle(Paint.Style.STROKE);// 半径int radius = (int) (centerX - mRoundWidth/2);oval = new RectF(centerX - radius, centerY - radius, centerX + radius, centerY + radius);// 画背景圆弧canvas.drawArc(oval, 180, 180, false, mPaint);// 2.画进度圆弧mPaint.setColor(mProgressColor);// 计算当前百分比percent = (float) mProgressStep / mMaxStep;// 根据当前百分比计算圆弧扫描的角度canvas.drawArc(oval, 180, percent * 180, false, mPaint);// 3.画文字 重置画笔mPaint.reset();mPaint.setAntiAlias(true);mPaint.setTextSize(mTextSize);mPaint.setColor(mTextColor);String mStep = ((int) (percent * mMaxStep)) + "";// 测量文字的宽高Rect textBounds = new Rect();mPaint.getTextBounds(mStep, 0, mStep.length(), textBounds);int dx = (getWidth() - textBounds.width()) / 2;// 获取画笔的FontMetricsPaint.FontMetrics fontMetrics = mPaint.getFontMetrics();// 计算文字的基线int baseLine = (int) (getHeight() / 2 + (fontMetrics.bottom - fontMetrics.top) / 2 - fontMetrics.bottom);// 绘制步数文字canvas.drawText(mStep, dx, baseLine, mPaint);}// 设置当前最大步数public synchronized void setMaxStep(int maxStep) {if (maxStep < 0) {throw new IllegalArgumentException("maxStep 不能小于0!");}this.mMaxStep = maxStep;}public synchronized int getMaxStep() {return mMaxStep;}// 设置进度public synchronized void setProgress(int progress) {if (progress < 0) {throw new IllegalArgumentException("progress 不能小于0!");}this.mProgressStep = progress;// 重新刷新绘制 -> onDraw()invalidate();}public synchronized int getProgress() {return mProgressStep;}public float getPercent() {return percent;}public void setRoundWidth(int mRoundWidth) {this.mRoundWidth = mRoundWidth;}public void setRoundColor(int mRoundColor) {this.mRoundColor = mRoundColor;}public void setProgressColor(int mProgressColor) {this.mProgressColor = mProgressColor;}public void setTextSize(int mTextSize) {this.mTextSize = mTextSize;}public void setTextColor(int mTextColor) {this.mTextColor = mTextColor;}public void setColorPrimary(int colorPrimary) {this.colorPrimary = colorPrimary;}public void setColorAccent(int colorAccent) {this.colorAccent = colorAccent;}
}

自定义view 仿qq步数 半圆弧相关推荐

  1. 自定义View | 仿QQ运动步数进度效果

    项目GitHub地址 思路 固定不动的蓝色大圆弧 动画变动的红色小圆弧 中间的步数文字显示 相关的自定义属性 比如固定不动的大圆弧, 我们不能写死他的蓝色颜色属性, 要提供一个颜色的自定义属性给用户自 ...

  2. android的动态tab,Android自定义view仿QQ的Tab按钮动画效果(示例代码)

    话不多说 先上效果图 实现其实很简单,先用两张图 一张是背景的图,一张是笑脸的图片,笑脸的图片是白色,可能看不出来.实现思路:主要是再触摸view的时候同时移动这两个图片,但是移动的距离不一样,造成的 ...

  3. 自定义View 仿QQ运动步数进度效果

    1. 概述   我记得QQ之前是有一个,运动步数的进度效果,今天打开QQ一看发现没有了.具体效果我也不清楚了,我就按照自己大概的印象写一下,这一期我们主要是熟悉Paint画笔的使用:    2. 效果 ...

  4. android自定义计步器形状,Android自定义View仿QQ运动步数效果

    本文实例为大家分享了Android QQ运动步数的具体代码,供大家参考,具体内容如下 今天我们实现下面这样的效果: 首先自定义属性: 自定义View代码如下: /** * Created by Mic ...

  5. oracle number型步数,Android自定义View仿QQ计步器

    自定义计步器 Android自定义View是Android开发中比较重要的一项,也是很多开发者比较怕的一个东西.其实只要认真去学习,自定义View其实没有那么可怕:相反的,我们还能从自定义View中找 ...

  6. 自定义View - 仿QQ运动步数效果

    今天我们实现下面这样的效果: 首先自定义属性: <?xml version="1.0" encoding="utf-8"?> <resourc ...

  7. Android自定义view仿QQ的Tab按钮动效

    话不多说 先上效果图 实现其实很简单,先用两张图 一张是背景的图,一张是笑脸的图片,笑脸的图片是白色,可能看不出来.实现思路:主要是再触摸view的时候同时移动这两个图片,但是移动的距离不一样,造成的 ...

  8. 超简单仿QQ步数显示控件

    本着写文即学习的态度,记录下自定义一款超简单仿QQ步数加载控件,话不多数先看图. 一,特性: 1,外圈大圆.进度圆和数字显示的颜色均可以自定义 2,设置最大值和当前值 3,设置加载持续时间 二,实现: ...

  9. Android仿支付宝UI功能开发,Android 自定义view仿支付宝咻一咻功能

    支付宝上有一个咻一咻的功能,就是点击图片后四周有水波纹的这种效果,今天也写一个类似的功能. 效果如下所示: 思路: 就是几个圆的半径不断在变大,这个可以使用动画缩放实现,还有透明动画 还有就是这是好几 ...

最新文章

  1. python调用zabbix api接口实时展示数据
  2. jquery之empty()与remove([expr])区别
  3. php golang 加密 对接,把php的加密算法转为go语言
  4. 查看pcl版本 linux,Ubuntu16上安装PCL
  5. mybatis-plus忽略映射字段
  6. python3 安装opencv_树莓派安装Python3的OpenCV
  7. vsCode 快捷键、插件
  8. solidworks二次开发 学习日记--1 开发方式
  9. 阿里巴巴“牛逼”了,申请“行政干预”区块链专利
  10. PostgreSQL数据库pg_test_timing学习使用
  11. html5弹幕制作(探索ing)
  12. java判断是否微信浏览器_Java判断浏览器是微信还是支付宝
  13. JQuery Validate(1)---电话号码与邮箱验证
  14. java.sql.SQLException: is unrecognized or represents more than one time zone. You must configure
  15. android开发常用的ADB命令
  16. Mac下对小米8刷机Android8.1并安装Magisk和edXposed
  17. 搜狐狐友营销的十二个办法
  18. 美赛2022年O奖经验分享(一)
  19. STM32禁用JTAG,并使用JTAG引脚
  20. python数字规律分析_【小白学爬虫】用Python分析福彩3D|发现数字的秘密

热门文章

  1. 【导航规划】导航规划背景知识总结
  2. 一款基于区块链的全球社交软件OneChat
  3. CListCtrl控件使用方法总结
  4. 分享一些高质量的博客
  5. linux-lsmod、lspci命令释义
  6. oracle power 函数
  7. ADAMS/MATLAB联合仿真机械臂重力补偿问题
  8. 2022年Android面试题及答案收集(不断更新中)
  9. 涉密外业计算机管理和使用情况,吉林省自然资源厅
  10. 速度与颜值的碰撞,评测荣耀V9的“快”与“美”