package com.xxx.widget;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.Path;
import android.util.AttributeSet;
import android.view.View;
import androidx.annotation.ColorInt;
import androidx.annotation.Nullable;
public class DiskView extends View {
private static final int DEFAULT_THICK = 10;
private static final int DEFAULT_INNER_COLOR = Color.parseColor("#A0AAAA00");
private static final int DEFAULT_BORDER_COLOR = Color.parseColor("#333333");
private Paint mPaint;
private Path mPath;
private float mDiskUsedPercent = 0.0f;
private int mThick = DEFAULT_THICK;
private int mInnerColor = DEFAULT_INNER_COLOR;
private int mBorderColor = DEFAULT_BORDER_COLOR;
public DiskView(Context context) {
super(context);
init();
}
public DiskView(Context context, @Nullable AttributeSet attrs) {
super(context, attrs);
init();
}
public DiskView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
init();
}
public void setThick(int thick) {
if (thick > 0) {
mThick = thick;
invalidate();
}
}
public void setInnerColor(@ColorInt int color) {
mInnerColor = color;
invalidate();
}
public void setBorderColor(@ColorInt int color) {
mBorderColor = color;
invalidate();
}
public void setDiskUsedPercent(float percent) {
if (percent >= 0f && percent <= 1f) {
mDiskUsedPercent = percent;
invalidate();
}
}
private void init() {
mPath = new Path();
mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setStrokeWidth(1f);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
float paintWidth = mPaint.getStrokeWidth();
float halfPaintWidth = paintWidth / 2;
float width = getMeasuredWidth() - paintWidth;
float height = getMeasuredHeight() - paintWidth;
// 绘制上圆和下圆轮廓
mPaint.setStyle(Paint.Style.STROKE);
mPaint.setColor(mBorderColor);
canvas.drawOval(halfPaintWidth, halfPaintWidth, width, height - mThick, mPaint);
mPath.reset();
mPath.moveTo(width, (height - mThick) / 2f);
mPath.lineTo(width, (height + mThick) / 2f);
mPath.arcTo(halfPaintWidth, mThick, width, height, 0, 180, false);
mPath.lineTo(halfPaintWidth, (height - mThick) / 2f);
canvas.drawPath(mPath, mPaint);
// 绘制上圆填充
mPaint.setColor(mInnerColor);
mPaint.setStyle(Paint.Style.FILL_AND_STROKE);
canvas.drawArc(halfPaintWidth, halfPaintWidth, width, height - mThick, -90, mDiskUsedPercent * 360, true, mPaint);
// 绘制上圆与下圆之间的柱状填充
float swipeAngle = 0f;
if (mDiskUsedPercent > 0.75f) {
swipeAngle = 180;
} else if (mDiskUsedPercent > 0.25f) {
swipeAngle = mDiskUsedPercent * 360 - 90;
}
if (swipeAngle > 0f) {
mPath.reset();
mPath.moveTo(width, (height - mThick) / 2f);
mPath.lineTo(width, (height + mThick) / 2f);
// forceMoveTo 设置为 true 会重置起点
mPath.arcTo(halfPaintWidth, mThick, width, height, 0, swipeAngle, false);
// sweepAngle 为负表示逆时针方向
mPath.arcTo(halfPaintWidth, halfPaintWidth, width, height - mThick, swipeAngle, -swipeAngle, false);
canvas.drawPath(mPath, mPaint);
}
}
}
Android 3D 立体饼状图(磁盘已用空间)自定义 View
最后编辑于 :
©著作权归作者所有,转载或内容合作请联系作者
- 文/潘晓璐 我一进店门,熙熙楼的掌柜王于贵愁眉苦脸地迎上来,“玉大人,你说我怎么就摊上这事。” “怎么了?”我有些...
- 文/花漫 我一把揭开白布。 她就那样静静地躺着,像睡着了一般。 火红的嫁衣衬着肌肤如雪。 梳的纹丝不乱的头发上,一...
- 文/苍兰香墨 我猛地睁开眼,长吁一口气:“原来是场噩梦啊……” “哼!你这毒妇竟也来了?” 一声冷哼从身侧响起,我...
推荐阅读更多精彩内容
- 前言 最近项目中遇到一个需求,需要一个饼状图,显示百分比,点击每一个扇形区域可以切换下面列表的数据。拿到这个需求后...
- 贴吧有人求助下图实现思路,看着描述效果不错,决定实现 效果图如下 实现思路分析 ` 关键代码及算法 绘制圆弧偏移和...
- 仿支付宝统计饼状图的自定义view,如下图: 项目地址:https://github.com/bigeyechou...
- 版权声明:本文为博主原创文章,转载请注明出处! 今天跟大家分享的是用Flutter来实现的自定义饼状图,下面来看看...