AChartengine: 1. repaint(), 2. limit data points - android

1:
I use achartengine to draw a line chart. Since i receive constantly data i want the chart scroll automatically to the right. My Problem is now, that the chart only scrolls when i touch the display of my phone(Android 2.3). Chart is running in extra thread and gets repaint() every ~100 ms.
2: How do i limit the collected data points. Is there an option to save the last 100 points and delete the older values? Currently my app saves all data points and gets slower and slower.
Best regards.

You can dynamically set the visible chart area using: renderer.setXAxisMin() and renderer.setXAxisMax(). Call repaint() after calling these methods.
Using the same APIs as the above you can control which data to be displayed. It's up to you to remove data from the series after that.

Related

MPAndroidChart - animations between real-time data points

I am currently using MPAndroidChart to display real-time data. I have been able to add points to a line chart and update a radar chart with the data. However, I am having issues with implementing smooth animations when updating the charts.
So, for example, instead of just redrawing the line chart to include a new point of data, I would like the line to gradually extend to a new data point, sliding to the right if an old data point is being replaced (when outside of the visible axis range). And in the radar chart, if a point of data changes from, say, 20 to 60, I would like the point on the chart to smoothly move from the 20 to 60 instead of the chart being completely redrawn.
At the moment, I have found the animateX, animateY, and animateXY methods in the library. However, these only seem to be for initially creating the chart. I need there to be animations while data is continually added to an existing chart. Is it possible with this library? What kinds of tweaks would I need to make?

Displaying Annotations on Map on viewing region only with Titanium SDK

