I am trying to perform basic task of rotating a canvas 20 times a second using timer but it doesn't seem to be working properly and its lagging. for example, if I rotate rectangle 0.3 degrees per 50 ms it should rotate 6 degree in on second, but that is not the case. It really slow in rotation. Here is my sample code:
//Code for update task
class UpdateTimeTask extends TimerTask {
public void run() {
hndView.post(new Runnable() {
public void run() {
hndView.invalidate(); //this code invalidates custom view that calls onDraw to draw rotated hand
}
});
}
}
//Code for onDraw method of custom view that needs to be update
@Override
protected void onDraw(Canvas canvas){
super.onDraw(canvas);
//ang is angle to rotate and inc is float value of 0.3 degree to be incremented
ang = ang + inc;
if (ang >= 360) ang = ang - 360;
canvas.rotate(ang, canvas.getWidth()/2, canvas.getHeight()/2);
canvas.drawRect((canvas.getWidth()/2 - 2), (canvas.getHeight()/2 - 125), (canvas.getWidth()/2 + 2), (canvas.getHeight()/2 + 10), mTextPaint);
canvas.restore();
}
//code to schedule task
Timer timer = new Timer();
UpdateTimeTask tt = new UpdateTimeTask();
timer.schedule(tt, 0, 50);
Can anyone please tell me what am I doing wrong here? Should I used different approach to perform this task? Because its hard to believe that you cannot have simple smooth rotation of rectangle 20 times in one second.