Android 计时器中的活动未发生Invalidate()
这是我在“活动”中的代码,用于使画布无效,而不是使画布无效。表示onDraw()没有被调用一次Android 计时器中的活动未发生Invalidate(),android,performance,android-layout,android-canvas,Android,Performance,Android Layout,Android Canvas,这是我在“活动”中的代码,用于使画布无效,而不是使画布无效。表示onDraw()没有被调用一次 public GraphView view; @Override public void onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentView(R.layout.main); view = GraphView(this,n
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();
}
});
}
在视图类上,这是从活动获取调用的方法。其他OnDraw声明与要求相同
public void InvalidateGraph()
{
m_bCalledPlotRealTimeGraph = true;
invalidate(chanX_count1, 0, chanX_count1+7, graphheight);
}
有什么帮助吗?您正试图更改
计时器
线程上的视图
,但该线程将不起作用。您需要在主(UI)线程上调用invalidate
:
你需要启动计时器
Timer t = new Timer();
//Set the schedule function and rate
t.scheduleAtFixedRate(new TimerTask() {
@Override
public void run() {
InvalidateTimer();
}
},1000,40);
t.start()
使用处理器代替计时器
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和onResult写入
if( mupdateTask == null )
mupdateTask = new UpdateHandler();
handler.removeCallbacks(mupdateTask);
使用
handler.postDelayed(mupdateTask, 100);
我没有完全理解。我的ChanX_count1在GraphView上。您所编写的代码是关于活动的。因此,我可以将ChanX_count1设置为静态,并将ChanX_count1设置为GraphView.ChanX_count1?@user2710860,然后您需要对
活动的引用。您可以使用图形视图
的上下文
来执行此操作。我会更新我的答案。但是使用GraphView引用它不起作用。我认为这是在创造一个新的对象,或者可能是别的东西。我不确定。有人能帮我吗?这太简单了,我通过我的self-view=(GraphView)findViewbyId(R.id.GraphModel);是的,我可以使用handler和postDelayed,但问题是:我的DrawFunction应该在40毫秒内被调用。所以我的问题是View类是否能够处理如此快的速度为什么它不会失效?
handler.postDelayed(mupdateTask, 100);