今天我们实现下面这样的效果:

首先自定义属性:

<?xml version="1.0" encoding="utf-8"?>
<resources><declare-styleable name="MyQQStep"><attr name="out_color" format="color"/><attr name="inner_color" format="color"/><attr name="border_width" format="dimension"/><attr name="text_size" format="dimension"/><attr name="text_color" format="color"/></declare-styleable>
</resources>

自定义View代码如下:

/*** Created by Michael on 2019/11/1.*/public class MyQQStep extends View {private int out_color;private int inner_color;private float width;private float textSize;private int color;private int width01;private int height01;private Paint outPaint;private Paint innerPaint;private Paint textPaint;private float percent;private int step;public MyQQStep(Context context) {this(context,null);}public MyQQStep(Context context, @Nullable AttributeSet attrs) {this(context, attrs,0);}public MyQQStep(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {super(context, attrs, defStyleAttr);TypedArray array = context.obtainStyledAttributes(attrs,R.styleable.MyQQStep);out_color = array.getColor(R.styleable.MyQQStep_out_color, Color.BLACK);inner_color = array.getColor(R.styleable.MyQQStep_inner_color, Color.RED);width = array.getDimension(R.styleable.MyQQStep_border_width,10);textSize = array.getDimensionPixelSize(R.styleable.MyQQStep_text_size,20);color = array.getColor(R.styleable.MyQQStep_text_color, Color.GREEN);array.recycle();initPaint();percent = 0;step = 5000;}private void initPaint() {outPaint = new Paint();outPaint.setAntiAlias(true);outPaint.setStyle(Paint.Style.STROKE);outPaint.setStrokeWidth(width);outPaint.setColor(out_color);outPaint.setStrokeCap(Paint.Cap.ROUND);innerPaint = new Paint();innerPaint.setAntiAlias(true);innerPaint.setStrokeWidth(width);innerPaint.setStyle(Paint.Style.STROKE);innerPaint.setColor(inner_color);innerPaint.setStrokeCap(Paint.Cap.ROUND);textPaint = new Paint();textPaint.setAntiAlias(true);textPaint.setColor(color);textPaint.setStyle(Paint.Style.STROKE);textPaint.setTextSize(textSize);}@Overrideprotected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {//super.onMeasure(widthMeasureSpec, heightMeasureSpec);int widthMode = MeasureSpec.getMode(widthMeasureSpec);int heightMode = MeasureSpec.getMode(heightMeasureSpec);if (widthMode == MeasureSpec.AT_MOST){}else{width01 = MeasureSpec.getSize(widthMeasureSpec);}if (heightMode == MeasureSpec.AT_MOST){}else{height01 = MeasureSpec.getSize(heightMeasureSpec);}setMeasuredDimension((width01>height01?height01:width01),(width01>height01?height01:width01));}@Overrideprotected void onDraw(Canvas canvas) {super.onDraw(canvas);int realWidth = getWidth()>getHeight()?getHeight():getWidth();int realHeight = getWidth()>getHeight()?getHeight():getWidth();RectF r1 = new RectF(width/2,width/2,realWidth-width/2,realHeight-width/2);canvas.drawArc(r1,135,270,false,outPaint);canvas.drawArc(r1,135,270*percent,false,innerPaint);Rect r = new Rect();String s = step+"";textPaint.getTextBounds(s,0,s.length(),r);int textWidth = r.width();int textHeight = r.height();Paint.FontMetricsInt fontMetricsInt = new Paint.FontMetricsInt();int dy = (fontMetricsInt.bottom-fontMetricsInt.top)/2-fontMetricsInt.bottom;int baseLine = textHeight/2+dy+realHeight/2-textHeight/2;int x0 = realWidth/2-textWidth/2;canvas.drawText(s,x0,baseLine,textPaint);}public void setPercent(float percent,float value){this.percent = percent;this.step = (int) value;invalidate();}}

最后在布局以及MainActivity中调用:

<com.example.qq_step.MyQQStepandroid:id="@+id/qq_step"android:layout_width="match_parent"android:layout_height="match_parent"app:out_color="@color/colorAccent"app:border_width="10dp"app:inner_color="@color/colorPrimary"app:text_size="20sp"app:text_color="@color/colorPrimaryDark"/>
 private void initView() {final MyQQStep qq_view = findViewById(R.id.qq_step);ValueAnimator animator = ValueAnimator.ofFloat(0,5000);animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {@Overridepublic void onAnimationUpdate(ValueAnimator animation) {float p = animation.getAnimatedFraction();qq_view.setPercent(p,5000*p);}});animator.setDuration(10000);animator.start();}

自定义View - 仿QQ运动步数效果相关推荐

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

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

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

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

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

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

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

    前言 今天接着上一篇来写关于自定义View方面的东西,我是近期在学习整理这方面的知识点,所以把相关的笔记都放到这个Android自定义View的专栏里了,方便自己下次忘记的时候能回来翻翻,今天的内容是 ...

  5. Android自定义View之仿QQ运动步数进度效果

    文章目录 前言 先看效果图 ![在这里插入图片描述](https://img-blog.csdnimg.cn/6e4ddec17933496ea4830fa08d8ffbe5.png?x-oss-pr ...

  6. android 自定义view实现仿QQ运动步数进度效果

    最近公司在策划一个新的项目,原型还没出来,再说这公司人都要走没了,估计又要找工作了,所以必须要学习,争取每个写个关于自定义view方面的,这样几个月积累下来,也能学习到东西,今天就带来简单的效果,就是 ...

  7. android 自定义园动画,Android 自定View实现仿QQ运动步数圆弧及动画效果

    在之前的Android超精准计步器开发-Dylan计步中的首页用到了一个自定义控件,和QQ运动的界面有点类似,还有动画效果,下面就来讲一下这个View是如何绘制的. 1.先看效果图 2.效果图分析 功 ...

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

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

  9. 自定义view 仿qq步数 半圆弧

    先看效果图: 自定义属性 <declare-styleable name="QQSteps"><attr name="roundWidth" ...

最新文章

  1. pandas的series和dataframe
  2. 《数据分析实战 基于EXCEL和SPSS系列工具的实践》一3.4 数据量太大了怎么办
  3. 见证IBM如何重新定义企业基础架构
  4. Java实现1到n的倒数的累加和
  5. numeric column can contains null
  6. boost::hana::slice_c用法的测试程序
  7. 02/03_Pytorch安装、Conda安装Pythorch,换源、pytorch官网、验证、安装jupyter、卸载、安装、启动jupyter、配置Jupyter notebook、使用
  8. (1)编译安装lamp三部曲之apache-技术流ken
  9. mailcore -- POP
  10. Qt工作笔记-qmake和uic(对Qt底层的进一步认识)
  11. Qt文档阅读笔记-Qt Concurrent介绍及简单使用
  12. Android进阶2之图片倒影效果 .
  13. 写好数据分析报告,数据的思路非常重要
  14. python扫雷算法_python实战教程之自动扫雷(自己存下来学习之用)
  15. 前端 JavaScript 实现一个简易计算器
  16. [Python] 根据提供的函数对指定序列做映射:map() 函数
  17. PTA--03-树2 List Leaves
  18. Linux环境下使用阿里云盘
  19. 电梯plc的io分配_三菱Q系列PLC的io分配
  20. 【电化学】-物质传递(迁移与扩散)

热门文章

  1. LSK理论、系统及应用目标规划简介
  2. matlab代码保密:pcode *.m
  3. python判断按键是否按下_python – 如何检查键修饰符是否被按下(shift,ctrl,alt)?
  4. 发酵罐设计软件测试,发酵罐设计的心得体会
  5. Pytest的基本使用
  6. win10忘记密码,重装系统
  7. hdu 2629 Identity Card (字符串解析模拟题)
  8. Unity3D自带案例AngryBots分析(二)——人物动作控制逻辑
  9. 动态动作系统——一种全新的游戏人物动作制作思路
  10. 从前端技术到体验科技(附演讲视频)