2

我有一MyComposite堂课,我想在其中设置大小变化的动画。
为此,我正在循环更改大小。
每次循环后,我都会调用layout().

不幸的是,Composite 不会在每次迭代后重新绘制,而是直接跳转到我的 Composite 的最终大小。

如何强制小部件在每次尺寸更改时重绘?

MyComposite 和动画



//start
new Animation().start(myComposite);

...

public MyComposite(Composite parent, int style, int bgcolor) {
        super(parent, style);
        this.setBackground(getDisplay().getSystemColor(bgcolor));       
    }

    @Override
    public Point computeSize(int wHint, int hHint, boolean changed) {
        return super.computeSize(width, height, changed);
    }


    class Animation{
        public void start(MyComposite composite){
            for(int i=0; i<1000; i++){
                composite.width++;
                composite.getParent().layout(true, true);
            }
        }
    }


我的复合

4

2 回答 2

5

重绘工作如下:

  • layout()标记强制重新定位所有复合子项。这将在下一次重绘时变得可见,这将在未来某个地方完成,当复合的屏幕区域将被重绘时
  • redraw()将小部件标记为无效。在下一次重绘系统操作时,该区域将被重新绘制。
  • update()强制所有未完成的 redraw() 请求现在完成。

所以问题是,我没有立即触发重绘请求。正确的动画函数如下所示:


//layout of the composite doesn't work
//composite.layout(true, true);

//layout of parent works
composite.getParent().layout(true, true);

//marks the composite's screen are as invalidates, which will force a 
composite.redraw(); redraw on next paint request 

//tells the application to do all outstanding paint requests immediately
composite.update(); 


于 2012-08-26T13:48:13.803 回答
0

我相信您的问题是所有内容都在单个显示线程上执行。因此,您的代码会快速调用 width++ 和 .layout,然后该调用结束,显示线程最终有机会实际执行 .layout。

我建议查看在自己的线程中运行的 java.util.Timer,然后使用 Display.getDefault().asyncExec 或 .syncExec 将这些事件排队回显示线程。

于 2012-08-25T22:49:12.683 回答