Android自動(dòng)縮放上下限折線圖示例
正文
一條折線,根據(jù)最大最小值自動(dòng)縮放上下限。
- 繼承
View - 數(shù)據(jù)使用
FloatBuffer存儲(chǔ) - 可改變顯示窗口的大小
- 可指定坐標(biāo)軸,折線和字體顏色
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軸自動(dòng)縮放時(shí)的增減距離
float axisYPerStep = 0.1f;
// 圖表線條在view頂部留出的間距
float viewYStart = 2;
float axisTextSize = 10;
private int onShowPointsCount = 500; // 當(dāng)前顯示的數(shù)據(jù)個(gè)數(shù)
int onShowMinPoints = 100; // 至少要顯示的數(shù)據(jù)個(gè)數(shù)
private int maxPoint = 9000; // 數(shù)據(jù)存儲(chǔ)最大個(gè)數(shù)
// 坐標(biāo)軸線條寬度
float axisLineWid = 1f;
int dataLineWid = 4;
// 數(shù)據(jù)線顏色
private int dataColor = Color.parseColor("#eaffe9");
// 圖表中的背景線條顏色
private int mainBgLineColor = Color.parseColor("#535353");
// 坐標(biāo)軸顏色
private int axisColor = Color.WHITE;
// 坐標(biāo)值字體顏色
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; // 圖表左下點(diǎn)在view中的x坐標(biāo)
private float botLeftYOnView = 0;
private float originYToBottom = 20; // 圖表原點(diǎn)距離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, "把當(dāng)前數(shù)據(jù)移動(dòng)到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; // 數(shù)據(jù)在buffer中的起始下標(biāo)
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) {
// 畫(huà)坐標(biāo)軸
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 自動(dòng)縮放上下限的折線圖的詳細(xì)內(nèi)容,更多關(guān)于Android 自動(dòng)縮放上下限的折線圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
相關(guān)文章
Listview加載的性能優(yōu)化是如何實(shí)現(xiàn)的
在android開(kāi)發(fā)中Listview是一個(gè)很重要的組件,它以列表的形式根據(jù)數(shù)據(jù)的長(zhǎng)自適應(yīng)展示具體內(nèi)容,用戶可以自由的定義listview每一列的布局,接下來(lái)通過(guò)本文給大家介紹Listview加載的性能優(yōu)化是如何實(shí)現(xiàn)的,對(duì)listview性能優(yōu)化相關(guān)知識(shí)感興趣的朋友一起學(xué)習(xí)吧2016-01-01
Android掃描二維碼時(shí)出現(xiàn)用戶禁止權(quán)限報(bào)錯(cuò)問(wèn)題解決辦法
這篇文章主要介紹了Android掃描二維碼時(shí)出現(xiàn)用戶禁止權(quán)限報(bào)錯(cuò)問(wèn)題解決辦法的相關(guān)資料,需要的朋友可以參考下2017-06-06
Android中利用App實(shí)現(xiàn)消息推送機(jī)制的代碼
Android中利用App實(shí)現(xiàn)消息推送機(jī)制的代碼,需要的朋友可以參考下。2011-05-05
Android開(kāi)發(fā)中的9個(gè)常見(jiàn)錯(cuò)誤和解決方法
這篇文章主要介紹了Android開(kāi)發(fā)中的9個(gè)常見(jiàn)錯(cuò)誤和解決方法,這是Android開(kāi)發(fā)中最常見(jiàn)的9個(gè)錯(cuò)誤,經(jīng)過(guò)各種各樣的整理,以及和熱心網(wǎng)友討論總結(jié)而來(lái),需要的朋友可以參考下2015-01-01
Android自定義ViewGroup實(shí)現(xiàn)帶箭頭的圓角矩形菜單
這篇文章主要為大家詳細(xì)介紹了Android自定義ViewGroup帶箭頭的圓角矩形菜單實(shí)現(xiàn)方法,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2016-07-07
Android實(shí)現(xiàn)獲取簽名及公鑰的方法
這篇文章主要介紹了Android實(shí)現(xiàn)獲取簽名及公鑰的方法,可實(shí)現(xiàn)Android通過(guò)包名獲取相關(guān)簽名及公鑰的功能,具有一定參考借鑒價(jià)值,需要的朋友可以參考下2015-10-10

