android 简单环形比例图
短信预约 -IT技能 免费直播动态提醒
好久不写博客了 最近项目中用到一个环形比例图,分享一下
先上效果图
List list = new ArrayList();
list.add(new String[]{"0.2", "#9B9B9B"});//gray
list.add(new String[]{"0.3", "#29C7BA"});//theme
list.add(new String[]{"0.4", "#518ef8"});//blue
list.add(new String[]{"0.05", "#ff9500"});//yellow
list.add(new String[]{"0.05", "#F67D88"});//tip
ervScale.setAngleAndColorList(list);
public class EasyRingView extends View {
private Context mContext;
private Paint mPaint;
private int mDefaultColor;
private float mRingWidth;
private List mAngleAndColorList;
public EasyRingView(Context context) {
this(context, null);
}
public EasyRingView(Context context, @Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public EasyRingView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
mContext = context;
init();
}
private void init() {
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mRingWidth = Utils.dp2px(mContext, 20);
mPaint.setStrokeWidth(mRingWidth);
mPaint.setStrokeCap(Paint.Cap.ROUND);
mDefaultColor = ContextCompat.getColor(mContext, R.color.gray);
}
public void setRingWidth(float width) {
mRingWidth = width;
if (mPaint != null) {
mPaint.setStrokeWidth(width);
invalidate();
}
}
public void setAngleAndColorList(@NonNull List scaleAndColorList) {
if (mAngleAndColorList == null) {
mAngleAndColorList = new ArrayList();
} else {
mAngleAndColorList.clear();
}
if (scaleAndColorList.size() == 0) {
mAngleAndColorList.add(new AngleAndColorBean(360, mDefaultColor));
} else {
float angleSum = 0;
for (String[] scaleAndColor : scaleAndColorList) {
float angle;
try {
angle = Float.parseFloat(scaleAndColor[0]) * 360;
} catch (NumberFormatException e) {
angle = 0;
}
int color;
try {
color = Color.parseColor(scaleAndColor[1]);
} catch (Exception e) {
color = mDefaultColor;
}
if (angle > 0) {
mAngleAndColorList.add(new AngleAndColorBean(angle, color));
angleSum += angle;
}
}
if (angleSum 0) {
//avoid sum not equal 360
Collections.sort(mAngleAndColorList, (o1, o2) -> {
if (o1.getAngle() > o2.getAngle()) {
return 1;
} else if (o1.getAngle() 1) {
float startAngle = -90;
for (int i = mAngleAndColorList.size() - 1; i >= 0; i--) {
AngleAndColorBean angleAndColorBean = mAngleAndColorList.get(i);
mPaint.setColor(angleAndColorBean.getColor());
canvas.drawArc(left, top, right, bottom, startAngle - angleAndColorBean.getAngle(), angleAndColorBean.getAngle(), false, mPaint);
startAngle -= angleAndColorBean.getAngle();
}
//avoid the last scale has two corner
AngleAndColorBean lastBean = mAngleAndColorList.get(mAngleAndColorList.size() - 1);
mPaint.setColor(lastBean.getColor());
canvas.drawArc(left, top, right, bottom, -90 - lastBean.getAngle() / 2, lastBean.getAngle() / 2, false, mPaint);
} else {
mPaint.setColor(mAngleAndColorList.get(0).getColor());
canvas.drawArc(left, top, right, bottom, 0, 360, false, mPaint);
}
}
private class AngleAndColorBean {
//所占角度
private float angle;
//色值
private int color;
AngleAndColorBean(float angle, int color) {
this.angle = angle;
this.color = color;
}
float getAngle() {
return angle;
}
void setAngle(float angle) {
this.angle = angle;
}
public int getColor() {
return color;
}
public void setColor(int color) {
this.color = color;
}
}
}
作者:这个bug不是我写的
免责声明:
① 本站未注明“稿件来源”的信息均来自网络整理。其文字、图片和音视频稿件的所属权归原作者所有。本站收集整理出于非商业性的教育和科研之目的,并不意味着本站赞同其观点或证实其内容的真实性。仅作为临时的测试数据,供内部测试之用。本站并未授权任何人以任何方式主动获取本站任何信息。
② 本站未注明“稿件来源”的临时测试数据将在测试完成后最终做删除处理。有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341