Android中自定义环形图
如图:

自定义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中自定义环形图的更多相关文章
- Android中自定义环形图2
如图: 自定义属性,在values文件夹下创建 attrs.xml <?xml version="1.0" encoding="utf-8"?> & ...
- [转]Android中自定义checkbox样式
android中自定义checkbox的图片和大小 其实很简单,分三步: 1.在drawable中创建文件checkbox_selector.xml: <?xml version=" ...
- 转--Android中自定义字体的实现方法
1.Android系统默认支持三种字体,分别为:“sans”, “serif”, “monospace 2.在Android中可以引入其他字体 . 复制代码 代码如下: <?xml versio ...
- Android中自定义ActionBar的背景色等样式style
Android中想要去自定义ActionBar的背景色等样式. [折腾过程] 1.自己找代码,发现对应的配置的地方了: AndroidManifest.xml ? 1 2 <applicatio ...
- Android中自定义veiw使用Java中的回调方法
//------------------MainActivity----中---------------------------------- import android.os.Bundle;imp ...
- android开发:Android 中自定义View的应用
大家好我们今天的教程是在Android 教程中自定义View 的学习,对于初学着来说,他们习惯了Android 传统的页面布局方式,如下代码: <?xml version="1.0&q ...
- Android中自定义组合控件
Android中自定义控件的情况非常多,一般自定义控件可以分为两种:继承控件及组合控件.前者是通过继承View或其子类,重写方法实现自定义的显示及事件处理方式:后者是通过组合已有的控件,来实现结构的简 ...
- Android中自定义ListView实现上拉加载更多和下拉刷新
ListView是Android中一个功能强大而且很常用的控件,在很多App中都有ListView的下拉刷新数据和上拉加载更多这个功能.这里我就简单记录一下实现过程. 实现这个功能的方法不止一个,Gi ...
- Android中自定义View和自定义动画
Android FrameWork 层给我们提供了很多界面组件,但是在实际的商业开发中这些组件往往并不能完全满足我们的需求,这时候我们就需要自定义我们自己的视图和动画. 我们要重写系统的View就必须 ...
随机推荐
- ubuntu gitclone下载的文件放哪里了
在home主文件夹里面.
- 1. vue 的安装
兼容性 Vue 不支持 IE8 及以下版本,因为 Vue 使用了 IE8 无法模拟的 ECMAScript 5 特性.但它支持所有兼容 ECMAScript 5 的浏览器. 安装: 1.直接用 < ...
- DS18B20温度获取
https://detail.tmall.com/item.htm?id=40083203373&spm=a1z09.2.0.0.31cd2e8d1sb06V&_u=e1qf7bf56 ...
- 2-开发共享版APP(搭建指南)-修改包名
https://www.cnblogs.com/yangfengwu/p/11273734.html https://www.cnblogs.com/yangfengwu/p/11273746.htm ...
- BBS项目-01
目录 BBS项目 BBS开发流程: BBS表格创建: BBS项目 BBS开发流程: BBS项目: 开发流程: 需求分析 草拟一些项目的大致技术点和流程 架构设计 架构师(框架 语言 数据库 缓存数据库 ...
- Ajax 与 Django
目录 Django与AJAX orm优化查询: MTV 与 MVC模型 choices 参数 update 与 save的区别 AJAX导入: Jquery 实现AJAX ajax基本语法结构 原生J ...
- 转载:tensorflow保存训练后的模型
训练完一个模型后,为了以后重复使用,通常我们需要对模型的结果进行保存.如果用Tensorflow去实现神经网络,所要保存的就是神经网络中的各项权重值.建议可以使用Saver类保存和加载模型的结果. 1 ...
- Spring注解和标签的比较说明
待完善.... xml标签 注解 说明 xml的Spring约束头 @Configuration xml约束头表明这是用于spring的的配置文件 @Configuration注解表情这是用于Spri ...
- R语言排序 -- sort() order() rank()
order() 的返回值是对应“排名”元素所在向量中的位置.注意返回的不是元素本身,而是元素的位置. sort() 是直接对向量中的元素进行排序,返回的是排序后的元素组成的向量. rank() 是求秩 ...
- ddns+ros(routeros)+centos7.6+nginx+php+dnspod
参考文章: http://www.myxzy.com/post-464.html https://www.cnblogs.com/crazytata/p/9686490.html php的源码下载: ...