`
ferreousbox
  • 浏览: 284627 次
  • 性别: Icon_minigender_1
  • 来自: 杭州
社区版块
存档分类
最新评论

SWT窗口特效之缓慢展开和消失

    博客分类:
  • java
阅读更多

    这个特效不知道取什么名字好,就叫缓慢展开或消失吧,整个效果就像是一张纸一样慢慢拉出来或缩进去。这个效果的实现也比较简单,主要是通过设置shell的size来实现的,通常我们会有四种效果,即从左到右慢慢展现和消失、从右到左慢慢展现和消失、从上到下慢慢展现和消失、从下到上慢慢展现和消失。下面分别说明如下:

 

1.从左到右慢慢展现

 

    这个效果实现的思路就是shell的location不变,刚开始设置shell的size的宽度为0,高度不变,然后逐步的将shell的width增加,从而就实现了shell的缓慢打开。代码如下:

 

final int width = shell.getSize().x;
final int startX = shell.getLocation().x;
new Thread(new Runnable() {
    private int startWidth = 0;
    public void run() {
        while (startWidth <= width) {
            shell.getDisplay().syncExec(new Runnable() {
                public void run() {
	    shell.setSize(++ startWidth, shell.getSize().y);
                }
            });
            Thread.yield(); // 暂停
        }
    }
}).start();

 

    注意,我们是将每一次修改的操作都当成一个Runnable来执行的,而不是把整个while循环放在一个Runnable中执行,这样是为了避免Display执行Runnable时无法处理其他响应事件,从而导致展开效果并不流畅!另外,即使使用asyncExec执行也是一样的效果!

 

 

2.从右到左慢慢展现

 

    从右到左的慢慢展现效果实现的思路和从左到右其实是一样的,只不过增加了一个同时设置shell的location的操作,这样我们才可以模拟从右边到左边慢慢打开的效果。实现代码如下:

final int width = shell.getSize().x;
final int startX = shell.getLocation().x;
new Thread(new Runnable() {
    private int startWidth = 0;
    public void run() {
        while (startWidth <= width) {
            shell.getDisplay().syncExec(new Runnable() {
                public void run() {
	    shell.setSize(++ startWidth, shell.getSize().y);
                    shell.setLocation(startX + width - startWidth, shell.getLocation().y);
                }
            });
            Thread.yield();
        }
    }
}).start();

 

 

3.从左到右慢慢消失

 

    慢慢消失的实现思路是逐步的改变shell的size,从左到右慢慢消失就是shell的高度不变,width从原始的宽度慢慢减小到0,同时还需要同步设置shell的location,以保证右边总是在同一个位置。代码如下:

final int shellWidth = shell.getSize().x;
final int shellStartX = shell.getLocation().x;
new Thread() {
    private int width = shellWidth;
    private int startX = shellStartX;
    public void run() {
        while (width > 0) {
            shell.getDisplay().syncExec(new Runnable() {
                public void run() {
                    shell.setSize(-- width, shell.getSize().y);
                    shell.setLocation(++ startX, shell.getLocation().y);
                }
            });
            Thread.yield();
        }
    }
}.start();

  

     在消失完毕后,你还可以选择将shell关闭或是重新恢复到以前的大小和位置,那么就需要记录shell开始的size和location了。

 

 

4.从右到左慢慢消失

 

    从右到左慢慢消失相比从左到右慢慢消失只是少了一个重新设置shell的location的操作,其代码如下: 

final int shellWidth = shell.getSize().x;
final int shellStartX = shell.getLocation().x;
new Thread() {
    private int width = shellWidth;
    private int startX = shellStartX;
    public void run() {
        while (width > 0) {
            shell.getDisplay().syncExec(new Runnable() {
                public void run() {
                    shell.setSize(-- width, shell.getSize().y);
                }
            });
            Thread.yield();
        }
    }
}.start();

  

 

5.从上到下慢慢显示

 

    实现思路就是shell的location不变,shell的width不变,将shell的height从0慢慢增加到shell的实际高度即可。代码也比较简单,就不重复写了,留给大家自己写吧,呵呵。

 

 

6.从上到下慢慢消失

 

    实现思路就是shell的width不变,逐步的将shell的height从原始高度变到0,同时height每减少1,则将其location的y增加1,从而保证shell的底部总是在同一位置,而顶部则是在逐步的下移,从而实现效果,代码略。

 

 

7.从下到上慢慢显示

 

    实现思路就是shell的width不变,将高度逐步的由0变成原始的高度,同时也需要同步改变shell的location,高度每增加1,则shell的location的y则减1.代码略。

 

 

8.从下到上慢慢消失

 

    实现思路就是shell的width不变,将height由原始的高度慢慢减小到0,其location不变,代码略。

 

 

    以上就是对shell的size和location的改变来实现慢慢消失或显示的效果,这也是一个很简单的窗口特效了。后面将继续给大家介绍SWT窗口的其他特效,如百叶窗的打开和关闭特效、圆形消失和显示特效等等:-)

1
0
分享到:
评论
4 楼 ae6623 2014-08-20  
高大上!!



package book.ch2;

import org.eclipse.swt.SWT;
import org.eclipse.swt.SWTError;
import org.eclipse.swt.browser.Browser;
import org.eclipse.swt.events.ShellEvent;
import org.eclipse.swt.events.ShellListener;
import org.eclipse.swt.graphics.Point;
import org.eclipse.swt.layout.GridData;
import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Event;
import org.eclipse.swt.widgets.Label;
import org.eclipse.swt.widgets.Listener;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Text;

public class HelloTextShell2 {

	/**
	 * @param args
	 */
	public static void main(String args[]) {

		Display display = new Display();
		final Shell shell = new Shell(display);

		shell.setText("SWT Browser Test");
		shell.setSize(500, 500);
		final int width = shell.getSize().x;
		final int startX = shell.getLocation().x;
		new Thread(new Runnable() {

			private int startWidth = 0;

			public void run() {
				while (startWidth <= width) {
					shell.getDisplay().syncExec(new Runnable() {
						public void run() {
							shell.setSize(++startWidth, shell.getSize().y);
						}
					});
					Thread.yield(); // 暂停
				}
			}
		}).start();

		shell.open();

		while (!shell.isDisposed()) {
			if (!display.readAndDispatch())
				display.sleep();
		}
		display.dispose();
	}
}


3 楼 liuwenfeng554 2012-11-05  
如果从中间 向周边扩展 该怎么实现 
2 楼 ferreousbox 2009-07-07  
blueocean 写道
Eclipse里面的那个Java editor界面最大化的时候那个特效如何关闭啊, 看着我就头痛.

在"window->Preferences->General->Appearance"中关闭"Enable animations"选项即可!
1 楼 blueocean 2009-07-07  
Eclipse里面的那个Java editor界面最大化的时候那个特效如何关闭啊, 看着我就头痛.

相关推荐

Global site tag (gtag.js) - Google Analytics