如图:

自定义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. NOIP 2008 笨小猴

    洛谷 P1125 笨小猴 洛谷传送门 JDOJ 1539: [NOIP2008]笨小猴 T1 JDOJ传送门 Description 笨小猴的词汇量很小,所以每次做英语选择题的时候都很头疼.但是他找到 ...

  2. 【电脑】E470C如何关闭触摸板

    经查  以这种方式关闭最为简单. 若E470C没有这个模块,就装一个! http://www.edowning.net/soft/145089.htm#downbtn2

  3. [codevs3044]矩形面积求并

    题目描述 Description 输入n个矩形,求他们总共占地面积(也就是求一下面积的并) 输入描述 Input Description 可能有多组数据,读到n=0为止(不超过15组) 每组数据第一行 ...

  4. 绕过dva dispatch在更新model数据的异步

    我的业务是在更新选择列表后,马上进行总价格更新,那么由于model的更新不是实时的,因此我在this.props得到的值还是旧值,解决办法就是通过dispatch成功返回的值,传给计算函数 handl ...

  5. Pandas | 13 索引和选择数据

    Pandas现在支持三种类型的多轴索引; 编号 索引 描述 1 .loc() 基于标签 2 .iloc() 基于整数 3 .ix() 基于标签和整数 .loc() Pandas提供了各种方法来完成基于 ...

  6. async和await对promise异步方案的改进,以及使用注意事项

    async.await相比原生promise的有优势: 1.更加简洁,await一个promise即可,那么会自动返回这个promise的resolve值,无需在then函数的回调中手动取值,彻底解决 ...

  7. html规范思维导图(仅限于自己)

  8. 三天精通Vue教程

    在这里更新作为后端工程师想要快速掌握Vue需要看的重点内容,三天精通教程,加油! 学前摘要 ES6的常用语法 Vue的常用语法

  9. Set和Multiset 怎么用咧↓↓↓

    转自:[C++ STL]Set和Multiset - Memset - 博客园https://www.cnblogs.com/ChinaHook/p/6985444.html (对字体进行了略微的修改 ...

  10. Spring Security教程之退出登录logout(十)

    要实现退出登录的功能我们需要在http元素下定义logout元素,这样Spring Security将自动为我们添加用于处理退出登录的过滤器LogoutFilter到FilterChain.当我们指定 ...