吾爱破解 - 52pojie.cn

 找回密码
 注册[Register]

QQ登录

只需一步,快速开始

查看: 1093|回复: 1
收起左侧

[求助] 【安卓】自定义折线图view动态添加数据坐标会出问题,静态没事

[复制链接]
采集的小蜜蜂 发表于 2021-6-26 11:41
我自己写了个折线图view,把以下自定义view放到自己的目录下,直接在xml中用一个Horizantalscrollview包了一个<com.包名.LineDataView />(我的控件)
如果直接给datas添加写的数据,0~140之间的数据可以正常显示。
但是如果每隔一秒给这个数组添加一个数,绿线画的正常,红线和黄线就乱了。有没有给大神看一下。
[Java] 纯文本查看 复制代码
package com.rmt.relax.custom;

import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;

import androidx.annotation.Nullable;

import java.util.ArrayList;

public class LineDataView extends View {
    private float mWidth, mHeight;
    private Paint paint1;
    private Paint paint2;
    private Paint paint3;
    private float everyvaluePercent;
    private int topvalue=140;
    private int cut1=60,cut2=100;
    private float valueSpace = 80;
    private ArrayList<Integer> datas = new ArrayList<>();
    private float downx;

    public LineDataView(Context context) {
        super(context);
        init();
    }

    public LineDataView(Context context, [url=home.php?mod=space&uid=1043391]@nullable[/url] AttributeSet attrs) {
        super(context, attrs);
        init();
    }

