如图:

自定义view

package com.riverlet.ringview;

import android.animation.ObjectAnimator;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Paint;
import android.graphics.RectF;
import android.util.AttributeSet;
import android.util.Log;
import android.view.View;
import android.view.animation.AccelerateInterpolator; public class AnnularChartView extends View { /**
* 默认颜色
*/
private static final int[] DEFAULT_COLOR = new int[]{0xff82B8FF, 0xffFF7F78, 0xffFFAE72, 0xff74D1B1, 0xffC38AFC};
/**
* 圆环半径,以内环算
*/
private int innerRadius;
/**
* 圆环厚度
*/
private int ringWidth;
/**
* 画笔数组
*/
private Paint[] paints;
/**
* 画笔数组对应的颜色
*/
private int[] colors = DEFAULT_COLOR;
/**
* 圆环圆心x坐标
*/
private int centerX;
/**
* 圆环圆心y坐标
*/
private int centerY;
/**
* 圆环范围
*/
private RectF oval;
/**
* 每个数据对应的角度
*/
private int[] angles;
/**
* 数据
*/
private float[] datas;
/**
* 动画用的进度
*/
private float progress;
/**
* 动画
*/
private ObjectAnimator animator; public AnnularChartView(Context context) {
this(context, null);
} public AnnularChartView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
} public AnnularChartView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
//动画
initAnimator();
} @Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
//矩形
initRectF(getMeasuredWidth(), getMeasuredHeight());
} /**
* 计算圆环的范围
*
* @param w
* @param h
*/
private void initRectF(int w, int h) {
if (w == 0 && h == 0) {
return;
}
centerX = (int) ((float) w / 2);
centerY = (int) ((float) h / 2);
innerRadius = (int) ((float) w / 2 / 89 * 64);
ringWidth = (int) ((float) w / 2 / 89 * 25);
oval = new RectF(centerX - innerRadius, centerY - innerRadius, centerX + innerRadius, centerY + innerRadius);
} /**
* 初始化paint
*/
private void initPaints() {
if (datas == null) {
angles = null;
} else {
float total = 0;
for (float data : datas) {
total += data;
}
if (total <= 0) {
angles = null;
} else {
angles = new int[datas.length];
int sumAngles = 0;
for (int i = 0; i < datas.length; i++) {
float angle;
if (i == datas.length - 1) {
angles[i] = 360 - sumAngles;
Log.v("setData", angles[i] + "");
} else {
angle = datas[i] / total * 360;
if (angle < 1) {
angles[i] = 1;
} else {
angles[i] = Math.round(angle);
}
sumAngles += angles[i];
Log.v("setData", angles[i] + "");
}
}
}
}
if (angles != null) {
//用于定义的圆弧的形状和大小的界限
paints = new Paint[angles.length];
for (int i = 0; i < angles.length; i++) {
Paint paint = new Paint();
paint.setColor(colors[i % colors.length]);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(25);
paint.setAntiAlias(true);
paints[i] = paint;
}
}
animStart();
} private void initAnimator() {
progress = 0;
animator = ObjectAnimator.ofFloat(this, "progress", 0f, 1.0f);
animator.setDuration(800);
animator.setInterpolator(new AccelerateInterpolator());
animator.start();
} @Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
if (oval == null) {
initRectF(getWidth(), getHeight());
}
int lastAngle = 0;
int nums = angles == null ? 0 : angles.length;
if (nums > 0) {
for (int i = 0; i < nums; i++) {
if (i > 0) {
lastAngle = (int) (lastAngle + angles[i - 1] * progress);
}
paints[i].setStrokeWidth(ringWidth);
if (angles[i] > 0) {
canvas.drawArc(oval, 270 + lastAngle, (angles[i] + 1) * progress, false, paints[i]);
}
}
} else {
Paint paint = new Paint();
paint.setColor(0xffa0a0a0);
paint.setStyle(Paint.Style.STROKE);
paint.setStrokeWidth(ringWidth);
paint.setAntiAlias(true);
canvas.drawArc(oval, 270, 360 * progress + 1, false, paint);
}
} public float getProgress() {
return progress;
} public void setProgress(float progress) {
this.progress = progress;
invalidate();
} /**
* 开始动画
*/
public void animStart() {
if (animator.isStarted()) {
animator.cancel();
}
animator.start();
} public void setData(float[] datas) {
this.datas = datas;
initPaints();
} public int[] getColors() {
return colors;
} public void setColors(int[] colors) {
this.colors = colors;
} public void setAnimator(ObjectAnimator animator) {
this.animator = animator;
}
}

  

布局

<?xml version="1.0" encoding="utf-8"?>
<android.support.constraint.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:layout_width="match_parent"
android:layout_height="match_parent"
tools:context="com.riverlet.ringview.MainActivity"> <com.riverlet.ringview.AnnularChartView
android:id="@+id/annularChartView"
android:layout_width="200dp"
android:layout_height="200dp"
android:layout_marginTop="30dp"
android:text="Hello World!"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toTopOf="parent" /> <TextView
android:id="@+id/text_1"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="50dp"
android:background="@drawable/bg"
android:padding="5dp"
android:text="100 : 100 : 100 : 100 : 100"
android:textColor="#ffffff"
android:textSize="25sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/annularChartView" /> <TextView
android:id="@+id/text_2"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/bg"
android:padding="5dp"
android:text="100 : 200 : 300 : 400 : 500"
android:textColor="#ffffff"
android:textSize="25sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_1" /> <TextView
android:id="@+id/text_3"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="20dp"
android:background="@drawable/bg"
android:padding="5dp"
android:text="500 : 100 : 300 : 100 : 600"
android:textColor="#ffffff"
android:textSize="25sp"
app:layout_constraintLeft_toLeftOf="parent"
app:layout_constraintRight_toRightOf="parent"
app:layout_constraintTop_toBottomOf="@+id/text_2" />
</android.support.constraint.ConstraintLayout>

  

