Android自定义View仿QQ运动步数效果
作者:AD钙奶-lalala 发布时间:2021-06-25 00:11:37
标签:Android,QQ,运动步数
本文实例为大家分享了Android QQ运动步数的具体代码,供大家参考,具体内容如下
今天我们实现下面这样的效果:
首先自定义属性:
<?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);
}
@Override
protected 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));
}
@Override
protected 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.MyQQStep
android: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() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
float p = animation.getAnimatedFraction();
qq_view.setPercent(p,5000*p);
}
});
animator.setDuration(10000);
animator.start();
}
来源:https://blog.csdn.net/qq_36428821/article/details/102856162


猜你喜欢
- 本文实例讲述了Android控件之ListView用法。分享给大家供大家参考。具体如下:示例一:在android开发中ListView是比较
- 在 Servlet/Jsp 项目中,如果涉及到系统任务,例如在项目启动阶段要做一些数据初始化操作,这些操作有一个共同的特点,只在项目启动时进
- 微信红包的使用已经很广泛,本篇文章介绍了微信发红包的实例,需要有认证的公众号,且开通了微信支付,商户平台且开通了现金红包的权限即可。http
- 如何解决某个节点故障的问题?如何解决数据一致性的问题?如何解决数据倾斜的问题?CAP理论先从定义开始:C(Consistence):一致性所
- Android实现简单音乐播放器(MediaPlayer),供大家参考,具体内容如下开发工具:Andorid Studio 1.3运行环境:
- 做直播,音视频通讯。经常需要通过rtp协议封装音视频数据来发送。网上找到的基本都是c或c++版本的,没有JAVA版本的。就算千辛万苦找到一篇
- windows系统中的画板工具,有好几种画刷,C#中并没有直接对应可使用的类,只能自己研究。1.画刷原理根据本人对PS的相关功能细心分析,发
- 前言先说缓存,合理使用缓存是优化中最常见的,将从数据库中查询出来的数据放入缓存中,下次使用时不必从数据库查询,而是直接从缓存中读取,避免频繁
- 委托给了C#操作函数的灵活性,我们可使用委托像操作变量一样来操作函数,其实这个功能并不是C#的首创,早在C++时代就有函数指针这一说法,而在
- 在Mms中每个Thread都有其相应的联系人,但是threads表中并没有直接保存联系人的信息(号码或名字),而是保存一个叫做recipie
- 代码如下所示:using System;using System.Collections.Generic;using System.Linq
- 这里写链接内容仿映客送小礼物的特效,顺便复习一下属性动画,话不多说先看效果图。需求分析可以看到整个动画有几部分组成,那我们就把每个部分拆分出
- 详解Kotlin的空指针处理Kotlin的空指针处理相比于java有着极大的提高,可以说是不用担心出现NullPointerExceptio
- 平时开发中经常遇到的很小的问题,这里记录一下。一般在AndroidManifest.xml中添加了android:windowSoftInp
- 本文实例讲述了Java基于解释器模式实现定义一种简单的语言功能。分享给大家供大家参考,具体如下:一 模式定 * 释器模式:就是给定一个语言的文
- 泛型的概述和优势泛型概述泛型:是JDK5中引入的特性,可以在编译阶段约束操作的数据类型,并进行检查。泛型的格式:<数据类型>;
- 包的内容包的内容应该仔细设计,使其只包含在功能上相关的类和接口。包中的类可以自由地访问该包中其他类的非私有成员,有些类甚至可能有足够的权限去
- 需求前台有日期字符串的数据,提交到后台。后台实体类使用Date属性接收。日期字符串有多种格式,需要用一个转换器将合法的日期字符串格式转换为D
- 面试课题 Spring boot AOPSpring boot 中 AOP是其中 重要的特性,其实现的方式借助的 * + Proxy 动态
- 项目描述:这是一个基于SpringBoot+Vue框架开发的仿小米电子产品售卖商城系统。首先,这是一个前后端分离的项目,代码简洁规范,注释说