安卓绘制多条曲线的颜色问题

在绘制曲线的时候,根据需求要绘制三种不一样颜色的曲线,但是根据我写的代码,曲线是绘制出来了,但是后面的曲线颜色却覆盖了之前的颜色,就是我在绘制第二条线的时候,第一条线颜色也变成了第二种,绘制第三条曲线的时候,前面两条曲线颜色都变成了第三条线的颜色,请问如何解决。

img

你是在哪更改 invaleIndex 这个属性的?


package com.sfgd.ubreath.view;


import android.content.Context;
import android.content.res.Resources;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.LinearGradient;
import android.graphics.Paint;
import android.graphics.Path;
import android.graphics.PointF;
import android.graphics.Rect;
import android.graphics.Shader;
import android.util.AttributeSet;
import android.view.View;

import com.sfgd.ubreath.utils.Logs;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;


/**
 * CO2自动义控件
 * 功能:传入数据,可将数据以曲线图展示,并且不同的数值颜色有区分
 */
public class StudyBendLine extends View {

    private float sumHeight;//总控件的高度
    private float sumWidth;//总空间的宽度
    private float maxTime;//最大的时间 用来划分单位的 最小就是20 X1.2是为了给上方和下方预留空间
    private Paint linePaint;//线的画笔
    private Paint valuePaint;//线的画笔
    private Paint shadowPaint;//阴影的画笔
    private Paint mPaint;//曲线画笔
    private Paint circlePaint;//圆点画笔
    private Paint circlePaint2;//圆点画笔
    private Paint scorePaint;
    private Paint textPaint;//文字的画笔
    private ArrayList<Integer> timeList;//具体的值
    private ArrayList<String> dataList;//底部的时间
    private float oneHeight; //每一个小段所要分成的高
    private float oneWidth;//每一个小段所要分成的宽
    private float buttomHeiht; //给底部一排日期预留出的时间
    private Path baseLinePath;//折线路径
    private float smoothness = 0.36f; //折线的弯曲率
    private Paint baseShadow;//折线下的阴影的画笔
    private ArrayList<PointF> xyList = new ArrayList<>();


    //表示当前绘制的曲线是第几条  一共可以绘制三条
    private int invaleIndex = 0;

    //储存定好的坐标点的集合
    private List<PointF> newPoints;

    public StudyBendLine(Context context) {
        super(context);
        initPaint(context);
    }

    public StudyBendLine(Context context, AttributeSet attrs) {
        super(context, attrs);
        initPaint(context);
    }

