自定义view 仿qq步数 半圆弧
先看效果图:
自定义属性
<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步数 半圆弧相关推荐
- 自定义View | 仿QQ运动步数进度效果
项目GitHub地址 思路 固定不动的蓝色大圆弧 动画变动的红色小圆弧 中间的步数文字显示 相关的自定义属性 比如固定不动的大圆弧, 我们不能写死他的蓝色颜色属性, 要提供一个颜色的自定义属性给用户自 ...
- android的动态tab,Android自定义view仿QQ的Tab按钮动画效果(示例代码)
话不多说 先上效果图 实现其实很简单,先用两张图 一张是背景的图,一张是笑脸的图片,笑脸的图片是白色,可能看不出来.实现思路:主要是再触摸view的时候同时移动这两个图片,但是移动的距离不一样,造成的 ...
- 自定义View 仿QQ运动步数进度效果
1. 概述 我记得QQ之前是有一个,运动步数的进度效果,今天打开QQ一看发现没有了.具体效果我也不清楚了,我就按照自己大概的印象写一下,这一期我们主要是熟悉Paint画笔的使用: 2. 效果 ...
- android自定义计步器形状,Android自定义View仿QQ运动步数效果
本文实例为大家分享了Android QQ运动步数的具体代码,供大家参考,具体内容如下 今天我们实现下面这样的效果: 首先自定义属性: 自定义View代码如下: /** * Created by Mic ...
- oracle number型步数,Android自定义View仿QQ计步器
自定义计步器 Android自定义View是Android开发中比较重要的一项,也是很多开发者比较怕的一个东西.其实只要认真去学习,自定义View其实没有那么可怕:相反的,我们还能从自定义View中找 ...
- 自定义View - 仿QQ运动步数效果
今天我们实现下面这样的效果: 首先自定义属性: <?xml version="1.0" encoding="utf-8"?> <resourc ...
- Android自定义view仿QQ的Tab按钮动效
话不多说 先上效果图 实现其实很简单,先用两张图 一张是背景的图,一张是笑脸的图片,笑脸的图片是白色,可能看不出来.实现思路:主要是再触摸view的时候同时移动这两个图片,但是移动的距离不一样,造成的 ...
- 超简单仿QQ步数显示控件
本着写文即学习的态度,记录下自定义一款超简单仿QQ步数加载控件,话不多数先看图. 一,特性: 1,外圈大圆.进度圆和数字显示的颜色均可以自定义 2,设置最大值和当前值 3,设置加载持续时间 二,实现: ...
- Android仿支付宝UI功能开发,Android 自定义view仿支付宝咻一咻功能
支付宝上有一个咻一咻的功能,就是点击图片后四周有水波纹的这种效果,今天也写一个类似的功能. 效果如下所示: 思路: 就是几个圆的半径不断在变大,这个可以使用动画缩放实现,还有透明动画 还有就是这是好几 ...
最新文章
- python调用zabbix api接口实时展示数据
- jquery之empty()与remove([expr])区别
- php golang 加密 对接,把php的加密算法转为go语言
- 查看pcl版本 linux,Ubuntu16上安装PCL
- mybatis-plus忽略映射字段
- python3 安装opencv_树莓派安装Python3的OpenCV
- vsCode 快捷键、插件
- solidworks二次开发 学习日记--1 开发方式
- 阿里巴巴“牛逼”了,申请“行政干预”区块链专利
- PostgreSQL数据库pg_test_timing学习使用
- html5弹幕制作(探索ing)
- java判断是否微信浏览器_Java判断浏览器是微信还是支付宝
- JQuery Validate(1)---电话号码与邮箱验证
- java.sql.SQLException: is unrecognized or represents more than one time zone. You must configure
- android开发常用的ADB命令
- Mac下对小米8刷机Android8.1并安装Magisk和edXposed
- 搜狐狐友营销的十二个办法
- 美赛2022年O奖经验分享(一)
- STM32禁用JTAG,并使用JTAG引脚
- python数字规律分析_【小白学爬虫】用Python分析福彩3D|发现数字的秘密