public class PieChart extends View {
private static final String TAG = "PieChart";
private static final boolean LOG_TEST = false;
public PieChart(Context context) {
this(context, null);
}
public PieChart(Context context,
@Nullable AttributeSet attrs) {
this(context, attrs, 0);
}
public PieChart(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
this(context, attrs, defStyleAttr, 0);
}
public PieChart(Context context, @Nullable AttributeSet attrs, int defStyleAttr,
int defStyleRes) {
super(context, attrs, defStyleAttr, defStyleRes);
initStyle(context, attrs, defStyleAttr);
}
private int pieChartWidth = 144;
private int pieChartRadius = 50;
private float pieChartAngle = 0;
private int pieChartCenterOffset = -6;
private int pieChartColorBackground = Color.GRAY;
private int pieChartColorSelected = Color.BLUE;
private Paint mTestPaint;
private Paint mPieChartPaint = new Paint();
private RectF mRectFBackground;
private RectF mRectFSelected;
private float mMax = 64;
private float mValue = 14;
private void initRectF() {
float center = pieChartWidth / 2f;
float width = pieChartRadius * 2f;
mRectFBackground = new RectF();
mRectFBackground.left = center - pieChartRadius;
mRectFBackground.right = mRectFBackground.left + width;
mRectFBackground.top = center - pieChartRadius;
mRectFBackground.bottom = mRectFBackground.top + width;
center = center + pieChartCenterOffset / 2f;
mRectFSelected = new RectF();
mRectFSelected.left = center - pieChartRadius;
mRectFSelected.right = mRectFSelected.left + width;
mRectFSelected.top = center - pieChartRadius;//+ pieChartCenterOffset / 4f;
mRectFSelected.bottom = mRectFSelected.top + width;
}
private void initPaint() {
mPieChartPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mPieChartPaint.setAntiAlias(true);
mPieChartPaint.setStyle(Paint.Style.FILL);
if (LOG_TEST) {
mTestPaint = new Paint();
mTestPaint.setFlags(Paint.ANTI_ALIAS_FLAG);
mTestPaint.setAntiAlias(true);
mTestPaint.setFilterBitmap(true);
mTestPaint.setStyle(Paint.Style.STROKE);
mTestPaint.setColor(Color.RED);
mTestPaint.setStrokeWidth(2);
mTestPaint.setTextSize(24);
}
}
private void initStyle(Context context, AttributeSet attrs, int defStyleAttr) {
TypedArray typedArray = null;
try {
typedArray = context.obtainStyledAttributes(attrs, R.styleable.PieChart,
defStyleAttr, 0);
pieChartWidth = typedArray.getDimensionPixelSize(
R.styleable.PieChart_pieChartWidth, pieChartWidth);
pieChartRadius = typedArray.getDimensionPixelSize(
R.styleable.PieChart_pieChartRadius, pieChartRadius);
pieChartAngle = typedArray.getFloat(
R.styleable.PieChart_pieChartAngle, pieChartAngle);
pieChartCenterOffset = typedArray.getDimensionPixelSize(
R.styleable.PieChart_pieChartCenterOffset, pieChartCenterOffset);
pieChartColorBackground = typedArray.getColor(
R.styleable.PieChart_pieChartColorBackground, pieChartColorBackground);
pieChartColorSelected = typedArray.getColor(
R.styleable.PieChart_pieChartColorSelected, pieChartColorSelected);
initRectF();
initPaint();
} catch (Exception e) {
LogUtil.loge(TAG, e.toString());
} finally {
if (typedArray != null) {
typedArray.recycle();
}
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
mValue = (mValue % mMax);
float maxTo360 = 360f / mMax;
mValue = mValue * maxTo360;
mMax = 360f;
mPieChartPaint.setColor(pieChartColorBackground);
canvas.drawArc(mRectFBackground, pieChartAngle, mMax - mValue, true, mPieChartPaint);
mPieChartPaint.setColor(pieChartColorSelected);
canvas.drawArc(mRectFSelected, pieChartAngle - mValue, mValue, true, mPieChartPaint);
}
public void setValue(float max, float value) {
mMax = max;
mValue = value;
postInvalidate();
}
}
<declare-styleable name="PieChart">
<attr name="pieChartWidth" format="dimension"/>
<attr name="pieChartRadius" format="dimension"/>
<attr name="pieChartAngle" format="float"/>
<attr name="pieChartCenterOffset" format="dimension"/>
<attr name="pieChartColorBackground" format="reference"/>
<attr name="pieChartColorSelected" format="reference"/>
</declare-styleable>
<color name="fuxi_color_store_background">#34353A</color>
<color name="fuxi_color_store_selected">#033D40</color>