2

We are building an app that is supposed to do several things based on an input text file. First it parses the data in the text file and extracts the useful information and builds a linkedList. The Linked List is a group of BinaryBlock objects. From there we want to dynamically graph the getwfs() function of the BinaryBlocks. At this point, we have got the graph somewhat working but instead of graphing over time it graphs in one big clump and scrolls all the way to the end.

BinaryBlock is listed here:

This is fully tested and functional and really only important in the larger context of the program:

// BinaryBlock.java

public class BinaryBlockLite {


private int pwda[], wfs[];

String lineData;

public BinaryBlockLite( String data ){ // class constructor
    lineData = data;
    pwda = new int[5];
    wfs = new int[4];

    setWfs();

}//end constructor

    public String WfsToString(){
    if (wfs.length == 0)
        return "";

    String data = "Channel 2: ";
    for(int i = 0; i < wfs.length; ++i ){
        data = data + "wfs[" + i + "]=" + wfs[i] + " ";
    }
    return data + "\n";
}


  //===========================================================
 //    Setters
//=============================================================


//read the 5 individual bytes of the Pwda from LineData and into the pwda[] array  
private void setPwda(){
    int location = 13;
    for(int i = 0; i < 5; ++i){

        String temp = "" + lineData.charAt(++location) + lineData.charAt(++location);

        pwda[i] = Integer.parseInt(temp, 16);
    }
}

//logically manipulate the 5 bytes of the PWDA into 4 10-bit WFSs
private void setWfs(){
    setPwda();
    int j = 0;

    for (int i = 0; i < 4; ++i){
        wfs[i] = ((pwda[j] << 2) | (( pwda[j + 1] >> 6) & 0x03)) & 0x03ff;
        wfs[++i] = ((pwda[j + 1] << 4) | (( pwda[j + 2] >>> 4) & 0x0f)) & 0x03ff;
        wfs[++i] = ((pwda[j + 2] << 6) | (( pwda[j + 3] >>> 2) & 0x3f)) & 0x03ff;
        wfs[++i] = ((pwda[j + 3] << 8) | (( pwda[j + 4] >>> 0) & 0xff)) & 0x03ff;
    }
}




  //===========================================================
 //    Getters
//=============================================================


public int[] getPwda(){
    return pwda;
}
public int[] getWfs(){
    return wfs;
}




}//end BinaryBlock Class

The main harness is listed here: The problem is simply that instead of repainting each time and graphing it across the screen, it graphs all of the point at one time.

//MainActivity.java

import android.os.Bundle;
import android.app.ActionBar.LayoutParams;
import android.app.Activity;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Color;
import android.view.Menu;

import android.view.View;
import android.widget.Button;
import android.widget.LinearLayout;

import com.smith.fsu.wave.BinaryBlockLite;

import java.io.BufferedReader;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.util.LinkedList;
import java.util.ListIterator;

import org.achartengine.ChartFactory;
import org.achartengine.GraphicalView;
import org.achartengine.model.XYMultipleSeriesDataset;
import org.achartengine.model.XYSeries;
import org.achartengine.renderer.XYMultipleSeriesRenderer;
import org.achartengine.renderer.XYSeriesRenderer;


public class MainActivity extends Activity {

    public static LinkedList<BinaryBlockLite> list;
    Button btnMain;
    Boolean fileLoaded = false;
    int xTick = 0,
        lastMinX = 0; 
    Context context = this;

    //

    ////////////////////////////////////////////////////////////////
    //import test


    private XYMultipleSeriesDataset WFDataset = new XYMultipleSeriesDataset();
     private XYMultipleSeriesRenderer WaveFormRenderer = new XYMultipleSeriesRenderer();
     private XYSeries WFCurrentSeries;
     private GraphicalView WFChartView;

    //////////////////////////////////////////////////////////////////////////

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        list = new LinkedList<BinaryBlockLite>();
        btnMain=(Button)findViewById(R.id.btnMain);
        WaveFormRenderer.setAxisTitleTextSize(16);
        WaveFormRenderer.setChartTitleTextSize(20);
        WaveFormRenderer.setLabelsTextSize(15);
        WaveFormRenderer.setLegendTextSize(15);
        WaveFormRenderer.setMargins(new int[] {20, 30, 15, 0});
        WaveFormRenderer.setAxesColor(Color.YELLOW);

