/**

* 饼状图百分比控件

*/

public class PercentCircle extends View {

private final String tag = "PercentCircle";

/**

* 绘制百分比的圆,一共有两部分,分别是内圆、外圆;

* 思路:首先需要两支画笔, 设置画笔对应的属性等;

*/

/**

* 内部圆画笔

*/

private Paint mBackgroundInPaint;

/**

* 内部圆背景颜色

*/

private int mBackgroundInColor;

/**

* 圆心的坐标

*/

private int mCircleX;

private int mCircleY;

/**

* 当前角度

*/

private float mCurrentAngle;

/**

* 圆的外接矩形

*/

private RectF mArcRectF;

/**

* 开始绘制的角度

*/

private float mStartSweepValue;

/**

* 要绘制的角度

*/

private float mTargetPercent;

/**

* 外边圆的画笔

*/

private Paint mRingPaint;

/**

* 外层圆半径

*/

private int mRadius;

/**

* 外层圆颜色

*/

private int mRingColor;

private float newPercent;

public PercentCircle(Context context) {

super(context);

init(context);

}

public PercentCircle(Context context, AttributeSet attrs) {

super(context, attrs);

LogUtils.v(tag, "percentCircle:" + "PercentCircle(Context context, AttributeSet attrs)");

mRadius = 360;

// 背景圆的颜色

mBackgroundInColor = Color.WHITE;

// 外圆环的颜色

mRingColor = Color.parseColor("#a1e0fd");

init(context);

}

public PercentCircle(Context context, AttributeSet attrs, int defStyleAttr) {

super(context, attrs, defStyleAttr);

LogUtils.v(tag, "percentCircle:" + "PercentCircle(Context context, AttributeSet attrs, int defStyleAttr)");

init(context);

}

private void init(Context context) {

//圆环开始角度 -90° 正北方向

mStartSweepValue = -90;

//当前角度

mCurrentAngle = 0;

//设置中心园的画笔

mBackgroundInPaint = new Paint();

mBackgroundInPaint.setAntiAlias(true);

mBackgroundInPaint.setColor(mBackgroundInColor);

mBackgroundInPaint.setStyle(Paint.Style.FILL);

//设置外圆环的画笔

mRingPaint = new Paint();

mRingPaint.setAntiAlias(true);

mRingPaint.setColor(mRingColor);

mRingPaint.setStyle(Paint.Style.FILL);

}

// 主要是测量wrap_content时候的宽和高,因为宽高一样,只需要测量一次宽即可,高等于宽

@Override

protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {

setMeasuredDimension(measure(widthMeasureSpec), measure(widthMeasureSpec));

}

// 当wrap_content的时候,view的大小根据半径大小改变,但最大不会超过屏幕

private int measure(int measureSpec) {

int result = 0;

//1、先获取测量模式 和 测量大小

//2、如果测量模式是MatchParent 或者精确值,则宽为测量的宽

//3、如果测量模式是WrapContent ,则宽为 直径值 与 测量宽中的较小值;否则当直径大于测量宽时,会绘制到屏幕之外;

int specMode = MeasureSpec.getMode(measureSpec);

int specSize = MeasureSpec.getSize(measureSpec);

if (specMode == MeasureSpec.EXACTLY) {

result = specSize;

} else {

result = (mRadius * 2);

if (specMode == MeasureSpec.AT_MOST) {

result = Math.min(result, specSize);

}

}

return result;

}

@Override

protected void onLayout(boolean changed, int left, int top, int right, int bottom) {

super.onLayout(changed, left, top, right, bottom);

//1、如果半径大于圆心的横坐标,需要手动缩小半径的值,否则画到屏幕之外;

//4、画背景圆的外接矩形,用来画圆环;

mCircleX = getMeasuredWidth() / 2;

mCircleY = getMeasuredHeight() / 2;

if (mRadius > mCircleX) {

mRadius = mCircleX;

}

mArcRectF = new RectF(mCircleX - mRadius, mCircleY - mRadius, mCircleX + mRadius, mCircleY + mRadius);

}

@Override

protected void onDraw(Canvas canvas) {

super.onDraw(canvas);

//1、先画外边大的圆

//1、再画中间的小圆覆盖大圆的中间部分

mRingColor = Color.parseColor("#a1e0fd");

mRingPaint.setColor(mRingColor);

canvas.drawArc(mArcRectF, mStartSweepValue, (float) (3.6 * mTargetPercent), true, mRingPaint);

mRingColor = Color.parseColor("#f99740");

mRingPaint.setColor(mRingColor);

canvas.drawArc(mArcRectF, (float) (mStartSweepValue + 3.6 * mTargetPercent), (float) (3.6 * newPercent), true, mRingPaint);

canvas.drawCircle(mCircleX, mCircleY, mRadius * 0.672f, mBackgroundInPaint);

}

public void setTargetPercent(float targetPercent) {

mTargetPercent = targetPercent;

}

public void setNewPercent(float newPercent) {

this.newPercent = newPercent;

}

}