    public LineDataView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        init();
    }

    @Override
    protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
        super.onMeasure(widthMeasureSpec, heightMeasureSpec);
        mHeight = getMeasuredHeight();
        setMeasuredDimension((int) (datas.size()*valueSpace),(int)mHeight);
    }

    @Override
    protected void onDraw(Canvas canvas) {
        super.onDraw(canvas);
        if(datas.size()>0) {
            everyvaluePercent = mHeight / topvalue;
            float lastx, lasty;
            lastx = 0;
            lasty = mHeight - datas.get(0) * everyvaluePercent;
            for (int i = 1; i < datas.size(); i++) {
                if (datas.get(i) <= datas.get(i - 1)) {
                    canvas.drawLine(lastx, lasty, lastx + valueSpace, mHeight - datas.get(i) * everyvaluePercent, paint2);
                    if (datas.get(i) > cut2) {
                        canvas.drawLine(lastx, lasty, lastx + valueSpace, mHeight - datas.get(i) * everyvaluePercent, paint1);
                    } else if (datas.get(i) <= cut2) {
                        if (datas.get(i) <= cut1 && datas.get(i - 1) > cut2) {
                            float x1 = (getValueHeight(datas.get(i - 1)) - getValueHeight(cut2)) * (valueSpace) / (getValueHeight(datas.get(i - 1)) - getValueHeight(datas.get(i)));
                            canvas.drawLine(lastx, lasty, lastx + x1, mHeight - (float) getValueHeight(cut2), paint1);
                            float x2 = (getValueHeight(datas.get(i - 1)) - getValueHeight(cut1)) * (valueSpace) / (getValueHeight(datas.get(i - 1)) - getValueHeight(datas.get(i)));
                            canvas.drawLine(lastx + x2, mHeight - (float) getValueHeight(cut1), lastx + valueSpace, mHeight - datas.get(i) * everyvaluePercent, paint3);
                        } else if (datas.get(i) <= cut1 && datas.get(i - 1) <= cut2) {
                            canvas.drawLine(lastx, lasty, lastx + valueSpace, mHeight - datas.get(i) * everyvaluePercent, paint3);
                        } else if (datas.get(i) > cut1) {
                            float x1 = (getValueHeight(datas.get(i - 1)) - getValueHeight(cut2)) * (valueSpace) / (getValueHeight(datas.get(i - 1)) - getValueHeight(datas.get(i)));
                            canvas.drawLine(lastx, lasty, lastx + x1, mHeight - (float) getValueHeight(cut2), paint1);
                        }
                    }
                    lastx = lastx + valueSpace;
                    lasty = mHeight - datas.get(i) * everyvaluePercent;

                } else {
                    canvas.drawLine(lastx, lasty, lastx + valueSpace, mHeight - datas.get(i) * everyvaluePercent, paint2);
                    if (datas.get(i) > cut1) {
                        if (datas.get(i) < cut2) {
                            float x = (getValueHeight(cut1) - getValueHeight(datas.get(i - 1))) * (valueSpace) / (getValueHeight(datas.get(i)) - getValueHeight(datas.get(i - 1)));
                            canvas.drawLine(lastx, lasty, lastx + x, mHeight - (float) getValueHeight(cut1), paint3);
                        } else if (datas.get(i) > cut2 && datas.get(i - 1) < cut2) {
                            float x1 = (getValueHeight(cut1) - getValueHeight(datas.get(i - 1))) * (valueSpace) / (getValueHeight(datas.get(i)) - getValueHeight(datas.get(i - 1)));
                            canvas.drawLine(lastx, lasty, lastx + x1, mHeight - (float) getValueHeight(cut1), paint3);
                            float x2 = (getValueHeight(cut2) - getValueHeight(datas.get(i - 1))) * (valueSpace) / (getValueHeight(datas.get(i)) - getValueHeight(datas.get(i - 1)));
                            canvas.drawLine(lastx + x2, mHeight - (float) getValueHeight(cut2), lastx + valueSpace, mHeight - datas.get(i) * everyvaluePercent, paint1);
                        } else if (datas.get(i) > cut2 && datas.get(i - 1) >= cut2) {
                            canvas.drawLine(lastx, lasty, lastx + valueSpace, mHeight - datas.get(i) * everyvaluePercent, paint1);
                        }
                    } else if (datas.get(i) <= cut1) {
                        canvas.drawLine(lastx, lasty, lastx + valueSpace, mHeight - datas.get(i) * everyvaluePercent, paint3);
                    }
                    lastx = lastx + valueSpace;
                    lasty = mHeight - datas.get(i) * everyvaluePercent;
                }

            }
        }

    }

    public void init() {
        paint1 = new Paint();
        paint1.setAntiAlias(true);
        paint1.setStyle(Paint.Style.FILL);
        paint1.setStrokeWidth(3);
        paint1.setColor(Color.RED);

        paint2 = new Paint();
        paint2.setAntiAlias(true);
        paint2.setStyle(Paint.Style.FILL);
        paint2.setStrokeWidth(3);
        paint2.setColor(Color.GREEN);

        paint3 = new Paint();
        paint3.setAntiAlias(true);
        paint3.setStyle(Paint.Style.FILL);
        paint3.setStrokeWidth(3);
        paint3.setColor(Color.YELLOW);

    }

    public void refrelayout() {
        
        invalidate();
        requestLayout();
    }

    public void setTopvalue(int value) {
        this.topvalue = value;
        refrelayout();
    }

    public void setDatas(ArrayList<Integer> datas) {
        this.datas = datas;
        mWidth=datas.size()*valueSpace;
        refrelayout();
    }
    public float getValueHeight(float i){
        return i*everyvaluePercent;
    }
}

发帖前要善用论坛搜索功能,那里可能会有你要找的答案或者已经有人发布过相同内容了,请勿重复发帖。

头像被屏蔽
844043335 发表于 2021-6-26 12:45
提示: 作者被禁止或删除 内容自动屏蔽
您需要登录后才可以回帖 登录 | 注册[Register]

本版积分规则

返回列表

RSS订阅|小黑屋|处罚记录|联系我们|吾爱破解 - LCG - LSG ( 京ICP备16042023号 | 京公网安备 11010502030087号 )

GMT+8, 2024-11-26 00:44

Powered by Discuz!

Copyright © 2001-2020, Tencent Cloud.

快速回复 返回顶部 返回列表