Android自定义带动画的半圆环型进度效果
作者:灬布衣丶公爵丨 发布时间:2022-02-08 09:31:15
标签:Android,进度
本文实例为大家分享了Android半圆环型进度效果的具体代码,供大家参考,具体内容如下
package com.newair.ondrawtext;
import android.animation.ValueAnimator;
import android.annotation.TargetApi;
import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.RectF;
import android.graphics.Typeface;
import android.os.Build;
import android.util.AttributeSet;
import android.view.View;
import android.view.animation.OvershootInterpolator;
/**
* Created by ouhimehime on 16/6/15.
* --------自定义控件-------
*/
public class CustomView extends View {
//画笔
private Paint paint;
private RectF oval;
//圆弧颜色
private int roundColor;
//进度颜色
private int progressColor;
//文字内容
private boolean textIsShow;
//字体大小
private float textSize = 14;
//文字颜色
private int textColor;
//最大进度
private int max = 1000;
//当前进度
private int progress = 300;
//圆弧宽度
private int roundWidth = 30;
private int viewWidth; //宽度--控件所占区域
private float nowPro = 0;//用于动画
private ValueAnimator animator;
public CustomView(Context context) {
super(context);
}
public CustomView(Context context, AttributeSet attrs) {
super(context, attrs);
initAttrs(attrs, context);
}
public CustomView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
initAttrs(attrs, context);
}
@TargetApi(Build.VERSION_CODES.LOLLIPOP)
public CustomView(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initAttrs(attrs, context);
}
private void initAttrs(AttributeSet attr, Context context) {
TypedArray array = context.obtainStyledAttributes(attr, R.styleable.CustomView);
roundColor = array.getColor(R.styleable.CustomView_roundColor, Color.BLACK);//环形颜色
progressColor = array.getColor(R.styleable.CustomView_progressColor, Color.RED);//进度颜色
textIsShow = array.getBoolean(R.styleable.CustomView_textIsShow, false);//文字
textSize = array.getDimension(R.styleable.CustomView_textSize, 14);//文字大小
textColor = array.getColor(R.styleable.CustomView_textColor, Color.BLACK);//文字颜色
roundWidth = array.getInt(R.styleable.CustomView_roundWidth, 30);//圆环宽度
array.recycle();
//动画
animator = ValueAnimator.ofFloat(0, progress);
animator.setDuration(1800);
animator.setInterpolator(new OvershootInterpolator());
animator.addUpdateListener(new ValueAnimator.AnimatorUpdateListener() {
@Override
public void onAnimationUpdate(ValueAnimator animation) {
nowPro = (float) animation.getAnimatedValue();
postInvalidate();
}
});
animator.start();
}
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
final int widthSpecMode = MeasureSpec.getMode(widthMeasureSpec);
final int widthSpecSize = MeasureSpec.getSize(widthMeasureSpec);
if (widthSpecMode == MeasureSpec.AT_MOST) {//可获得最大空间
setMeasuredDimension(widthMeasureSpec, (widthSpecSize / 2) + (int) (Math.cos(20) * (widthSpecSize / 2)));
} else if (widthMeasureSpec == MeasureSpec.EXACTLY) {//一般指精确值
setMeasuredDimension(widthMeasureSpec, (widthSpecSize / 2) + (int) (Math.cos(20) * (widthSpecSize / 2)));
} else {
setMeasuredDimension(widthMeasureSpec, (viewWidth / 2) + (int) (Math.cos(20) * (viewWidth / 2)));
}
}
@Override
protected void onSizeChanged(int w, int h, int oldw, int oldh) {
super.onSizeChanged(w, h, oldw, oldh);
viewWidth = w;//得到宽度以此来计算控件所占实际大小
//计算画布所占区域
oval = new RectF();
oval.left = roundWidth + getPaddingLeft();
oval.top = roundWidth + getPaddingTop();
oval.right = viewWidth - roundWidth - getPaddingRight();
oval.bottom = viewWidth - roundWidth - getPaddingBottom();
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
Paint paint = new Paint();
paint.setAntiAlias(true); //设置画笔为无锯齿
paint.setColor(roundColor); //设置画笔颜色
paint.setStrokeWidth(roundWidth); //线宽
paint.setStyle(Paint.Style.STROKE); //空心
canvas.drawArc(oval, 160, 220, false, paint); //绘制圆弧
//画进度层
paint.setColor(progressColor);
paint.setStrokeWidth(roundWidth + 1);
canvas.drawArc(oval, 160, 220 * nowPro / max, false, paint); //绘制圆弧
if (textIsShow) {
paint.setColor(textColor);
paint.setStrokeWidth(0);
paint.setTypeface(Typeface.DEFAULT);
paint.setTextSize(textSize * 2);
float textWidth = paint.measureText((int) ((nowPro / (float) max) * 100) + "%");
canvas.drawText((int) ((nowPro / (float) max) * 100) + "%", viewWidth / 2 - textWidth / 2, viewWidth / 2, paint);
}
}
private int getDefaultHeight() {
return 0;
}
private int getDefaultWidth() {
return 0;
}
public int getRoundColor() {
return roundColor;
}
public void setRoundColor(int roundColor) {
this.roundColor = roundColor;
}
public int getProgressColor() {
return progressColor;
}
public void setProgressColor(int progressColor) {
this.progressColor = progressColor;
}
public boolean getText() {
return textIsShow;
}
public void setText(boolean text) {
this.textIsShow = text;
}
public float getTextSize() {
return textSize;
}
public void setTextSize(float textSize) {
this.textSize = textSize;
}
public int getTextColor() {
return textColor;
}
public void setTextColor(int textColor) {
this.textColor = textColor;
}
public int getMax() {
return max;
}
public void setMax(int max) {
this.max = max;
}
public int getProgress() {
return progress;
}
public void setProgress(int progress) {
this.progress = progress;
}
}
自定义属性
<declare-styleable name="CustomView">
<attr name="roundColor" format="color" />
<attr name="progressColor" format="color" />
<attr name="textIsShow" format="boolean" />
<attr name="textSize" format="dimension" />
<attr name="textColor" format="color" />
<attr name="roundWidth" format="integer" />
</declare-styleable>
用法
<com.newair.ondrawtext.CustomView
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:visibility="visible"
app:progressColor="@android:color/holo_orange_dark"
app:roundColor="@android:color/holo_blue_dark"
app:roundWidth="45"
app:textColor="@android:color/black"
app:textIsShow="true"
app:textSize="14sp" />


