画出折线图上的红点
public class LineChartView extends View {
private Context context;
private int screenWidth;
private Paint redPaint;
private double singleInterval;
private double halfInterval;
private int height;
private int singleHeight;
private double totalWidth;
private Paint redStrokePaint;
private int dp65;
private int dp45;
private List<BriskAccountRecordEntity> list;
private double maxValue;
private double singleValue;
@Override
protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
super.onMeasure(widthMeasureSpec, heightMeasureSpec);
setMeasuredDimension((int) totalWidth, height);
}
public LineChartView(Context context) {
this(context, null);
}
public LineChartView(Context context, AttributeSet attrs) {
this(context, attrs, 0);
}
public LineChartView(Context context, AttributeSet attrs, int defStyleAttr) {
super(context, attrs, defStyleAttr);
this.context = context;
screenWidth = DensityUtil.getScreenWidth(context);
singleInterval = screenWidth / 7;
halfInterval = singleInterval / 2;
totalWidth = singleInterval * 30;
height = DensityUtil.dip2px(context, 200);
singleHeight = height / 7;
redPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
redPaint.setColor(getResources().getColor(R.color.color_single_E71C18));
redStrokePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
redStrokePaint.setStyle(Paint.Style.STROKE);
redStrokePaint.setStrokeWidth(1);
redStrokePaint.setColor(getResources().getColor(R.color.color_single_E71C18));
dp65 = DensityUtil.dip2px(context, 6.5f);
dp45 = DensityUtil.dip2px(context, 4.5f);
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
for (int i = 0; i < list.size(); i++) {
BriskAccountRecordEntity entity = list.get(i);
double profit = entity.getProfit();
double percent = profit / maxValue;
float y = (float) (1 - percent) * 5 * singleHeight + singleHeight;
canvas.drawCircle((float) ((i * singleInterval) + halfInterval), y, dp45, redPaint);
canvas.drawCircle((float) ((i * singleInterval) + halfInterval), y, dp65, redStrokePaint);
}
}
public void setLineChartList(List<BriskAccountRecordEntity> list) {
this.list = list;
if (list == null) {
return;
}
if (list.size() <= 7) {
totalWidth = singleInterval * 7;
} else {
totalWidth = singleInterval * list.size();
}
}
public void setMaxValue(double maxValue) {
this.maxValue = maxValue;
singleValue = maxValue / 5;
}
}