我是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();}
}
}
答案 0 :(得分:1)
我认为这会更好:
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();}
}
}
}
}