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" />
0
投稿
猜你喜欢
- code #1private void Form1_SizeChanged(object sender, EventArgs e) //最小
- 本文实例讲述了C#生成随机数功能。分享给大家供大家参考,具体如下:using System;using System.Collections
- 01 高效设置我们先来讲讲有哪些设置调整之后 IDEA 会更好用。先说明一点:这里只是根据我个人喜好和习惯的建议,IDEA 的默认配置已经很
- 目录一、背景二、推荐方式2.1 自定义的枚举2.2 外部枚举三、总结一、背景平时工作开发过程中,难免会用到状态机(状态的流转)。如奖学金审批
- 本文实例讲述了Java实现的质因数分解操作。分享给大家供大家参考,具体如下:这里演示java通过递归实现质因数分解,代码如下:import
- RestTemplate 请求url中包含百分号 会被转义成25最初使用RestTemplate 进行远程调用方法如下:private St
- C#实现:#region 计算字符串相似度 /// &l
- 0. 前言在上一篇中,我故意留下了查询的示范没讲。虽然说可以通过以下代码获取一个DataReader:IDataReader reader
- 前言在项目中,如果我们要遵循分层领域模型规约: 话,肯定避免不了在DTO、VO、BO、AO、VO、Query等实体的转换,我们通常有几种做法
- 一、为何要使用netty开发由于之前已经用Java中的socket写过一版简单的聊天室,这里就不再对聊天室的具体架构进行细致的介绍了,主要关
- 近期由于负责项目的一个模块,该模块下有很多分类,每个分类都有一个编码code,这个值是作为一个参数携带过来的。但是每个code确实对应一个方
- 出现乱码请修改为BufferedReader br = new BufferedReader(new InputStreamReader(n
- 学习java的人都知道spring,springMVC,mybatis等框架,像structs2这样的框架是基于servlet以外实现的,而
- 这篇文章主要介绍了Spring Boot项目维护全局json数据代码实例,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考
- 本文汇总了C#启动外部程序的几种常用方法,非常具有实用价值,主要包括如下几种方法:1. 启动外部程序,不等待其退出。2. 启动外部程序,等待
- 意图:想将项目用到的两个dll库文件(CryptEnDe.dll和ICSharpCode.SharpZipLib.dll)一同编译进exe中
- 实验目的在C#和Rest/Restful以及其它的Web服务交互过程中,大量使用到JSON传递数据,如何快捷的转化C#对象到JSON和转化J
- 描述说明:public class TryCatchStu { /*try catch:自己处理异常 *t
- java 接口回调实例详解首先官方对接口回调的定义是这样的,所谓回调:就是A类中调用B类中的某个方法C,然后B类中反过来调用A类中的方法D,
- 最近因为项目需要,自己实现了个可以自由移动,并且长按可以跳出一个控制播放的,大的悬浮窗。好,开始吧。首先我们先聊权限,悬浮窗需要在manif