现在的位置: 首页 > 综合 > 正文

Swing 闪屏加载和延迟技巧

2018年03月23日 ⁄ 综合 ⁄ 共 2782字 ⁄ 字号 评论关闭

1   引言

我们经常在启动一些程序的时候,会出现一个屏保的闪屏,这个闪屏是一副关于该程序的Logo图片,比如eclipse等等,有的时候并且伴有一些启动信息;

 

2   limeWire的闪屏

闪屏的实现代码如下:

 

SplashScreen splashScreen = SplashScreen.getSplashScreen();

            Frame splash = null;

            Image splashImage = null; // begin with null, assuming we can get it from SplashScreen

 

            // show initial splash screen only if there are no arguments

            if (args == null || args.length == 0) {

                Rectangle bounds;

               

                // If the splash screen existed & was visible,

                // then we want to take the image of the splash screen

                // and the bounds it was displayed at, so that

                // when we convert to an AWT image, the same image

                // is shown at the same place.

                if(splashScreen != null && splashScreen.isVisible()) {

                    bounds = splashScreen.getBounds();

                    splashImage = Toolkit.getDefaultToolkit().createImage(splashScreen.getImageURL());   

                }

 

闪屏的显示和关闭:

 

if(splashImage != null) {

                    splash = AWTSplashWindow.splash(splashImage, bounds);

                } else if (splashScreen != null && splashScreen.isVisible()) {

                    // If we couldn't find an image to show via AWT, then just close

                    // the existing splash and be done with it.

                    splashScreen.close();                   

                }

获取图片:

private static Image getSplashImage() {

        URL imageURL = ClassLoader.getSystemResource("org/limewire/ui/swing/mainframe/resources/splash.png");

        if (imageURL != null) {

            return Toolkit.getDefaultToolkit().createImage(imageURL);

        } else {

            return null;

        }

}

 

3   给“他”展示的时间

闪屏启动以后,我们必须给出这个闪屏一直在前段,以便我们后端能够加载程序;

limeWire中,故意的采用了反射来加载程序,以便能够故意的延迟时间给闪屏:

 

Class<?> loadClass = Class.forName("org.limewire.ui.swing.GuiLoader");

            Object loadInstance = loadClass.newInstance();

            Method loadMethod = loadClass.getMethod("load", new Class[] { String[].class, Frame.class, Image.class } );

            loadMethod.invoke(loadInstance, args, splash, splashImage);

 

在程序加载成功需要显示下一个GUI界面的时候,会有新的线程来关闭该闪屏;

显示新的对话框;

SwingUtils.invokeNowOrWait(new Runnable() {

                @Override

                public void run() {

                    if (awtSplash != null) {

                        awtSplash.setVisible(false);

                    }

                    //must warn users about sharing documents again after an upgrade

                    SharingSettings.WARN_SHARING_DOCUMENTS_WITH_WORLD.setValue(true);

                    boolean confirmed =

                        new IntentDialog(LimeWireUtils.getLimeWireVersion(),

                                virusEngine.get()).confirmLegal();

                    if (!confirmed) {

                        System.exit(0);

                    }

                   

                    if (awtSplash != null) {

                        awtSplash.setVisible(true);

                    }

                }

            });

抱歉!评论已关闭.