    /**
     * 初始化画笔
     *
     * @linpaint 线条画笔
     * @shadowPaint 阴影画笔
     */
    private void initPaint(Context context) {


        //画线的画笔
        linePaint = new Paint();
        valuePaint = new Paint();
        linePaint.setColor(Color.parseColor("#FF3DCC76"));
        linePaint.setAntiAlias(true);
        linePaint.setTextSize(dp2px(getContext(), 16));
        linePaint.setStrokeWidth(dp2px(getContext(), 1));
        valuePaint.setColor(Color.parseColor("#FF3DCC76"));
        valuePaint.setAntiAlias(true);
        valuePaint.setTextSize(dp2px(getContext(), 16));
        valuePaint.setStrokeWidth(dp2px(getContext(), 1));
        //画背景的画笔
        shadowPaint = new Paint();
        shadowPaint.setColor(Color.parseColor("#CBF2ED"));
        shadowPaint.setAntiAlias(true);
        //画最下方文字的画笔
        textPaint = new Paint();
        textPaint.setColor(Color.parseColor("#999999"));
        textPaint.setAntiAlias(true);
        textPaint.setTextSize(dp2px(getContext(), 14));

        circlePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint.setColor(Color.parseColor("#8BE4D4"));
        circlePaint.setStrokeWidth(dp2px(getContext(), 2));
        circlePaint.setStyle(Paint.Style.STROKE);

        circlePaint2 = new Paint(Paint.ANTI_ALIAS_FLAG);
        circlePaint2.setColor(Color.WHITE);
        circlePaint2.setStyle(Paint.Style.FILL);

        baseShadow = new Paint();
        baseShadow.setAntiAlias(true);
        baseShadow.setColor((Color.WHITE & 0x40FFFFFF) | 0x10000000);
        baseShadow.setStyle(Paint.Style.FILL);

        buttomHeiht = dp2px(2);//线距离底部高度

        mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        mPaint.setColor(Color.GREEN);
        mPaint.setStrokeWidth(dp2px(getContext(), 1));
        mPaint.setStyle(Paint.Style.STROKE);
        mPaint.setAntiAlias(true);
        mPaint.setStrokeCap(Paint.Cap.ROUND);


        scorePaint = new Paint(Paint.ANTI_ALIAS_FLAG);
        scorePaint.setStyle(Paint.Style.STROKE);
        scorePaint.setStrokeCap(Paint.Cap.ROUND);
        scorePaint.setColor(Color.parseColor("#DDDDDD"));
        scorePaint.setStrokeWidth(dp2px(0.5f));
        baseLinePath = new Path();
    }


    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        sumHeight = getMeasuredHeight();
        sumWidth = getMeasuredWidth();

    }

    private void measure() {
//        maxTime = getMaxTime(timeList);//最大分数为120
        String text = "P";
        Rect rect = new Rect();
        textPaint.getTextBounds(text, 0, text.length(), rect);
        oneHeight = ((sumHeight - buttomHeiht - 2 * rect.height()) / 40);
        oneWidth = sumWidth / (300 * 4);
    }

    /**
     * activity进行hangdle更新,每隔一秒调取该方法
     */

    public void updateTime(float value) {
        xIndex++;
        toGetXy(value);//获取x和y的坐标
        invalidate();
    }


    private int xIndex = 0;
    private Canvas myCanvas;

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        myCanvas = canvas;
        measure();
        drawBottomLine(myCanvas);
        drawLine(myCanvas);
        toDrawLine(myCanvas);

    }

    private void toGetXy(float value) {

        if (xIndex == 1) {
            xyList.add(new PointF(0, sumHeight));
            float x = oneWidth + xIndex * 4 * oneWidth;
            float y = (sumHeight - (oneHeight * value));
            xyList.add(new PointF(x + oneHeight, y - buttomHeiht));
        } else {
            float x = oneWidth + xIndex * 4 * oneWidth;
            float y = (sumHeight - (oneHeight * value));
            xyList.add(new PointF(x + oneHeight, y - buttomHeiht));
        }

    }


    private float lX = 0;
    private float lY = 0;

    /**
     * 画线
     */
    private void toDrawLine(Canvas canvas) {
        if (xyList == null || xyList.size() == 0 || xyList.size() < 3) {
            return;
        }
        newPoints = new ArrayList<>();
        newPoints.addAll(xyList);


        baseLinePath.moveTo(newPoints.get(newPoints.size() - 3).x, newPoints.get(newPoints.size() - 3).y);

        PointF p = newPoints.get(newPoints.size() - 2);
        PointF firstPointF = newPoints.get(newPoints.size() - 3);
        float x1 = firstPointF.x + lX;
        float y1 = firstPointF.y + lY;

        PointF secondPointF = newPoints.get(newPoints.size() - 1);
        lX = (secondPointF.x - firstPointF.x) / 2 * smoothness;
        lY = (secondPointF.y - firstPointF.y) / 2 * smoothness;
        float x2 = p.x - lX;
        float y2 = p.y - lY;
        if (y1 == p.y) {
            y2 = y1;
        }
        baseLinePath.cubicTo(x1, y1, x2, y2, p.x, p.y);

        if (0 == invaleIndex) {
            mPaint.setColor(Color.parseColor("#FF4F7FFF"));
        } else if (1 == invaleIndex) {
            mPaint.setColor(Color.parseColor("#FFFFA640"));
        } else if (2 == invaleIndex) {
            mPaint.setColor(Color.parseColor("#FF3DCCC2"));
        }


        canvas.drawPath(baseLinePath, mPaint);

    }


    /**
     * @param isFinish true表示绘制三条完成
     */
    public void endInvale(boolean isFinish) {
        if (isFinish) {

        } else {
            if (invaleIndex == 2) {

            } else {
                invaleIndex++;
                xIndex = 0;
                xyList.clear();

//                mPaint = new Paint(Paint.ANTI_ALIAS_FLAG);
//                mPaint.setColor(Color.GREEN);
//                mPaint.setStrokeWidth(dp2px(getContext(), 1));
//                mPaint.setStyle(Paint.Style.STROKE);
//                mPaint.setAntiAlias(true);
//                mPaint.setStrokeCap(Paint.Cap.ROUND);



            }

        }
    }

    private int clickPosition = -1;

    public void showTimeAndValue(int index) {
        clickPosition = index;
        invalidate();
    }

    /**
     * 分割线
     *
     * @param canvas
     */
    private void drawLine(Canvas canvas) {

        float mMax1 = 40;
        float mMax2 = mMax1 / 4 * 3;
        float mMax3 = mMax1 / 4 * 2;
        float mMax4 = mMax1 / 4 * 1;

        String text = "0";
        Rect rect = new Rect();
        scorePaint.getTextBounds(text, 0, text.length(), rect);

        text = mMax1 + "";
        float y1 = (sumHeight - (oneHeight * mMax1));
//        canvas.drawText(text, 0, y1 - buttomHeiht - rect.height() / 2 - dp2px(4), textPaint);
        canvas.drawLine(0, 0 + buttomHeiht, sumWidth, 0 + buttomHeiht, scorePaint);

        text = mMax3 + "";
        float y2 = (sumHeight - (oneHeight * mMax2));
//        canvas.drawText(text, 0, y2 - buttomHeiht - rect.height() / 2 - dp2px(4), textPaint);
        canvas.drawLine(0, (sumHeight - buttomHeiht * 2) / 4 * 3 + buttomHeiht, sumWidth, (sumHeight - buttomHeiht * 2) / 4 * 3 + buttomHeiht, scorePaint);

        text = mMax4 + "";
        float y3 = (sumHeight - (oneHeight * mMax3));
//        canvas.drawText(text, 0, y3 - buttomHeiht - rect.height() / 2 - dp2px(4), textPaint);
        canvas.drawLine(0, (sumHeight - buttomHeiht * 2) / 4 * 2 + buttomHeiht, sumWidth, (sumHeight - buttomHeiht * 2) / 4 * 2 + buttomHeiht, scorePaint);

        text = mMax1 + "";
        float y4 = (sumHeight - (oneHeight * mMax4));
//        canvas.drawText(text, 0, y4 - buttomHeiht - rect.height() / 2 - dp2px(4), textPaint);
        canvas.drawLine(0, (sumHeight - buttomHeiht * 2) / 4 * 1 + buttomHeiht, sumWidth, (sumHeight - buttomHeiht * 2) / 4 * 1 + buttomHeiht, scorePaint);

    }

    /**
     * 底部标线
     *
     * @param canvas
     */
    private void drawBottomLine(Canvas canvas) {
        Rect rect = new Rect();
//        scorePaint.setColor(Color.GREEN);
        scorePaint.getTextBounds("0", 0, "0".length(), rect);
        scorePaint.setStrokeWidth(4);
        canvas.drawLine(0, 0, 0, sumHeight - dp2px(0) - rect.height() / 2, scorePaint);//画底部灰线

        canvas.drawLine(0, sumHeight - dp2px(0) - rect.height() / 2, sumWidth, sumHeight - dp2px(0) - rect.height() / 2, scorePaint);//画底部灰线
//        canvas.drawLine(0, sumHeight - dp2px(15) - rect.height() / 2, sumWidth, sumHeight - dp2px(15) - rect.height() / 2, scorePaint);//画底部灰线

        canvas.drawLine(sumWidth / 6 * 1, sumHeight - dp2px(0) - rect.height() / 2, sumWidth / 6 * 1, sumHeight - dp2px(0) - rect.height() - dp2px(3), scorePaint);//画底部灰线
        canvas.drawLine(sumWidth / 6 * 2, sumHeight - dp2px(0) - rect.height() / 2, sumWidth / 6 * 2, sumHeight - dp2px(0) - rect.height() - dp2px(3), scorePaint);//画底部灰线
        canvas.drawLine(sumWidth / 6 * 3, sumHeight - dp2px(0) - rect.height() / 2, sumWidth / 6 * 3, sumHeight - dp2px(0) - rect.height() - dp2px(3), scorePaint);//画底部灰线
        canvas.drawLine(sumWidth / 6 * 4, sumHeight - dp2px(0) - rect.height() / 2, sumWidth / 6 * 4, sumHeight - dp2px(0) - rect.height() - dp2px(3), scorePaint);//画底部灰线
        canvas.drawLine(sumWidth / 6 * 5, sumHeight - dp2px(0) - rect.height() / 2, sumWidth / 6 * 5, sumHeight - dp2px(0) - rect.height() - dp2px(3), scorePaint);//画底部灰线
        canvas.drawLine(sumWidth / 6 * 6, sumHeight - dp2px(0) - rect.height() / 2, sumWidth / 6 * 6, sumHeight - dp2px(0) - rect.height() - dp2px(3), scorePaint);//画底部灰线

    }

    /**
     * 阴影层叠
     *
     * @param canvas
     * @param Points
     */

    private void drawArea(Canvas canvas, List<PointF> Points) {
        LinearGradient mShader = new LinearGradient(0, 0, 0, getMeasuredHeight(), new int[]{Color.parseColor("#BAEFE6"), Color.parseColor("#D7F5F0"), Color.parseColor("#F9FEFD")}, new float[]{0.5f, 0.65f, 0.85f}, Shader.TileMode.REPEAT);
        baseShadow.setShader(mShader);
        if (Points.size() > 0 && xyList != null && xyList.size() > 0) {
            baseLinePath.lineTo(xyList.get(Points.size() - 1).x, sumHeight - buttomHeiht);
            baseLinePath.lineTo(xyList.get(0).x, sumHeight - buttomHeiht);
            baseLinePath.close();
            canvas.drawPath(baseLinePath, baseShadow);
        }

    }

    public int dp2px(float dp) {
        final float scale = this.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    /**
     * 取出时间里面的最大的一个用来计算总长度
     *
     * @param timeList
     * @return
     */
    public float getMaxTime(List<Integer> timeList) {
        maxTime = 0;
        for (int i = 0; i < timeList.size(); i++) {
            if (maxTime < timeList.get(i)) {
                maxTime = timeList.get(i);
            }
        }
        if (maxTime <= 1000) {
            maxTime = 1000;
        } else {
            maxTime = 10000;
        }
        return maxTime;
    }

    public int dp2px(Context context, float dp) {
        final float scale = context.getResources().getDisplayMetrics().density;
        return (int) (dp * scale + 0.5f);
    }

    public float dp2px(Resources resources, float dp) {
        final float scale = resources.getDisplayMetrics().density;
        return dp * scale + 0.5f;
    }

    public float sp2px(Resources resources, float sp) {
        final float scale = resources.getDisplayMetrics().scaledDensity;
        return sp * scale;
    }

    public String getCurrenTimeFromLongToGraph(String longTime) {
        SimpleDateFormat formatter = new SimpleDateFormat("MM-dd HH:mm");
        long milliSecond = Long.valueOf(longTime);
        Date date = new Date();
        date.setTime(milliSecond);
        return formatter.format(date);
    }
}

分别使用不同的paint,不可以使用同一个paint,可以看一下Paint的源码,就知道原因了