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" />

Android--自定义半圆环型进度(带动画)的更多相关文章

  1. Android 自定义 View 圆形进度条总结

    Android 自定义圆形进度条总结 版权声明:本文为博主原创文章,未经博主允许不得转载. 微博:厉圣杰 微信公众号:牙锅子 源码:CircleProgress 文中如有纰漏,欢迎大家留言指出. 最近 ...

  2. Android自定义Activity酷炫的动画跳转效果

    两个Activity跳转的时候,自定义翻页效果: Intent intent = new Intent(FirstActivity.this, SecondActivity.class);   sta ...

  3. 微信小程序之自定义模态弹窗(带动画)实例

    1.基本需求. 实现用户自定义弹框 带动画(动画可做参靠,个人要是觉得不好看可以自定义动画) 获取弹出框的内容,自定义事件获取 2.案例目录结构 二.程序实现具体步骤 1.弹框index.wxml代码 ...

  4. Android自定义圆角矩形进度条2

    效果图: 或 方法讲解: (1)invalidate()方法 invalidate()是用来刷新View的,必须是在UI线程中进行工作.比如在修改某个view的显示时, 调用invalidate()才 ...

  5. Android 自定义圆形旋转进度条,仿微博头像加载效果

    微博 App 的用户头像有一个圆形旋转进度条的加载效果,看上去效果非常不错,如图所示: 据说 Instagram 也采用了这种效果.最近抽空研究了一下,最后实现的效果是这样: 基本上能模拟出个大概,代 ...

  6. 【转】android 自定义ViewPager,修改原动画

    转载请标明出处:http://blog.csdn.net/lmj623565791/article/details/38026503 记 得第一次见到ViewPager这个控件,瞬间爱不释手,做东西的 ...

  7. Android 自定义组件随着手指自动画圆

    首先自定义一个View子类: package com.example.androidtest0.myView; import android.content.Context; import andro ...

  8. android 自定义图片圆形进度条

    感觉话一个圆形进度条挺简单的 ,但是却偏偏给了几张图片让你话,说实话我没接触过,感觉好难,还好百度有大把的资源,一番努力下终于画出来了. 代码如下. package com.etong.cpms.wi ...

  9. Android 自定义弹出框带EditText

    EditText 布局页面 edittext_ownername_dialog.xml: <?xml version="1.0" encoding="utf-8&q ...

随机推荐

  1. JDK中线程组ThreadGroup

    如果线程有100条...分散的不好管理... 线程同样可以分组ThreadGroup类. 线程组表示一个线程的集合.此外,线程组也可以包含其他线程组.线程组构成一棵树,在树中,除了初始线程组外,每个线 ...

  2. (转)linux top命令中各cpu占用率含义及案例分析

    原文:https://blog.csdn.net/ydyang1126/article/details/72820349 linux top命令中各cpu占用率含义 0 性能监控介绍 1 确定应用类型 ...

  3. Install vsftpd on centos

    安装vsftpd程序. sudo yum -y install vsftpd 启动ftp服务. sudo service vsftp start 添加ftp用户,并设置密码. sudo useradd ...

  4. SSH框架整合详细分析【执行流程】

    struts1和spring有两种整合的方法  一种是action和spring bean映射:一种是将action交给spring初始化 第一种方式:访问.do的URL->tomcat接收到r ...

  5. 自然语言处理--LDA主题聚类模型

    LDA模型算法简介: 算法 的输入是一个文档的集合D={d1, d2, d3, ... , dn},同时还需要聚类的类别数量m:然后会算法会将每一篇文档 di 在 所有Topic上的一个概率值p:这样 ...

  6. 前端组件化Polymer入门教程(1)——初识&&安装

    前端组件化Polymer入门教程目录: 前端组件化Polymer入门教程(1)--初识&&安装 前端组件化Polymer入门教程(2)--快速入门 前端组件化Polymer入门教程(3 ...

  7. 散列算法-MD5

    信息摘要技术把明文内容按某种规则生成一段哈西值,即使明文消息只改动了一点点,生成的结果也会完全不同. MD5(Message-digest algorithm 5)就是信息摘要的一种实现,它可以从任意 ...

  8. VC++记录

    1. 记录时间 #include <atlstr.h>#include <time.h>clock_t clockBegin, clockEnd; clockBegin = c ...

  9. git remote: HTTP Basic: Access denied 错误解决办法

    问题描述: git push 报 HTTP Basic: Access denied 错误 原因:本地git配置的用户名.密码与gitlabs上注册的用户名.密码不一致. 解决方案: 1. 如果账号密 ...

  10. python模块之xlrd

    python处理excel的模块,xlrd读取excel,xlwt写入excel 一.安装 pip install xlrd 二.使用 1. 打开excel,得到Book对象 import xlrd ...