简体   繁体   中英

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;

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?

而是在一定时间后使用Android处理程序更新UI。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM