440_画出折线图上的红点





画出折线图上的红点


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;
    }
}



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值