猜你喜欢
- 任何一个类都是Class类的实例对象,这个实例对象有三种表示方式第一种表示方式(任何一个类都有一个隐含的静态成员变量class):Class
- public string check_number(string num_str)
- 本文实例讲述了Android编程实现canvas绘制饼状统计图功能。分享给大家供大家参考,具体如下:本例的目的是实现一个简单的饼状统计图,效
- 目录1、关于struts框架,下面那些说法是正确的?2、java语言中,按照一定格式生成程序的文档的工具是?3、根据下面的程序代码,哪些选项
- 一:获取根目录的方法取得控制台应用程序的根目录方法方法1、Environment.CurrentDirectory 取得或设置当前工作目录的
- 一、前言序列化:将对象转换为二进制序列在网络中传输或保存到磁盘反序列化:从网络或磁盘中将二进制序列转换为对象注意:对象必须实现Seriali
- 平时项目中只要涉及表,那么一定能接触到众多各式各样的ID编号,博主整理一些常用的ID格式,整合一个ID生成工具类,供大家参考,如果有什么不足
- 需求在前面的文章里使用.NET 6开发TodoList应用之领域实体创建原理和思路,我们留了一个坑还没有填上,就是在数据库保存的时
- 1 常量定义在程序中存在大量的数据来代表程序的状态,其中有些数据在程序运行过程中值不能发生改变,这些数据在程序中被叫做常量。2 常量语法命名
- 一、问题重现1.配置文件spring: #DataSource数据源 datasource: &nbs
- 上一篇文章我们介绍了Apache Commons Math3学习之数值积分实例代码,这里给大家分享math3多项式曲线拟合的相关内容,具体如
- 1 数据响应  数据响应一般分为两种:页面响应和数据响应,一般来说页面响应是用来开发一些单体项目(也就是
- 本文实例为大家分享了android通过NFC读取卡号的具体代码,供大家参考,具体内容如下1.获取权限<uses-permission
- 背景由于前前前阵子写了个壳,得去了解类的加载流程,当时记了一些潦草的笔记。这几天把这些东西简单梳理了一下,本文分析的代码基于Android8
- 举个例子Map < String , Object > jsonMap = new HashMap< String , O
- CamShift算法全称是“Continuously Adaptive Mean-Shift”(连续的自适应MeanShift算法),是对M
- 很多的Android入门程序猿来说对于Android自定义View,可能都是比较恐惧的,但是这又是高手进阶的必经之路,所有准备在自定义Vie
- 目录一 、EasyExcel简介二、常用注解三、依赖四、监听五、接口导入Excel六、接口 导出Excel (HttpServletResp
- 目录一 为什么要用锁二 synchronized怎么实现的三 CAS来者何人四synchronized和CAS孰优孰劣轻量级锁重量级锁总结提
- 本文实例讲述了Android编程实现圆角边框布局效果的方法。分享给大家供大家参考,具体如下:这里用的是TableLayout布局的。先看效果