好久不写博客了 最近项目中用到一个环形比例图,分享一下 先上效果图 List list = new ArrayList(); list.ad
好久不写博客了 最近项目中用到一个环形比例图,分享一下
先上效果图
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;
}
}
}
--结束END--
本文标题: android 简单环形比例图
本文链接: https://lsjlt.com/news/29619.html(转载时请注明来源链接)
有问题或投稿请发送至: 邮箱/279061341@qq.com QQ/279061341
2024-01-21
2023-10-28
2023-10-28
2023-10-27
2023-10-27
2023-10-27
2023-10-27
回答
回答
回答
回答
回答
回答
回答
回答
回答
回答
0