我是安卓新手。我正在使用 Achartengine 从二进制文件中绘制动态图读取值。下面给出的是我的代码。我想要一个动态运行图,其中的值会定期更新。为此,我尝试使用和不使用计时器来做到这一点。但是每次我在更新最后一个值时得到带有最终值的图表。我的意思是在所有更新完成后我得到一个静态图。我不知道如何纠正这个问题。我正在使用模拟器来测试代码。模拟器有问题还是我的代码有问题?提前致谢
没有计时器的代码:
public class RandomActivity extends Activity {
private XYMultipleSeriesDataset eeg1 = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer renderer_eeg1 = new XYMultipleSeriesRenderer();
private XYSeries series1;
private GraphicalView view_eeg1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
renderer_eeg1.setAxisTitleTextSize(16);
renderer_eeg1.setChartTitleTextSize(20);
renderer_eeg1.setLabelsTextSize(15);
renderer_eeg1.setLegendTextSize(15);
renderer_eeg1.setMargins(new int[] {20, 30, 15, 0});
renderer_eeg1.setAxesColor(Color.BLUE);
renderer_eeg1.setXTitle("Time");
renderer_eeg1.setChartTitle("eeg1");
XYSeriesRenderer seriesrenderer1 = new XYSeriesRenderer();
seriesrenderer1.setColor(Color.YELLOW);
renderer_eeg1.addSeriesRenderer(seriesrenderer1);
series1= new XYSeries("eeg1");
eeg1.addSeries(series1);
int a;
int b;
add();
for(int r=0;r<=600;r++){
if(r%2==0){
a=1;
b=-1;
}
else{
a=-1;
b=1;
}
series1.add(r, a);
if(r<=50){
renderer_eeg1.setXAxisMax(50);
}
else if(r>50){
double maxX = series1.getMaxX();
double minX = maxX - 50;
renderer_eeg1.setXAxisMin(minX);
}
view_eeg1.repaint();
}
}
protected void onResume() {
super.onResume();
if (view_eeg1== null) {
LinearLayout layout_eeg1 = (LinearLayout) findViewById(R.id.chart1);
view_eeg1= ChartFactory.getLineChartView(this, eeg1, renderer_eeg1);
layout_eeg1.addView(view_eeg1,new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
} else {
view_eeg1.repaint();}
}
}
带计时器的代码:
public class RandomActivity extends Activity {
private XYMultipleSeriesDataset eeg1 = new XYMultipleSeriesDataset();
private XYMultipleSeriesRenderer renderer_eeg1 = new XYMultipleSeriesRenderer();
private XYSeries series1;
private GraphicalView view_eeg1;
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
renderer_eeg1.setAxisTitleTextSize(16);
renderer_eeg1.setChartTitleTextSize(20);
renderer_eeg1.setLabelsTextSize(15);
renderer_eeg1.setLegendTextSize(15);
renderer_eeg1.setMargins(new int[] {20, 30, 15, 0});
renderer_eeg1.setAxesColor(Color.BLUE);
renderer_eeg1.setXTitle("Time");
renderer_eeg1.setChartTitle("eeg1");
XYSeriesRenderer seriesrenderer1 = new XYSeriesRenderer();
seriesrenderer1.setColor(Color.YELLOW);
renderer_eeg1.addSeriesRenderer(seriesrenderer1);
series1= new XYSeries("eeg1");
eeg1.addSeries(series1);
int a;
int b;
add();
for(int r=0;r<=600;r++){
Thread timer = new Thread(){
public void run(){
try{
sleep(500);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
};
if(r%2==0){
a=1;
b=-1;
}
else{
a=-1;
b=1;
}
series1.add(r, a);
if(r<=50){
renderer_eeg1.setXAxisMax(50);
}
else if(r>50){
double maxX = series1.getMaxX();
double minX = maxX - 50;
renderer_eeg1.setXAxisMin(minX);
}
view_eeg1.repaint();
}
}
protected void onResume() {
super.onResume();
if (view_eeg1== null) {
LinearLayout layout_eeg1 = (LinearLayout) findViewById(R.id.chart1);
view_eeg1= ChartFactory.getLineChartView(this, eeg1, renderer_eeg1);
layout_eeg1.addView(view_eeg1,new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT));
} else {
view_eeg1.repaint();}
}
}