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