I am developing an android application which gets graph values over tcp and draws the graph realtime and continuously.The application has to draw 100 pixels/values per second, finishing a 1000 pixel width graph in 10 seconds.
I am developing on a Samsung Galaxy Tab 10.1 Tablet.
Below is the main activity code.I just pasted necessary parts.
public class MainActivity extends Activity {
private MyGraph graph;
private Handler mHandler;
private Handler mHandler2;
private boolean running;
public static int counter=1;
private int limit=1000;
private class MyGraph extends View {
private Paint paintecg = new Paint();
private Paint paintdel = new Paint();
private Canvas canvas = new Canvas();
private Bitmap cache = Bitmap.createBitmap(1000,800, Bitmap.Config.ARGB_8888);
private float nextX; //next point in x axis
private float lastX=0;
private float nextY; // next point in y axis
private float lastY=150;
public MyGraph(Context context){
super(context);
paintecg.setStyle(Paint.Style.STROKE);
paintecg.setColor(Color.GREEN);
paintecg.setAntiAlias(true); paintecg.setStrokeWidth(2f);
paintdel.setStyle(Paint.Style.FILL_AND_STROKE);
paintdel.setColor(Color.BLACK);
}
public void onDraw(Canvas canvas) {
if (cache != null)
canvas.drawBitmap(cache, 0, 0, paintecg);
}
public void drawNext() {
canvas = new Canvas(cache);
nextX=lastX+1;
//adding new points to the graph
canvas.drawLine(lastX,valuearray_ecg[counter-1],nextX,valuearray_ecg[counter], paintecg);
//emptying next 25 pixels
canvas.drawRect(nextX, 0, nextX+25, 800, paintdel);
lastX=nextX;
if (nextXcounter++;
}
else {
counter=1;
lastX=0;
}
postInvalidate();
}
}
}
This is the handler created inside oncreate() method :
LinearLayout graphView=(LinearLayout) findViewById(R.id.layout_graph);
graph = new MyGraph(this);
mHandler = new Handler(new Handler.Callback() {
@Override
public boolean handleMessage(Message msg) {
graph.drawNext();
if (running)
mHandler.sendMessageDelayed(new Message(), 10);
return true;
}
});
graphView.addView(graph);
Graph is drawed inside a layout in main.xml
This way, when i set handler to 20 ms and my x-axis steps to 2 pixels, it draws 1000 pix in around 15 seconds.Weird thing is, if i lock and unlock the device while app is running, timing becomes normal and draws 1000 pixels in 10 seconds.
when i set handler's delay to 10 miliseconds and x-axis steps to 1 pixel, at first it draws 1000 pixels in 25+ seconds.After locking and unlocking it drops 20- seconds.
I see i'm probably doing it wrong.My question is, is there any way to draw a graph that fast using android's native canvas drawing? Or what is the best way to handle an app like this?
.
stackoverflow.comm
No comments:
Post a Comment