2012-06-29 73 views
0

我是android新手,需要幫助將空心代碼轉換爲實時更新圖形,該圖形顯示心電圖跟蹤。 現在讓我們用正弦曲線工作。所以我需要一個顯示移動的正弦圖的圖形。 現在我只是一個簡單的代碼,它使用aChartEngine插件顯示靜態圖形。 使用可伸縮模式(線程/異步模式)實現移動(實時更新)圖的最簡單方法是什麼?使用Async任務實現圖形的實時更新

public class LiveView extends Activity { 
Button startTimer; 
TextView heartRate, battery; 
LinearLayout lineGraphLayout; 
View lineGraph; 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.liveview); 
    startTimer = (Button) findViewById(R.id.bStartTimer); 
    heartRate = (TextView) findViewById(R.id.tvHeartRate); 
    battery = (TextView) findViewById(R.id.tvBatteryLevel); 
    lineGraphLayout = (LinearLayout) findViewById(R.id.lineGraph); 
} 

@Override 
protected void onResume() { 
    // TODO Auto-generated method stub 
    super.onResume(); 
    if (lineGraph == null) { 
     // sample Values 
     lineGraph = getLineGraphView(); 
     lineGraphLayout.addView(lineGraph); 
    } else 
     ((GraphicalView) lineGraph).repaint(); 
} 


private View getLineGraphView() { 
    int[] x = { 1, 2, 3, 4, 5, 6, 7, 8, 9, 10 }; // x values! 
    int[] y = { 30, 34, 45, 57, 77, -89, 100, 111, 123, 145 }; 
    TimeSeries series = new TimeSeries("Lead 1"); 
    for (int i = 0; i < x.length; i++) 
     series.add(x[i], y[i]); 

    XYMultipleSeriesDataset dataset = new XYMultipleSeriesDataset(); 
    dataset.addSeries(series); 

    XYMultipleSeriesRenderer mRenderer = new XYMultipleSeriesRenderer(); 
    XYSeriesRenderer renderer = new XYSeriesRenderer(); 
    mRenderer.addSeriesRenderer(renderer); 

    View tempView = ChartFactory.getLineChartView(this, dataset, mRenderer); 
    tempView.setBackgroundColor(Color.WHITE); 
    return tempView; 
} 

}

我嘗試使用TimerTask的......但應用程序只是掛!

public void toCallAsynchronous() { 
    TimerTask doAsynchronousTask; 
    final Handler handler = new Handler(); 
    Timer timer = new Timer(); 

    doAsynchronousTask = new TimerTask() { 

     @Override 
     public void run() { 
      // TODO Auto-generated method stub 
      handler.post(new Runnable() { 
       public void run() { 
        if (isRunning) { 
         try { 
          lineGraph = getLineGraphView(); 
          lineGraphLayout.addView(lineGraph); 
          ((GraphicalView) lineGraph).repaint(); 

         } catch (Exception e) { 
          // TODO Auto-generated catch block 

         } 
        } 
       } 
      }); 

     } 

    }; 

    timer.schedule(doAsynchronousTask, 0, 50);// execute in every 500 
                // ms 

回答

1

我認爲Handler是不是AsycTask或線程在這裏更好。處理程序直接運行在UI線程上,很容易實現:

public class LiveView extends Activity { 
    //... 
    RefreshHandler handler = new RefreshHandler(); 

    class RefreshHandler extends Handler { 
     @Override 
     public void handleMessage(Message msg) { 
      // Update your graph here 
      sendMessageDelayed(obtainMessage(0), 500); // next update in 500 ms 
     } 
    } 
} 

要啓動實時更新,請致電您的代碼handler.sendMessage(handler.obtainMessage(0));地方。

+2

只是關於處理程序,它們實際上綁定到創建它們的線程,因此不一定是GUI線程。 http://developer.android.com/reference/android/os/Handler.html – Kerry