2012-07-03 55 views
0

我新來android。我使用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
  1. 你之前重新繪製圖表實際上是添加到視圖,以便它不會畫。
  2. 您的代碼不在您創建的線程內。
  3. 您正在創建600個線程,但您沒有運行它們。
  4. 您的onResume代碼看起來像您認爲onResume被調用很多次,通常是錯誤的。
  5. 活動沒有任何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();} 
      } 
     } 
    } 
} 
+0

感謝了很多人,它的工作:)......但是當我儘量減少睡眠時間(即使圖形移動得更快)應用程序崩潰。我能夠走到300毫秒,低於它只是崩潰。任何解決方案另一件事,當我試圖在單個屏幕上顯示14個圖表(我知道它太笨重),它會持續一段時間,然後崩潰。你能解釋爲什麼嗎? achartengine不能做一些快速處理嗎? – user1090992

+0

我猜想這是一個提升條件,view_eeg1.repaint()會在您降低睡眠時實例化view_eeg1之前運行。您應該將實例化移至onCreate,或者添加一個控件以檢查它是否在重繪之前。 –

+0

嗨,在我們運行此代碼之前,我們需要在我們的應用程序中添加achart引擎API(Jar文件)。你可以下載api文件[這裏](http://code.google.com/p/achartengine/downloads/detail?name=achartengine-1.0.0.jar&can=2&q=) – harikrishnan