        String seriesTitle = "Input Data";
        XYSeries series = new XYSeries(seriesTitle);

        WFDataset.addSeries(series);
        WFCurrentSeries = series;

        XYSeriesRenderer renderer = new XYSeriesRenderer();
        renderer.setColor(Color.GREEN);
        WaveFormRenderer.addSeriesRenderer(renderer);
        WaveFormRenderer.setXAxisMin(0.0);
        WaveFormRenderer.setXAxisMax(500);


//      renderer.setFillBelowLine(true) ;
//      renderer.setFillBelowLineColor(Color.BLUE);
    }







    public void chartClick(View view) throws IOException{

                String strData = "";
                AssetManager amInput = context.getAssets();
                BufferedReader reader;
                InputStream is = null;
                try {
                    is = amInput.open("Dinamap-Data.txt");
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                reader = new BufferedReader(new InputStreamReader(is));

                try {
                    while( (strData = reader.readLine()) != null ) {


                        addBlock( strData ); //call to paint line

                    }
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }//while loop
                try {
                    reader.close();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }




   }//end mainClick() method for btnMain










    public void writeClick(View view){
        //write decimal data for wfs out to file from LinkedList<BinaryBlock>
        //using WfsToString() method of BinaryBlock class in separate thread
        (new Thread(new Runnable() {
            @Override
            public void run() {
                FileOutputStream fos = null;
                try {
                    fos = openFileOutput("wfs.txt", Context.MODE_WORLD_READABLE);
                } catch (FileNotFoundException e1) {
                    e1.printStackTrace();
                }
                ListIterator<BinaryBlockLite> itr = list.listIterator();
                while (itr.hasNext()){
                    String temp = itr.next().WfsToString();
                    try {
                        fos.write(temp.getBytes());
                    } catch (IOException e) {
                        e.printStackTrace();
                    }
                }
                try {
                    fos.close();
                } catch (IOException e) {
                    e.printStackTrace();
                }

            }
        })).start();
        btnMain.setEnabled(false);
    }












    private void addBlock(final String strData) {
        new Thread(new Runnable() { 
          public void run() { 


                int wfs[];

                //read line into binaryBlockLite object and store object in Linked List
                BinaryBlockLite bb = new BinaryBlockLite(strData);
                list.add(bb);

                //get the wfs data from the BinaryBlockLite object
                wfs = new int[bb.getWfs().length];
                wfs = bb.getWfs();

                //grab each wfs individually and paint to the chart, and increment xTick
                for (int k = 0; k < wfs.length; ++k){
                    /* check if we need to shift the x axis */
                    if (xTick > WaveFormRenderer.getXAxisMax()) {
                        WaveFormRenderer.setXAxisMax(xTick);
                        WaveFormRenderer.setXAxisMin(++lastMinX);
                    }

                    if (wfs[k] > 1000){
                        WFCurrentSeries.add(xTick, WFCurrentSeries.getY(xTick - 1));
                    }   
                    else{
                        WFCurrentSeries.add(xTick, wfs[k]);
                    }   
                    ++xTick;    
                }
                WFChartView.repaint();

            } 
        }).start(); 

    }















    @Override
    protected void onResume() {

        super.onResume();
        if (WFChartView == null) {
            LinearLayout layout = (LinearLayout) findViewById(R.id.WFchart);
            WFChartView = ChartFactory.getLineChartView(this, WFDataset, WaveFormRenderer);

            layout.addView(WFChartView, new LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
//          boolean enabled = WFDataset.getSeriesCount() > 0;
//          setSeriesEnabled(enabled);
          } else {
              WFChartView.repaint();
          }
    }


    @Override
    public boolean onCreateOptionsMenu(Menu menu) {
        getMenuInflater().inflate(R.menu.activity_main, menu);
        return true;
    }
}

I think it's either super simple or a threading issue which is something that I haven't played with enough. Any help would be appreciated.

4

1 回答 1

0

查看代码,我看到您将所有数据添加到系列中,然后调用repaint.

如果您希望数据逐渐添加,则将循环内部移动repaintfor放入Thread.sleep()内部,以让 UI 线程有机会实际执行repaint.

于 2012-11-12T17:12:47.417 回答