1.效果

2.实现

2.1自定义属性

在res/values 文件夹中新建xx.xml,内容如下

<?xml version="1.0" encoding="utf-8"?>
<resources>
<!--自定义QQ步数控件-->
<declare-styleable name="QQStepView">
<!--外圈的颜色-->
<attr name="outerColor" format="color" />
<!--内圈的额颜色-->
<attr name="innerColor" format="color" />
<!--外圈的宽度-->
<attr name="outerWidth" format="dimension" />
<!--内圈的宽度-->
<attr name="innerWidth" format="dimension" />
<!--最大步数-->
<attr name="stepMax" format="integer" />
<!--中间文字的大小-->
<attr name="stepTextSize" format="dimension" />
<!--中间文字的颜色-->
<attr name="stepTextColor" format="color" />
</declare-styleable>
</resources>

2.2继承view实现java代码

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Rect;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View; import androidx.annotation.Nullable; /**
* Created by DongKing on 2020/11/6
* Version 1.0
* Describe:qq步数
*/
class QQStepView extends View {
private int mStepMax = 10000;//默认最大值
private int mCurrentStep = 0;//当前值
private int mOuterColor = Color.RED;
private int mInnerColor = Color.BLUE;
private int mOuterWidth = 30;
private int mInnerWidth = 18;
private int mStepTextSize = 18;
private int mStepTextColor = Color.LTGRAY; Paint mInnerPaint;//画内圆的画笔
Paint mOuterPaint;//画外圆的画笔
Paint mTextPaint;//画文字的画笔 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);
//获取自定义属性
TypedArray array = context.obtainStyledAttributes(attrs, R.styleable.QQStepView);
mOuterColor = array.getColor(R.styleable.QQStepView_outerColor, mOuterColor);
mInnerColor = array.getColor(R.styleable.QQStepView_innerColor, mInnerColor);
mOuterWidth = (int) array.getDimension(R.styleable.QQStepView_outerWidth, mOuterWidth);
mInnerWidth = (int) array.getDimension(R.styleable.QQStepView_innerWidth, mInnerWidth);
mStepTextSize = array.getDimensionPixelSize(R.styleable.QQStepView_stepTextSize, mStepTextSize);
mStepTextColor = array.getColor(R.styleable.QQStepView_stepTextColor, mStepTextColor);
mStepMax = array.getInt(R.styleable.QQStepView_stepMax, mStepMax); array.recycle();
//初始化画笔
mOuterPaint = new Paint();
mOuterPaint.setAntiAlias(true);
mOuterPaint.setStrokeCap(Paint.Cap.ROUND);
mOuterPaint.setColor(mOuterColor);
mOuterPaint.setStrokeWidth(mOuterWidth);
mOuterPaint.setStyle(Paint.Style.STROKE);
mInnerPaint = new Paint();
mInnerPaint.setAntiAlias(true);
mInnerPaint.setStrokeCap(Paint.Cap.ROUND);
mInnerPaint.setColor(mInnerColor);
mInnerPaint.setStrokeWidth(mInnerWidth);
mInnerPaint.setStyle(Paint.Style.STROKE);
mTextPaint = new Paint();
mTextPaint.setAntiAlias(true);
mTextPaint.setTextSize(mStepTextSize);
mTextPaint.setColor(mStepTextColor);
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec); //保证是一个正方形
int width = MeasureSpec.getSize(widthMeasureSpec);
int height = MeasureSpec.getSize(heightMeasureSpec);
setMeasuredDimension(width > height ? height : width, width > height ? height : width);
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
int center = getWidth() / 2;
//减去比较大的那个的一半 -->防止超出边界
int radius = center - (mOuterWidth > mInnerWidth ? mOuterWidth : mInnerWidth) / 2;
RectF rectF = new RectF(center - radius, center - radius, center + radius, center + radius);
//1.画里面的圆弧
canvas.drawArc(rectF, 135, 270, false, mInnerPaint);
//2.画外面的圆弧
if (mStepMax == 0) {
return;
} float sweepAngle = (float) mCurrentStep / mStepMax * 270;
canvas.drawArc(rectF, 135, sweepAngle, false, mOuterPaint);
//3.画文字
Rect rect = new Rect();
String stepText = mCurrentStep + "";
mTextPaint.getTextBounds(stepText, 0, stepText.length(), rect);
int dx = getWidth() / 2 - rect.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);
} public synchronized void setCurrentStep(int currentStep) {
this.mCurrentStep = currentStep;
invalidate();
}
}

3.使用

3.1 布局文件中

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:myapp="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<com.nb.customview.QQStepView
android:id="@+id/qq_step_view"
android:layout_width="180dp"
android:layout_height="180dp"
android:layout_gravity="center_horizontal"
myapp:innerColor="@android:color/holo_green_dark"
myapp:innerWidth="10dp"
myapp:outerColor="@android:color/holo_blue_light"
myapp:outerWidth="12dp"
myapp:stepMax="10000"
myapp:stepTextColor="@android:color/holo_blue_light"
myapp:stepTextSize="30sp" />
</LinearLayout>

3.2 activity中

public class MainActivity extends AppCompatActivity {
QQStepView mQqStepView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mQqStepView = findViewById(R.id.qq_step_view);
init(); } private void init() {
ValueAnimator valueAnimator = ObjectAnimator.ofInt(0, 8000);
valueAnimator.setDuration(3000);
valueAnimator.setInterpolator(new DecelerateInterpolator());
valueAnimator.addUpdateListener(animation -> {
int currentStep = (int) animation.getAnimatedValue();
mQqStepView.setCurrentStep(currentStep);
});
valueAnimator.start();
} }

