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. 面试基础知识集合(python、计算机网络、操作系统、数据结构、数据库等杂记)

    python python _.__.__xx__之间的差别 python中range.xrange和randrange的区别 python中 =.copy.deepcopy的差别 python 继承 ...

  2. ASP.NET Core 中使用 GrayLog 记录日志

    使用 UDP 协议发送日志 自定义好的查询 key 存储数据,尽量不要使用 graylog2-server 服务端格式化日志再存储 Ubuntu 安装服务端 sudo apt-get update & ...

  3. 全网最详细的HBase启动以后,HMaster进程启动了,几秒钟以后自动关闭问题的解决办法(图文详解)

    不多说,直接上干货! 问题详情 情况描述如题所示,hbase启动以后,HMaster进程启动了,几秒钟以后自动关闭,但是HRegionServer进程正常运行: 解决办法: 1.检查下每台机器的时间是 ...

  4. Hibernate的Api以及三种查询方式

    Hibernate  Api |-- Configuration       配置管理类对象 config.configure();    加载主配置文件的方法(hibernate.cfg.xml) ...

  5. 微信小程序——豆瓣电影——(1):基础入门

    准备 Demo 项目地址 https://github.com/zce/weapp-demo Clone or Download(需准备GIT环境) $ cd path/to/project/root ...

  6. mysql索引总结(1)-mysql 索引类型以及创建

    mysql索引总结(1)-mysql 索引类型以及创建 mysql索引总结(2)-MySQL聚簇索引和非聚簇索引 mysql索引总结(3)-MySQL聚簇索引和非聚簇索引 mysql索引总结(4)-M ...

  7. nginx-access.log的logstash解析

    1.nginx的access的格式: log_format hehe '$http_host $server_addr $remote_addr [$time_local] "$reques ...

  8. 阿里云服务器搭建SVN

    简单步骤介绍 1:安装svn apt-get install subversion 2. 开启svn服务器 svnserve -d 检查是否开启:ps aux | grep svnserve 若出现如 ...

  9. EWS Managed API 2.0 设置获取邮件自动回复功能

    摘要 最近要在邮件提醒功能中添加,自动回复的功能.在移动端获取用户在outlook上是否开启了自动回复功能,如果用户在outlook上开启了自动回复功能, 获取用户自动回复的内容,如果没有开启,用户可 ...

  10. [android] 轮播图-无限循环

    实现无限循环 在getCount()方法中,返回一个很大的值,Integer.MAX_VALUE 在instantiateItem()方法中,获取当前View的索引时,进行取于操作,传递进来的int ...