Android 3D 立体饼状图(磁盘已用空间)自定义 View
2021-07-06 本文已影响0人
雁过留声_泪落无痕
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);
}
}
}
![](https://img.haomeiwen.com/i2767475/b3bcad2c788987ec.png)