Android App Graphview在运行UI线程中未更新

问题描述 投票:0回答:1

我正在尝试使用graphview在我的Anroid应用上绘制实时ADC数据。我正在尝试使用runOnUiThread在单独的线程中绘制数据,但是在图形和runOnUiThread上没有任何绘制被重复调用。

如何连续运行graphview?如果我在while循环中调用runOnUiThread,则该应用会挂起。

下面是我的代码

UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() { //Defining a Callback which triggers whenever data is read.


        @Override
        public void onReceivedData(byte[] arg0) {

// lowbyte = arg0;
            // highbyte = arg0;
            byte[] buffer = arg0;
            for (int i = 0; i <= (buffer.length - 1); i++) {
                if (buffer[i] != 13) {
                    if (buffer[i] == 10) {
                        finaldata = rawdata;
                        rawdata = "";
                    } else {
                        chdata = (char) buffer[i];
                        rawdata += chdata;
                    }
                }

            }

            data = Integer.parseInt(finaldata);

            control_a = 1;
            buffer_bt[databuff] = data;
            // }
            //lowbyte = buffer;

            //highbyte = buffer;

            databuff = databuff + 1;


            if (databuff == 767){

            databuff=0;
        }           

        }
    };

 @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        this.setRequestedOrientation(ActivityInfo.SCREEN_ORIENTATION_LANDSCAPE);
        requestWindowFeature(Window.FEATURE_NO_TITLE);//Hide title
        this.getWindow().setFlags(WindowManager.LayoutParams.
                FLAG_FULLSCREEN,WindowManager.LayoutParams.FLAG_FULLSCREEN);//Hide Status bar
        setContentView(R.layout.activity_main);
        //set background color
        LinearLayout background = (LinearLayout)findViewById(R.id.bg);
        background.setBackgroundColor(Color.BLACK);
        mHandler = new Handler();
        //GraphView = (LinearLayout) findViewById(R.id.Graph);
        usbManager = (UsbManager) getSystemService(this.USB_SERVICE);
        connectionstatus = (TextView) findViewById(R.id.tvBluetooth);
        bConnect = (Button)findViewById(R.id.bConnect);
        bConnect.setOnClickListener(this);
        bDisconnect = (Button)findViewById(R.id.bDisconnect);
        bDisconnect.setOnClickListener(this);
        //X-axis control button
        bXminus = (Button)findViewById(R.id.bXminus);
        bXminus.setOnClickListener(this);
        bXplus = (Button)findViewById(R.id.bXplus);
        bXplus.setOnClickListener(this);
        //
        tbLock = (ToggleButton)findViewById(R.id.tbLock);
        tbLock.setOnClickListener(this);
        tbScroll = (ToggleButton)findViewById(R.id.tbScroll);
        tbScroll.setOnClickListener(this);
        tbStream = (ToggleButton)findViewById(R.id.tbStream);
        tbStream.setOnClickListener(this);
        //init toggleButton
        Lock=true;
        AutoScrollX=true;
        Stream=true;
        IntentFilter filter = new IntentFilter();
        filter.addAction(ACTION_USB_PERMISSION);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_ATTACHED);
        filter.addAction(UsbManager.ACTION_USB_DEVICE_DETACHED);
        registerReceiver(broadcastReceiver, filter);
         init();
        Thread thread = new Thread(null, commRunnable, "tag");
            thread.start();

    }

 Runnable commRunnable = new Runnable(){

        @Override
        public void run() {

         plotdata = buffer_bt[i];
            i = i + 1;
            if (i == 767) {
                i = 0;
            }


            if (tbStream.isChecked()) {
                    // plot data
                runOnUiThread(new Runnable() {
                    @Override
                    public void run() {

                        Series.appendData(new GraphViewData(graph2LastXValue, plotdata), AutoScrollX);
                        if (graph2LastXValue >= Xview && Lock == true) {
                            Series.resetData(new GraphViewData[]{});
                            graph2LastXValue = 0;
                        } else graph2LastXValue += 0.4;

                        if (Lock == true)
                            graphView.setViewPort(0, Xview);
                        else
                            graphView.setViewPort(graph2LastXValue - Xview, Xview);

                        //refresh
                        GraphView.removeView(graphView);
                        GraphView.addView(graphView);

                    }


                });

            }

        }
    };
android android-graphview
1个回答
0
投票

我想我需要一个处理程序来将串行数据传递到UI线程。但是如何使用呢?

UsbSerialInterface.UsbReadCallback mCallback = new UsbSerialInterface.UsbReadCallback() { 

//Defining a Callback which triggers whenever data is read.


        @Override
        public void onReceivedData(byte[] arg0) {
// lowbyte = arg0;
            // highbyte = arg0;
            byte[] buffer = arg0;
            for (int i = 0; i <= (buffer.length - 1); i++) {
                if (buffer[i] != 13) {
                    if (buffer[i] == 10) {
                        finaldata = rawdata;
                        rawdata = "";
                    } else {
                        chdata = (char) buffer[i];
                        rawdata += chdata;
                    }
                }
            }

            data = Integer.parseInt(finaldata);
//I need a handler here. But how to use it in my code?
            control_a = 1;
            buffer_bt[databuff] = data;
            // }
            //lowbyte = buffer;

            //highbyte = buffer;

            databuff = databuff + 1;


            if (databuff == 767){

            databuff=0;
        }           

        }
    };


© www.soinside.com 2019 - 2024. All rights reserved.