[UPDATE] To complete this question, I completed my schedule using the following two methods (see below). drawCurve()
gets a drawCurve()
and an array of float
. The array is correctly filled (timestamps are accepted by the index of values ββin the array) and range from 0.0 to 1.0. The array is sent to prepareWindowArray()
, which takes a piece of the array from the windowStart
position for windowSize
values ββin a circular manner.
The array used by GraphView and the data provider (Bluetooth device) is the same. The middle class ensures that GraphView will not read data that is being written by the Bluetooth device. Since GraphView always cycles through the array and redraws it at each iteration, it is updated according to the data recorded by the Bluetooth device, and forcing the recording speed of the Bluetooth device to update the graph frequency, I get a smooth animation of my signal.
The GraphView
invalidate()
method is called by Activity
, which runs Timer
to update the graph every x
milliseconds. The refresh rate is updated dynamically, so it adapts to the data stream from the Bluetooth device (which determine the frequency of its signal in the header of its packet).
Find the full code of my GraphView
in the answer I wrote below (in the answer section). If you guys find errors or a way to optimize them, let me know; that would be appreciated!
private void drawCurve(Canvas canvas, float[] data){
The prepareWindowArray()
method that implements the circular behavior of the array:
private float[] prepareWindowArray(float[] data){
[/ UPDATE]
I am making an application that reads data from a Bluetooth device with a fixed speed. Every time I have new data, I want them to be plotted on the graph on the right, and to translate the rest of the graph to the left in real time. In principle, like an oscilloscope.
So, I made my own view, with the xy axis, header and units. For this, I just draw these things on the canvas "View". Now I want to draw a curve. I managed to create a static curve from an already filled array using this method:
public void drawCurve(Canvas canvas){ int left = getPaddingLeft(); int bottom = getHeight()-getPaddingTop(); int middle = (bottom-10)/2 - 10; curvePaint = new Paint(); curvePaint.setColor(Color.GREEN); curvePaint.setStrokeWidth(1f); curvePaint.setDither(true); curvePaint.setStyle(Paint.Style.STROKE); curvePaint.setStrokeJoin(Paint.Join.ROUND); curvePaint.setStrokeCap(Paint.Cap.ROUND); curvePaint.setPathEffect(new CornerPathEffect(10) ); curvePaint.setAntiAlias(true); mCurve = new Path(); mCurve.moveTo(left, middle); for(int i = 0; i < mData[0].length; i++) mCurve.lineTo(left + ((float)mData[0][i] * 5), middle-((float)mData[1][i] * 20)); canvas.drawPath(mCurve, curvePaint); }
It gives me something like this.
There are still things on my chart (the tray doesnβt scale correctly), but these are details that I can fix later.
Now I want to change this static graph (which gets a non-dynamic matrix of values) with something dynamic that will redraw the curve every 40 ms, pushing the old data to the left and displaying the new data to the right, so I could visualize in real time the information provided by the Bluetooth device .
I know that there is already some kind of GUI package, but I do not do anything with it, and I would prefer to use this graph myself. In addition, most of my GraphView class runs, except for the part of the curve.
The second question, I wonder how I should send new values ββto the chart. Should I use something like a FIFO stack, or can I achieve what I want with a simple matrix of paired?
On the other hand, the 4 fields below are already dynamically updated. Well, they kind of pretend to be βdynamicβ, they go over and over again through the same double matrix, they actually don't take fresh values.
Thank you for your time! If something is unclear about my question, let me know and I will update it in more detail.