这是我的代码,用于使无效的画布无效。意味着onDraw()甚至不会被调用一次;
public GraphView view;
@Override
public void onCreate(Bundle savedInstanceState)
{
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
view = GraphView(this,null);
runplotTimer();
}
public void runplotTimer()
{
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
InvalidateTimer();
}
},1000,40);
}
public void InvalidateTimer()
{
this.runOnUiThread(new Runnable() {
@Override
public void run()
{
//Log.d(ALARM_SERVICE, "Timer of 40 miliseconds");
view.InvalidateGraph();
}
});
}
在View类上,这是从Activity调用gettting的方法。其他OnDraw声明与所需的相同。
public void InvalidateGraph()
{
m_bCalledPlotRealTimeGraph = true;
invalidate(chanX_count1, 0, chanX_count1+7, graphheight);
}
请帮忙吗?
答案 0 :(得分:0)
您正尝试对View
主题上的Timer
进行更改,但这不会有效。您需要在主(UI)线程上调用invalidate
:
((Activity) view.getContext()).runOnUiThread(new Runnable() {
@Override
public void run() {
invalidate(chanX_count1, 0, chanX_count1+7, graphheight);
}
});
答案 1 :(得分:0)
你需要启动计时器
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
InvalidateTimer();
}
},1000,40);
t.start()
而不是Timer使用Handler。
class UpdateHandler implements Runnable {
@Override
public void run(){
handler.sendEmptyMessageAtTime(0, 1000);
handler.postDelayed(this, 1000);
}
}
private Handler handler = new Handler(Looper.getMainLooper()) {
@Override
public void handleMessage(Message msg) {
super.handleMessage(msg);
//Call your draw method
}
}
};
在onCreate和onResule内写
if( mupdateTask == null )
mupdateTask = new UpdateHandler();
handler.removeCallbacks(mupdateTask);
使用
调用您的处理程序handler.postDelayed(mupdateTask, 100);