gpt4 book ai didi

android - 每秒更新折线图

转载 作者:行者123 更新时间:2023-11-29 21:23:22 24 4
gpt4 key购买 nike

我想使用 this example 中的其中一个类

我正在显示折线图 like this

在我的例子中,x 轴代表时间,所以我想通过调用下面的执行函数并执行以下操作来每秒更新图表:1-添加更多点 2-将平移移动到我当前的时间

所以最后我希望看到情节中的线条随时间移动

我尝试创建一个名为 update 的函数,它创建一个新线程并从 onResume 调用它,但是应用程序崩溃了,我得到了

AndroidRuntime(23861): FATAL EXCEPTION: Thread-11775
AndroidRuntime(23861): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
AndroidRuntime(23861): android.view.ViewRootImpl$CalledFromWrongThreadException: Only the original thread that created a view hierarchy can touch its views.
AndroidRuntime(23861): at android.view.ViewRootImpl.checkThread(ViewRootImpl.java:5908)
AndroidRuntime(23861): at android.view.ViewRootImpl.requestLayout(ViewRootImpl.java:837)
AndroidRuntime(23861): at android.view.View.requestLayout(View.java:15792)
AndroidRuntime(23861): at android.widget.RelativeLayout.requestLayout(RelativeLayout.java:358)
AndroidRuntime(23861): at android.view.ViewGroup.removeAllViews(ViewGroup.java:3902)

这是我的主课

public class ChartDemo extends Activity {
private AverageCubicTemperatureChart TCh = new AverageCubicTemperatureChart();
RelativeLayout LayoutToDisplayChart;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);

LayoutToDisplayChart=(RelativeLayout)findViewById(R.id.relative_layout1);
}

@Override
protected void onResume() {
super.onResume();
Intent achartIntent = TCh.execute(this,LayoutToDisplayChart);
AverageCubicTemperatureChart.update(this,LayoutToDisplayChart);

}
}

AverageCubicTemperatureChart 类的代码

/**
* Average temperature demo chart.
*/
public class AverageCubicTemperatureChart implements IDemoChart {
/**
* Returns the chart name.
*
* @return the chart name
*/
public String getName() {
return "Average temperature";
}

public static void update(Context c, RelativeLayout p)
{
thread = new Thread() {
public void run() {
while(true) {
try {
Thread.sleep(1000);
} catch (InterruptedException e) {
e.printStackTrace();
}

parent.removeAllViews();
execute(c,p);

}
}
};
thread.start();
}

/**
* Returns the chart description.
*
* @return the chart description
*/
public String getDesc() {
return "The average temperature in 4 Greek islands (cubic line chart)";
}

/**
* Executes the chart demo.
*
* @param context the context
* @return the built intent
*/
public Intent execute(Context context,RelativeLayout parent) {
String[] titles = new String[] { "Crete", "Corfu", "Thassos", "Skiathos" };
List<double[]> x = new ArrayList<double[]>();
for (int i = 0; i < titles.length; i++) {
x.add(new double[] { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12 });
}
List<double[]> values = new ArrayList<double[]>();
values.add(new double[] { 12.3, 12.5, 13.8, 16.8, 20.4, 24.4, 26.4, 26.1, 23.6, 20.3, 17.2,
13.9 });
values.add(new double[] { 10, 10, 12, 15, 20, 24, 26, 26, 23, 18, 14, 11 });
values.add(new double[] { 5, 5.3, 8, 12, 17, 22, 24.2, 24, 19, 15, 9, 6 });
values.add(new double[] { 9, 10, 11, 15, 19, 23, 26, 25, 22, 18, 13, 10 });
int[] colors = new int[] { Color.BLUE, Color.GREEN, Color.CYAN, Color.YELLOW };
PointStyle[] styles = new PointStyle[] { PointStyle.CIRCLE, PointStyle.DIAMOND,
PointStyle.TRIANGLE, PointStyle.SQUARE };
XYMultipleSeriesRenderer renderer = AbstractDemoChart.buildRenderer(colors, styles);
int length = renderer.getSeriesRendererCount();
for (int i = 0; i < length; i++) {
((XYSeriesRenderer) renderer.getSeriesRendererAt(i)).setFillPoints(true);
}
AbstractDemoChart.setChartSettings(renderer, "Average temperature", "Month", "Temperature", 0, 100, 0, 100,
Color.LTGRAY, Color.LTGRAY);
renderer.setXLabels(12);
renderer.setYLabels(10);
renderer.setShowGrid(true);
renderer.setXLabelsAlign(Align.RIGHT);
renderer.setYLabelsAlign(Align.RIGHT);
renderer.setZoomButtonsVisible(true);
renderer.setPanLimits(new double[] { 0, 200, 0, 100 });
renderer.setZoomLimits(new double[] { 0, 200, 0, 100 });
Intent intent = ChartFactory.getCubicLineChartIntent(context, AbstractDemoChart.buildDataset(titles, x, values),
renderer, 0.33f, "Average temperature");
mChartView2=ChartFactory.getCubeLineChartView(context, AbstractDemoChart.buildDataset(titles, x, values), renderer, 0.33f);
parent.addView(mChartView2);
return intent;
}
}

最佳答案

您将不得不使用 runOnUiThread()

正如我已经在这里指出的那样: How to transfer the value of the string from thread to another?

澄清一下:您的 Logcat 错误意味着您无法修改 parent-View in AverageCubicTemperatureChart,因为此线程不是 parent 的所有者。

换行parent.addView(mChartView2) 到:

parent.runOnUiThread(new Runnable() {
@Override
public void run() {
parent.addView(mChartView2);
}
});

应该可以解决你的问题

关于android - 每秒更新折线图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20465172/

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