android绘制简单饼状图,Android 绘制饼状图相关推荐

  1. 如何用php饼型图,php绘制饼状图的代码举例

    //变量定义,画椭圆弧时的角度大小 define("ANGLELENGTH",3); /** * 绘制图片 * @param $title 3D图的标题 * @param $dat ...

  2. python绘制饼状图图例_python matplotlib饼状图参数及用法解析

    这篇文章主要介绍了python matplotlib饼状图参数及用法解析,文中通过示例代码介绍的非常详细,对大家的学习或者工作具有一定的参考学习价值,需要的朋友可以参考下 在python的matplo ...

  3. php jq 饼状图,jQuery插件FusionCharts绘制饼状图

    这次给大家带来jQuery插件FusionCharts绘制饼状图,jQuery插件FusionCharts绘制饼状图的注意事项有哪些,下面就是实战案例,一起来看一下. 1.了解帕累托图的特性以及和其他 ...

  4. python 数据可视化———绘制饼状图(bar)

    python 数据可视化---绘制饼状图(bar) 从入门到入门,快速上手饼状图 前言 Pyplot 是 Matplotlib 的子库,提供了和 MATLAB 类似的绘图 API. Pyplot 包含 ...

  5. 第166天:canvas绘制饼状图动画

    canvas绘制饼状图动画 1.HTML 1 <!DOCTYPE html> 2 <html lang="en"> 3 <head> 4 < ...

  6. 【python图像处理】python绘制饼状图

    饼状图在统计分析中有着重要的应用,python中用于绘制饼状图是matplotlib中的pyplot类,这里就介绍一下,如何绘制饼状图. 直接看下面的代码 """ === ...

  7. python使用matplotlib绘制饼状图

    python使用matplotlib绘制饼状图 Python绘图需要下载安装matplotlib模块,它是一个数学绘图库,我们将使用它来制作简单的图表. 绘制饼状图 代码一: import matpl ...

  8. 年末盘点时间——用Python绘制饼状图对商品库存进行分析

    人生苦短,我用python 存货盘点最重要的是什么,盘点比例要达到, 比如说要达到80%,于是就拿着企业给导的进销存明细表, 于是就开始筛选大金额的存货作为选择的样本, 这样就够比例了. 可是实际盘点 ...

  9. Python绘制饼状图/甜甜圈

    Python Matplotlib.pyplot Matplotlib库是一个面向对象的绘图库.绘图界面由pyplot模块提供.该模块提供了许多绘图函数,以下记录的是饼状图/甜甜圈图的相关参数和绘图过 ...

  10. uni-app 之canvas绘制饼状图

    uni-app 之canvas绘制饼状图 一开始,对于canvas我是拒绝的,后来,我发现我爱上了它,像爱上小哥哥一样~~ 说起canvas,是css3新增的标签.而饼状图又是canvas经典,我们公 ...

最新文章

  1. 机器学习入门必读:6种简单实用算法及学习曲线、思维导图
  2. javascript函数作用域与闭包
  3. 参加web前端学习前需要知道的注意事项
  4. 在SAP Hybris commerce Storefront里购物下单
  5. 大屏可视化分配率是什么意思_什么是分配率?
  6. mysql排序规则错误_MySQL中“非法混合排序规则”错误的疑难解答
  7. 奇店社群社区团购小程序v5.5.9
  8. 简单的jQuery获取URL的?后带的参数
  9. less 使用小结!笔记!
  10. tomcat ---- 启动,关闭和配置等等
  11. jQuery正则表达式实现表单验证功能(注册)
  12. 华为wifi的虚拟服务器,华为路由器虚拟服务器设置方法
  13. uniapp简单的登录页面布局
  14. LaTeX技巧353:图片标题的字体大小怎么设定呢?
  15. 如何处理电脑长时间未操作出现的假死?
  16. 基于机智云的智能家用窗户窗帘控制及物联网系统
  17. 小米手机安装linux视频教程,技术|在手机上轻松安装 Ubuntu Touch OS
  18. C#中WinFrom保存文件SaveFileDialog类的使用方法
  19. html动画变圆,HTML5 canvas制作圆形的万花筒动画效果
  20. 机器人领域主要国际会议与期刊列表

热门文章

  1. deficit记忆_英语单词分类记忆 高效快速的记忆法
  2. 办公文件实时自动同步工具-FileYee,好用!
  3. 常用模板 UPD12/4
  4. 微信小程序实现登录功能
  5. php打开文件fopen函数
  6. 深度解析输入偏置电流和输入失调电流的定义来源以及对电路的影响
  7. 软件测试的正反两面性思维,软件测试中破坏性测试思维的思考
  8. Android 深色模式适配总结
  9. html邮件模板美化,设计利器:定制你的炫酷邮件模板
  10. UVALive(LA) 4487 Exclusive-OR(带权并查集)