Java Swt - 调整动画大小。如何在每次布局更改后重绘?

时间:2012-08-25 19:05:48

标签: java swt

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

不幸的是,复合材料在每次迭代后都没有重新绘制,但会直接跳到我的Composite的最终尺寸。

如何强制Widget在每次更改尺寸时重绘?

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);
            }
        }
    }


MyComposite

2 个答案:

答案 0 :(得分:4)

重绘工作原理如下:

  • 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(); 


答案 1 :(得分:0)

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

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

相关问题