Android自動縮放上下限折線圖示例
更新時間:2022年08月16日 09:41:35 作者:rustfisher
這篇文章主要為大家介紹了Android?自動縮放上下限的折線圖示例,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進步,早日升職加薪
正文
一條折線,根據最大最小值自動縮放上下限。
- 繼承
View - 數據使用
FloatBuffer存儲 - 可改變顯示窗口的大小
- 可指定坐標軸,折線和字體顏色
AutoLineChart完整代碼
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;
}
}
以上就是Android 自動縮放上下限的折線圖的詳細內容,更多關于Android 自動縮放上下限的折線圖的資料請關注腳本之家其它相關文章!
相關文章
Android實現一個比相冊更高大上的左右滑動特效(附源碼)
這篇文章主要介紹了Android實現一個比相冊更高大上的左右滑動特效(附源碼),本文給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下2021-02-02

