0

我是安卓新手。我正在使用 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();}
}

}
4

1 回答 1

1
  1. 您在图表实际添加到视图之前重新绘制,因此它不会被绘制。
  2. 您的代码不在您正在创建的线程内。
  3. 您正在创建 600 个线程,但您没有运行它们。
  4. 您的 onResume 代码看起来像您认为 onResume 被多次调用,这通常是错误的。
  5. Activity 没有任何 Add() 函数,你有一个应该做什么?

我认为这会更好:

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;
    Thread timer = new Thread(){
        public void run(){
            try{
                for(int r=0;r<=600;r++){
                    sleep(500);
                    if(r%2==0){
                        a=1;
                    }
                    else{
                        a=-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();

                } catch (InterruptedException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                } 
            }
        };
    timer.start()
    }

    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();}
            }
        }
    }
}
于 2012-07-11T17:11:45.457 回答