2.自定义view-QQ运动步数的更多相关文章

  1. QQ运动步数&自定义ProgressBar

    效果如下 gif图展示效果不好,实际体验无卡顿 1.自定义属性 早Values目录下New-values resource file,命名为attrs.xml(命名随意,但规范命名为attrs.xml ...

  2. Android自定义View——QQ音乐中圆形旋转碟子

    1.在onMeasure中测量整个View的宽和高后,设置宽高 2.获取我们res的图片资源后,在ondraw方法中进行绘制圆形图片 3.通过Handler发送Runnable在主线程中更新UI,达到 ...

  3. Android自定义View实现仿QQ实现运动步数效果

    效果图: 1.attrs.xml中 <declare-styleable name="QQStepView"> <attr name="outerCol ...

  4. Android 自定义View实现QQ运动积分抽奖转盘

    因为偶尔关注QQ运动, 看到QQ运动的积分抽奖界面比较有意思,所以就尝试用自定义View实现了下,原本想通过开发者选项查看下界面的一些信息,后来发现积分抽奖界面是在WebView中展示的,应该是在H5 ...

  5. 手把手教你修改iOS版QQ的运动步数

    手把手教你修改iOS版QQ的运动步数 现在很多软件都加上了运动模块,比如QQ和微信,而且还有排行榜,可以和好友比较谁的运动步数多,任何东西只要添加了比较功能,就变得不一样了.今天教大家用代码去修改QQ ...

  6. 安卓自定义View实现图片上传进度显示(仿QQ)

    首先看下我们想要实现的效果如下图(qq聊天中发送图片时的效果): 再看下图我们实现的效果: 实现原理很简单,首先我们上传图片时需要一个进度值progress,这个不管是自己写的上传的方法还是使用第三方 ...

  7. wing带你玩转自定义view系列(2) 简单模仿qq未读消息去除效果

    上一篇介绍了贝塞尔曲线的简单应用 仿360内存清理效果 这一篇带来一个  两条贝塞尔曲线的应用 : 仿qq未读消息去除效果. 转载请注明出处:http://blog.csdn.net/wingicho ...

  8. 自定义View,随着手指运动的小球

    这个实例是自定的view的初步介绍,要设计的是一个随着手指运动的小球.原理是随时获取手指的坐标,然后在这个坐标上面实时改变自定义view的坐标.这个view仅仅是画了一个圆形而已. 自定义的view ...

  9. 2017了,回家前 "年末" 分享:下雨,飘雪,红包雨,碰撞球,自定义View

    (本博客为原创:http://www.cnblogs.com/linguanh/) 目录: 效果展示 感想 代码拆解 开源地址 效果展示 有没有兴趣继续看下去,直接看下"颜值"是第 ...

  10. 高阶自定义View --- 粒子变幻、隧道散列、组合文字

    高阶自定义View --- 粒子变幻.隧道散列.组合文字 作者:林冠宏 / 指尖下的幽灵 掘金:https://juejin.im/user/587f0dfe128fe100570ce2d8 博客:h ...

随机推荐

  1. Linux无法新增用户

    1.查看当前用户是否有权限创建用户 2.磁盘空间不足,vi打开/etc/passwd 报: E297: Write error in swap file"adduser.sh" 1 ...

  2. SpringCloud 源码系列(1)—— 注册中心 Eureka(上)

    Eureka 是 Netflix 公司开源的一个服务注册与发现的组件,和其他 Netflix 公司的服务组件(例如负载均衡.熔断器.网关等)一起,被 Spring Cloud 整合为 Spring C ...

  3. 思维导图学 Kotlin

    前言 最近做了<Kotlin实战>的思维导图笔记,Kotlin真香-- 目录 基础 函数 类.对象 λ表达式 类型 约定 高阶函数.泛型 公众号 coding 笔记.点滴记录,以后的文章也 ...

  4. macos brew zookeeper,安装后zookeeper启动失败?

    一.Zookeeper安装流程 执行如下安装命令: brew install zookeeper 执行截图如下: 安装后查看 zookeeper 安装信息(默认拉取最新版本) brew info zo ...

  5. Spring Boot 集成 MQTT

    本文代码有些许问题,处理方案见:解决 spring-integration-mqtt 频繁报 Lost connection 错误 一.添加配置 spring: mqtt: client: usern ...

  6. 我在苦苦坚持的时候,WebStorm已经悄悄的“真香”起来

    前言 最近接了一个活儿,是用WebStorm开发一个基于VUE的网站,但是我真的是几乎没接触过VUE相关的项目实践,更别说用WebStorm在实际中的应用,之前只是听朋友说多好用,但是,因为现有工具不 ...

  7. 5.1 Spring5源码--Spring AOP源码分析一

    目标: 1.什么是AOP, 什么是AspectJ, 2. 什么是Spring AOP 3. Spring AOP注解版实现原理 4. Spring AOP切面原理解析 一. 认识AOP 1.1 什么是 ...

  8. 老猿学5G扫盲贴:中国移动的5G计费架构解读

    专栏:Python基础教程目录 专栏:使用PyQt开发图形界面Python应用 专栏:PyQt入门学习 老猿Python博文目录 老猿学5G博文目录 一.引言 在<老猿学5G扫盲贴:3GPP中的 ...

  9. 第12.1节 Python os模块导览

    os 模块提供了许多与操作系统交互的函数,一定要使用 import os 而不是 from os import * ,这将避免内建的 open() 函数被 os.open() 隐式替换掉,它们的使用方 ...

  10. PyQt(Python+Qt)学习随笔:视图中的dragDropMode属性对dragEnabled和acceptDrops属性的影响

    老猿Python博文目录 老猿Python博客地址 在<PyQt(Python+Qt)学习随笔:QAbstractItemView的dragEnabled和dragDropMode属性的关系&g ...