gpt4 book ai didi

java - 添加新数据时 GraphView 不更新

转载 作者:行者123 更新时间:2023-12-02 05:10:34 24 4
gpt4 key购买 nike

我有一个 GraphView,它在添加新数据时不会更新,它会更改 Y 值以适应新数据,但不会绘制它们,代码如下:

 public class MainActivity extends Activity {
double data = 1;
double xgraph = 5;
GraphViewSeries sensorSeries;
GraphView graphView;
...

@Override
public void onCreate(Bundle savedInstanceState) {
...
sensorSeries = new GraphViewSeries(new GraphViewData[] {
new GraphViewData(1, 2.0d),
new GraphViewData(2, 1.5d),
new GraphViewData(3, 2.5d),
new GraphViewData(4, 1.0d)
});
graphView = new LineGraphView(this,"TEST");
graphView.addSeries(sensorSeries);
LinearLayout layout = (LinearLayout) findViewById(R.id.graph2);
graphView.setViewPort(0, 20);
layout.addView(graphView);

refreshButton.setOnClickListener(new View.OnClickListener() {
public void onClick(View v) {
sensorSeries.appendData(new GraphViewData(xgraph, data), false);
graphView.redrawAll();
myLabel.setText(""+data+" "+xgraph);
xgraph++;
dataAcx++;
}
});
}
...
}

最佳答案

要使更改生效,您需要调用:

graphView.onDataChanged(false, false);

而不是 View.OnClickListener 实现中的 graphView.redrawAll()。您必须使用参数来确保最佳性能,请查看源代码中的javadoc:

/**
* Call this to let the graph redraw and recalculate the viewport.
*
* This will be called when a new series was added or removed and when data was appended
* via {@link com.jjoe64.graphview.series.BaseSeries#appendData(com.jjoe64.graphview.series.DataPointInterface, boolean, int)}
* or {@link com.jjoe64.graphview.series.BaseSeries#resetData(com.jjoe64.graphview.series.DataPointInterface[])}.
*
* @param keepLabelsSize true if you don't want to recalculate the size of the labels.
* It is recommended to use "true" because this will improve
* performance and prevent a flickering.
* @param keepViewport true if you don't want that the viewport will be recalculated.
* It is recommended to use "true" for performance.
*/
public void onDataChanged(boolean keepLabelsSize, boolean keepViewport) { /*...*/ }

根本不需要Thread/Runnable

关于java - 添加新数据时 GraphView 不更新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27375139/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com