我从RelativeLayout创建了一个子类,当我调用invalidate方法时,他没有调用OnDraw mehtod,这是我的代码:
handler = new Handler(){
@Override
public void handleMessage(Message msg) {
if(msg.what == GameGlobals.UpdateDraw){
gameHandler.invalidate();
}
super.handleMessage(msg);
}
};
Button btt_start = (Button)findViewById(R.id.btt_start);
btt_start.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View arg0) {
setContentView(gameHandler);//The sub class from RelativeLayout
try {
gameHandler.setup(handler);
} catch (Exception e) {
showAlert(e.getMessage());
}
Thread thread = new Thread(gameHandler);
thread.run();
}
});
现在是子类:
//this method is called int the three constructors
private void setup(){
setWillNotDraw(false);
}
@Override//from Runnable
public void run() {
Message msg = new Message();
msg.what = GameGlobals.UpdateLogic;
handler.sendMessage(msg);
try {
Thread.sleep(10);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}
@Override
protected void onDraw(Canvas canvas) {
super.onDraw(canvas);
canvas.save();
canvas.drawText("testing..", 90, 90, textInfo);
canvas.restore();
}
好吧,就是这样,我调试了代码,我看到的是:onDraw方法只被调用两次:当它被创建时,然后第一次调用invalidate方法时,就是这样,他不要不断调用无效,这是一个线程!方法运行在那边,为什么他不再被调用?我会感谢你的帮助
答案 0 :(得分:0)
你的run()方法中没有循环..因此它会触发一次无效调用,睡眠10毫秒然后退出。