我目前正試圖讓我們的應用程序的UI變得輕量級,並且正在嘗試將重量級服務器調用移動到他們自己的線程中。然後,我使用Display.getDefault.asyncExec()
來更新小部件,它完美地工作。正確刷新小部件?
我想在服務器調用返回時向用戶顯示動畫。到那時,我正在使用另一個線程,它在另一個Display.getDefault.asyncExec()
中顯示動畫。這也有效......但以一種奇怪的方式。當我運行應用程序時,小部件會非常沉重地閃爍,並且有時屏幕不會更新。事情是,當我運行一個原型版本,即沒有主應用程序,只是一個裸露的SWT應用程序,它完美的作品。任何想法爲什麼發生這種情況
基本工作流程::
showAnimation = new Thread(new Runnable() {
@Override
public void run() {
while(true){
synchronized (this) {
try {
Thread.sleep(1000);
drawCircles(); // basically the animation, also called in a Display.getDefault().asyncExec()
}
catch (InterruptedException e)
{
// do nothing, because this thread is meant to be interrupted!
// and then, break out of the infinite loop
System.out.println("Interrupted");
break;
}
}
}
}
});
invokeLater = new Thread(new Runnable(){
@Override
public void run(){
// do the data base call here
System.out.println("Got data!");
if(showAnimation.isAlive())
{
showAnimation.interrupt();
}
}
});
,並在我看來 - >
showAnimation.start();
invokeLater.start();
我簡要地想知道我是否應該增加動畫線程的優先級,只是爲了看看是否可行。它沒有(也沒有意義增加線程的優先級,而不是縮小應用程序的瓶頸)。
任何想法?
P.S:本drawCircles()
代碼 - >
public void drawCircles(){
Display.getDefault().asyncExec(new Runnable() {
@Override
public void run(){
int radius = 100;
int maxX = radius ;
int maxY = radius ;
int direction=0;
int centerX = table.getBounds().width/2;
int centerY = table.getBounds().height/2;
System.out.println("Drawing");
Image image = new Image(Display.getDefault(),table.getBounds().width,table.getBounds().height);
GC gc = new GC(image);
gc.setAntialias(SWT.ON);
gc.setForeground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
//gc.drawLine(0, centerY, shell.getBounds().width, centerY);
//gc.drawLine(centerX, 0, centerX, shell.getBounds().height);
// 1st circle
gc.drawOval(centerX-radius/2, centerY-radius - radius/2, maxX, maxY);
// 2nd circle
gc.drawOval(centerX - 2*radius + radius/2, centerY - radius/2, maxX, maxY);
// 3rd circle
gc.drawOval(centerX+radius/2, centerY-radius/2, maxX, maxY);
// 4th circle
gc.drawOval(centerX-radius/2, centerY + radius/2, maxX, maxY);
direction++;
direction %= 4;
switch(direction){
case 0:
gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
gc.fillOval(centerX - 2*radius + radius/2, centerY - radius/2, maxX, maxY);
break;
case 1:
gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
gc.fillOval(centerX-radius/2, centerY-radius - radius/2, maxX, maxY);
break;
case 2:
gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
gc.fillOval(centerX+radius/2, centerY-radius/2, maxX, maxY);
break;
case 3 :
gc.setBackground(Display.getDefault().getSystemColor(SWT.COLOR_BLACK));
gc.fillOval(centerX-radius/2, centerY + radius/2, maxX, maxY);
break;
}
table.setBackgroundImage(image);
table.redraw();
table.update();
image.dispose();
gc.dispose();
}
});
}
當你說閃爍,這聽起來像動畫直接完成屏幕無緩衝。向我們展示您的動畫代碼。也許drawCircles? – anonymous
@anonymous:updated –
它每次調用drawCircles時創建一個新的線程。這不是很好,但我不認爲這是閃爍的原因。 DrawCircles似乎沒有采用任何緩衝。也許這可能對你有幫助? http://stackoverflow.com/questions/17783525/triple-buffer-heavy-flickering。 – anonymous