MainActivity中
package com.riverlet.ringview;

import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.view.View; public class MainActivity extends AppCompatActivity implements View.OnClickListener { AnnularChartView annularChartView; @Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main); annularChartView = findViewById(R.id.annularChartView);
findViewById(R.id.text_1).setOnClickListener(this);
findViewById(R.id.text_2).setOnClickListener(this);
findViewById(R.id.text_3).setOnClickListener(this);
} @Override
public void onClick(View v) {
switch (v.getId()) {
case R.id.text_1:
annularChartView.setData(new float[]{100f, 100f, 100f, 100f, 100f});
break;
case R.id.text_2:
annularChartView.setData(new float[]{100f, 200f, 300f, 400f, 500f});
break;
case R.id.text_3:
annularChartView.setData(new float[]{500f, 100f, 300f, 100f, 600f});
break;
}
}
}

  完成

参考于://https://www.jianshu.com/p/03f6751e4c99

Android中自定义环形图的更多相关文章

  1. Android中自定义环形图2

    如图: 自定义属性,在values文件夹下创建 attrs.xml <?xml version="1.0" encoding="utf-8"?> & ...

  2. [转]Android中自定义checkbox样式

    android中自定义checkbox的图片和大小   其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version=" ...

  3. 转--Android中自定义字体的实现方法

    1.Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace 2.在Android中可以引入其他字体 . 复制代码 代码如下: <?xml versio ...

  4. Android中自定义ActionBar的背景色等样式style

    Android中想要去自定义ActionBar的背景色等样式. [折腾过程] 1.自己找代码,发现对应的配置的地方了: AndroidManifest.xml ? 1 2 <applicatio ...

  5. Android中自定义veiw使用Java中的回调方法

    //------------------MainActivity----中---------------------------------- import android.os.Bundle;imp ...

  6. android开发:Android 中自定义View的应用

    大家好我们今天的教程是在Android 教程中自定义View 的学习,对于初学着来说,他们习惯了Android 传统的页面布局方式,如下代码: <?xml version="1.0&q ...

  7. Android中自定义组合控件

    Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...

  8. Android中自定义ListView实现上拉加载更多和下拉刷新

    ListView是Android中一个功能强大而且很常用的控件,在很多App中都有ListView的下拉刷新数据和上拉加载更多这个功能.这里我就简单记录一下实现过程. 实现这个功能的方法不止一个,Gi ...

  9. Android中自定义View和自定义动画

    Android FrameWork 层给我们提供了很多界面组件,但是在实际的商业开发中这些组件往往并不能完全满足我们的需求,这时候我们就需要自定义我们自己的视图和动画. 我们要重写系统的View就必须 ...

随机推荐

  1. 网页网站基础入门篇: 使用Adobe Dreamweaver CS6 制作网页/网站

    咱开发网页或者网站呢,最好使用个软件,我使用的是Adobe Dreamweaver CS6 (自行下载安装) 打开软件 现在呢咱使用 html5 <!doctype html> <h ...

  2. HTML 超链接返回上一级

    参考:http://blog.csdn.net/huanongjingchao/article/details/39587663 超链实现返回刚刚访问的网页: <a href="#&q ...

  3. Docker入门之安装Docker

    目录 目录 1 1. 前言 1 2. 创建网桥 2 3. 安装Docker 2 3.1. 二进制安装 3 3.1.1. 下载安装 3 3.1.2. 配置服务 3 3.1.3. 启动服务 4 3.2. ...

  4. 下载mqtt.fx

    #下载mqtt.fxhttp://www.jensd.de/apps/mqttfx/1.7.1/

  5. quick如何打开工程或者示例

    quick如何打开工程或者示例 1. 那里打开工程 cc.ui.UIPushButton.new(images, {scale9 = true}) :setButtonSize(buttonWidth ...

  6. SUSE12.2 添加ISO为源

    152 2019-05-16 16:40:13 mkdir /mnt/DVD1 153 2019-05-16 16:40:55 mount -o loop /root/SLE-12-SP2-Serve ...

  7. [算法模版]AC自动机

    [算法模版]AC自动机 基础内容 板子不再赘述,OI-WIKI有详细讲解. \(query\)函数则是遍历文本串的所有位置,在文本串的每个位置都沿着\(fail\)跳到根,将沿途所有元素答案++.意义 ...

  8. Windows安装gmpy2

    我在终端用python2的pip安装gmpy2时显示缺少Visual C++ 9.0 按照其要求,访问他给的网址安装一下 https://pypi.org/project/gmpy2/#files 进 ...

  9. pytest学习笔记二 fixtrue

    前言 官方文档关于fixture功能的解释如下: The purpose of test fixtures is to provide a fixed baseline upon which test ...

  10. 【06月10日】A股ROE最高排名

    个股滚动ROE = 最近4个季度的归母净利润 / ((期初归母净资产 + 期末归母净资产) / 2). 查看更多个股ROE最高排名 兰州民百(SH600738) - ROE_TTM:86.45% - ...