I have locally stored about 1200 anotation data in an array. Now I want to add/set only annotation to region that is presently viewing. When user updates the region this will add/set new annotations. I know we can implement this in regionChanged method. I want this to be loading fast. Please suggest me how can we implement this that will be efficient. Also please suggest me which will be fast enough Loading annotation in single time with
mapview.setAnnotations(annotationarray);
or showing as region wise. The default zoom level of application is as this will have only 2-4 annotation at a time. sorry for multiple question in single time.
With your current region and the latitudeDelta and longitudeDelta you can deduce what your current map bounding box is (that's the coordinates of the 4 corners of your current mapview).
You should check if the coordinates of each of your pins is within the bounding box and then if it is inside save it in a separate array. Once you finished the check add that array of annotations to the mapview.
Each time the region changes you should repeat this operation.
I don't know how efficient this solution is, but iterating through an array of 1200 items is not going to be inexpensive.
If this solution is very slow, you could also save the coordinates in a sqlite database and execute a query each time.

Help me optimize this graph animation

I need some help with this simple animation on my Android phone. I'm a newbie with animation and graphics.
I'm graphing accelerometer data as a sliding time series window. As new accelerometer data is read, its data is plotted on the right, pushing previous data to the left, as shown below:
My program is running pretty smoothly, but I would like some help optimizing the animation. Here are my main concerns:
My current implementation reads all the accelerometer data in one thread and keeps the data in a fixed-size FIFO queue to capture the width of the time series window. I then use Timer.scheduleAtFixedRate() to plot out the entire contents of the queue so that the whole graph is re-drawn every 50 milliseconds. Can I improve upon this? Do I really need to re-draw the graph so often like this? In another similar program I've seen, each pixel column is copied to one pixel to the left, rippling down the graph; the newest data's column is drawn on the far-right pixel column. Is this better?
I redraw the legend (in the upper left) in the drawing thread that runs the draw function every 50 milliseconds. Is there any way to "keep" that legend in place instead of having to constantly re-draw it?
Any other help would be appreciated. I have heard of optimizations like double-buffering but am clueless if that would help me.
If the legend and the cross hairs only need to be drawn once, then you should place it into a buffer bitmap. For your graph line maybe try using the Path object to plot the lines. When it gets time to draw the lines just drawLine to the appropriate point and then translate the canvas left appropriately.If

Design decisions / advice for a simple game on Android

I need your advice on the best way to implement a scrollable playing field in a simple game. The field consists of multiple rows, with 9 cells in each row. The rows can be dynamically added and removed during the course of the game. Each cell has a digit in it, and optionally several overlayed images, like selection, selector over, crossed out, etc. (see the pic below). The user should be able to click on individual cells of a row to select/deselect/cross the touched cells. At any point in the game there can be from 0 to 3000 cells (0 to about 333 rows). The field should scroll up and down smoothly.
I was thinking about having a ListView with its each row being a row on the field. That way i could add/remove rows dynamically during the game. However, how exactly should i implement a row: have one bitmap representing a row, and then when the user touches it -- get the coordinates of the touch area, and find out what cell was affected, and then act upon that. Is it possible to get the touch coordinates of a row in ListView? If not, should I place 9 dummy image placeholders in each row, and then act on user touching those? What about performance?
Or should I have one huge bitmap / canvas representing the entire field, place it inside a ScrollView and then calculate all the coordinates and update it as the user interacts with it? Is it going to be faster or slower than the ListView?
Any other solutions?
I prefer it to be a "regular" app type game, not a loop-based game, as I don't think I really need to redraw 30 times a second.
I am pretty new to Android. Thank you for your suggestions.
You can easily make that kind of setup run quickly with a "game loop" / SurfaceView combination. That means no ListView, only custom drawing and event handling. Luckily the event handling isn't that difficult, and you'll win later on because you'll have much greater control over the interface than if you had gone with a bunch of customized views and layouts.
I'd avoid that www.droidnova.com tutorial, it uses HashMaps unnecessarily, which will only cost you in performance.
Here's how I'd go about it:
Create an object to hold your cell data. For this example, I'd use an object with an id (to print), and an x and y for where to draw on the screen.
Decide on a board size which will fit on your screen without scrolling, say 10x10. You'll add the scrolling later.
Create a 2-dimensional array of cell objects with lengths boardSize x boardSize. Fill the objects with id and x and y position on the screen.
In your custom onDraw, iterate through each "row" and "column" of your single array and draw the object at its stored x and y value.
So now you've got a grid of objects displaying on your screen. Now you want to restrict the number of rows currently displayed and add some functionality to change which rows are visible. This is done as follows:
During initialization, set up some global ints as mCurrentRow = 0 and mNumVisibleRows = 3. These define your "view window".
Modify your drawing code to only draw rows starting at mCurrentRow and ending at mCurrentRow + mNumVisibleRows. The result of this should be that you only ever see mNumVisibleRows rows, and which group of rows you see depends on what you set mCurrentRow to.
Add some triangles to the right of your grid drawing and have tap touch events in those areas map to increments/decrements of mCurrentRow. Obviously, you should not allow that value to go outside your row count bounds.
If you want to get classy, draw a line between your triangles for a scroll area, and map touch events there to something like newCurrentRow = (touch.y / canvas.height()) * boardSize; That needs some tweaking, but you get the idea.
This approach has the downside of only showing a full set of rows at a time, so scrolling wouldn't be smooth. However, you have complete control over your canvas and what you draw, so with a little more math you could implement a smooth scrolling mechanism which would offset by a fractional row height in the y-direction instead of whole rows.
I dont know if you can create a game like you described with good performance. I would look into basic tile game programming.
But by avoiding the standard view components are have to write all the logic yourself requires quite some work. Things like handling "click" events on different rows needs to be calculated by the tile position relative to the game camera. So theres alot of new stuff in learning to develop game at a lower level.
You also have to take the rendering of your game into your own hands by developing a game loop that constantly updates and draw's your tiles from a background thread to reflect the scroll / state of your game.
You can get more infomation on the basics of a game loop at:
http://www.rbgrn.net/content/54-getting-started-android-game-development
If you want to learn more you should see the following keynotes from android.
http://developer.android.com/videos/index.html#v=U4Bk5rmIpic
http://developer.android.com/videos/index.html#v=7-62tRHLcHk
Those give you a very good insight in developing games for andriod at a low level where you can fine tune for performance.

Horizontal scroll problem

I am relatively new to Android. I am working on an application wherein
i want to display digital signals. But the problem is once I occupy
the available screen width how to i add a scrolling feature to
continue viewing the signal. Also i am drawing the signal using using
drawLine(), so what co-ordinates should i set for drawing the lines
when scrolling is enabled? Can somebody please give a simple example
where a line extends more than the available screen width and you can
scroll through to see the remaining line.
Thanx in advance.
I've written a few of these before in other languages, so I can ive you the principles, rather than an example. I'm assuming you have some sort of buttons to the left and right that allows the user to scroll through the data, and that your signal data is stored in an array. You will be able to determine how much data you can fit into one screen width based on whatever scaling factor you are using. Say your screen can display ten values at a time, then you simple store the starting point in your signal array and use that as the left most point to display on your screen. Then all you have to do is show the next ten values in your array. When the user selects the button to move left or right through the data simply increment or decrement the starting point. If you are streaming in your signal, then the stream can increment the starting point whenever you receive a new piece of data. A redraw after each start value change will give the impression of scrolling. Watch out for start and end conditions (i.e. you don't want to scroll until you have at least a full screen of data). All you are doing is creating a window onto the data you have.
Hope this helps.

Categories

Resources