一条折线,根据最大最小值自动缩放上下限。
View
FloatBuffer
存储import android.content.Context; import android.graphics.Canvas; import android.graphics.Color; import android.graphics.Paint; import android.graphics.Rect; import android.util.AttributeSet; import android.util.Log; import android.util.TypedValue; import android.view.View; import java.nio.FloatBuffer; public class AutoLineChart extends View { private static final String TAG = "rustApp" + AutoLineChart.class.getSimpleName(); private float yMax = 1.6f; private float yMin = -1.0f; float yAxisZoomLimitMax = 1.6f; float yAxisZoomLimitMin = -1.0f; // 缩小y轴的极限值 // Y轴自动缩放时的增减距离 float axisYPerStep = 0.1f; // 图表线条在view顶部留出的间距 float viewYStart = 2; float axisTextSize = 10; private int onShowPointsCount = 500; // 当前显示的数据个数 int onShowMinPoints = 100; // 至少要显示的数据个数 private int maxPoint = 9000; // 数据存储最大个数 // 坐标轴线条宽度 float axisLineWid = 1f; int dataLineWid = 4; // 数据线颜色 private int dataColor = Color.parseColor("#eaffe9"); // 图表中的背景线条颜色 private int mainBgLineColor = Color.parseColor("#535353"); // 坐标轴颜色 private int axisColor = Color.WHITE; // 坐标值字体颜色 private int axisTextColor = Color.WHITE; // 背景色 private int viewBgColor = Color.parseColor("#222222"); Rect rectText = new Rect(); private float xStep = 1.0f; private float viewWidth; private float viewHeight; private float botLeftXOnView = 0; // 图表左下点在view中的x坐标 private float botLeftYOnView = 0; private float originYToBottom = 20; // 图表原点距离view底部的距离 private FloatBuffer dataBuffer; private Paint bgPaint; private Paint linePaint; public AutoLineChart(Context context) { this(context, null); } public AutoLineChart(Context context, AttributeSet attrs) { this(context, attrs, 0); } public AutoLineChart(Context context, AttributeSet attrs, int defStyleAttr) { super(context, attrs, defStyleAttr); init(context); } public int getMaxPoint() { return maxPoint; } public void setOnShowPointsCount(int onShowPointsCount) { this.onShowPointsCount = onShowPointsCount; } public int getOnShowPointsCount() { return onShowPointsCount; } public int getOnShowMinPoints() { return onShowMinPoints; } public void addData(float data) { dataBuffer.put(data); if (dataBuffer.position() > (dataBuffer.capacity() * 2 / 3)) { float[] bufferArr = dataBuffer.array(); System.arraycopy(bufferArr, dataBuffer.position() - maxPoint, bufferArr, 0, maxPoint); dataBuffer.position(maxPoint); // Log.d(TAG, "把当前数据移动到buffer起始位置 " + dataBuffer); } invalidate(); } private void init(Context context) { dataBuffer = FloatBuffer.allocate(3 * maxPoint); // 分配3倍的空间 bgPaint = new Paint(Paint.ANTI_ALIAS_FLAG); linePaint = new Paint(Paint.ANTI_ALIAS_FLAG); bgPaint.setStrokeWidth(axisLineWid); bgPaint.setStyle(Paint.Style.STROKE); bgPaint.setColor(mainBgLineColor); linePaint.setStrokeWidth(dataLineWid); linePaint.setStyle(Paint.Style.STROKE); linePaint.setColor(dataColor); botLeftXOnView = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 32, context.getResources().getDisplayMetrics()); originYToBottom = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, context.getResources().getDisplayMetrics()); viewYStart = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 20, context.getResources().getDisplayMetrics()); axisLineWid = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_DIP, 1, context.getResources().getDisplayMetrics()); axisTextSize = TypedValue.applyDimension(TypedValue.COMPLEX_UNIT_SP, 10, context.getResources().getDisplayMetrics()); } @Override protected void onSizeChanged(int w, int h, int oldw, int oldh) { super.onSizeChanged(w, h, oldw, oldh); viewWidth = getWidth(); viewHeight = getHeight(); botLeftYOnView = viewHeight - originYToBottom; } @Override protected void onDraw(Canvas canvas) { super.onDraw(canvas); canvas.drawColor(viewBgColor); xStep = (viewWidth - botLeftXOnView) / (onShowPointsCount - 1); float maxData = 0.1f; float minData = 0; int dataStartIndexInBuffer = 0; // 数据在buffer中的起始下标 if (dataBuffer.position() > onShowPointsCount) { dataStartIndexInBuffer = dataBuffer.position() - onShowPointsCount; } float[] bufferArr = dataBuffer.array(); for (int i = dataStartIndexInBuffer; i < dataBuffer.position(); i++) { if (bufferArr[i] < minData) { minData = bufferArr[i]; } else if (bufferArr[i] > maxData) { maxData = bufferArr[i]; } } zoomYAxis(maxData, minData); drawBgLines(canvas); drawWave(canvas, dataStartIndexInBuffer); } // 缩放Y轴 private void zoomYAxis(float maxData, float minData) { if (maxData < yAxisZoomLimitMax) { yMax = yAxisZoomLimitMax; } else if (maxData < yMax) { while (maxData < yMax) { yMax -= axisYPerStep; } yMax += axisYPerStep; } else if (maxData > yMax) { while (maxData > yMax) { yMax += axisYPerStep; } } if (minData > yAxisZoomLimitMin) { yMin = yAxisZoomLimitMin; } else if (minData > yMin) { while (minData > yMin) { yMin += axisYPerStep; } yMin -= axisYPerStep; } else if (minData < yMin) { yMin -= axisYPerStep; } } private void drawBgLines(Canvas canvas) { // 画坐标轴 bgPaint.setStyle(Paint.Style.FILL); bgPaint.setStrokeWidth(axisLineWid); bgPaint.setTextSize(axisTextSize); bgPaint.setTextAlign(Paint.Align.RIGHT); for (float y = 0; y <= yMax; y += 0.5) { drawYAxis(canvas, y); } for (float y = 0; y >= yMin; y -= 0.5) { drawYAxis(canvas, y); } bgPaint.setColor(axisColor); canvas.drawLine(botLeftXOnView, viewYStart / 2, botLeftXOnView, botLeftYOnView + viewYStart / 2, bgPaint); // canvas.drawLine(botLeftXOnView, botLeftYOnView, viewWidth, botLeftYOnView, bgPaint); // x轴 } private void drawYAxis(Canvas canvas, float axisYValue) { final float yDataRange = yMax - yMin; final float yAxisRangeOnView = botLeftYOnView - viewYStart; float aY = botLeftYOnView - (axisYValue - yMin) / yDataRange * yAxisRangeOnView; bgPaint.setColor(axisColor); canvas.drawLine(botLeftXOnView - 20, aY, botLeftXOnView, aY, bgPaint); String axisText = String.valueOf(axisYValue); bgPaint.getTextBounds(axisText, 0, axisText.length(), rectText); // 获取文本的宽高 canvas.drawText(axisText, botLeftXOnView - rectText.width() / 2, aY + rectText.height() / 2, bgPaint); bgPaint.setColor(mainBgLineColor); canvas.drawLine(botLeftXOnView, aY, viewWidth, aY, bgPaint); } private void drawWave(Canvas canvas, int dataStartIndexInBuffer) { final float yDataRange = yMax - yMin; final float yAxisRangeOnView = botLeftYOnView - viewYStart; final float yDataStep = yAxisRangeOnView / yDataRange; float[] dataArr = dataBuffer.array(); for (int i = dataStartIndexInBuffer; i < dataBuffer.position() - 1; i++) { canvas.drawLine(botLeftXOnView + (i - dataStartIndexInBuffer) * xStep, getYL(dataArr[i], yDataStep), botLeftXOnView + (i - dataStartIndexInBuffer + 1) * xStep, getYL(dataArr[i + 1], yDataStep), linePaint); } } private float getYL(final float yData, float yDataStep) { return botLeftYOnView - (yData - yMin) * yDataStep; } }