开发者

timertask doesn't work!

i have tried using a timertask to refresh a line(after every 1 second) drawn with fixed point coordinates. But the line doesn't appear to refresh...is there anything wrong in my code?

LineRefresh.java:

package LineRefresh.xyz.com;
import android.app.Activity;
import android.graphics.Color;
import android.os.Bundle;

public class LineRefresh extends Activity {
DrawView drawView;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    drawView = new DrawView(this);
    drawView.setBackgroundColor(Color.WHITE);
    setContentView(drawView);

}
}

DrawView.java:

package LineRefresh.xyz.com;
import java.util.Timer;
import java.util.TimerTask;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.view.View;
开发者_Go百科
public class DrawView extends View {
Paint paint = new Paint();

public DrawView(Context context) {
    super(context);    
}

@Override
public void onDraw(final Canvas canvas) { 
    paint.setColor(Color.BLACK);
    canvas.drawLine(50, 200, 270, 200, paint);  

    Timer timer = new Timer();
    TimerTask task = new TimerTask() {
        @Override
        public void run() {
            paint.setColor(Color.BLACK);
            canvas.drawLine(50, 200, 270, 200, paint);
            }
    };
    timer.schedule(task, 1000,1000);
}

}

sholud i,instead, place the timertask somewhere else in my code?


Rather use Android Handler to update UI after certain time.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