使用Achartengine的android动态情节

时间:2012-07-03 05:02:04

标签: android plot achartengine

我是android新手。我使用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();}
}

}

1 个答案:

答案 0 :(得分:1)

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

    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();}
                }
            }
        }
    }