Android:如何在加载资源时显示动画闪屏

时间:2012-12-21 10:20:42

标签: javascript android html5 cordova

我希望在加载资源时将动画图像(当前使用PNG集)显示为闪屏。

我已经能够使用this显示启动画面。但问题是:启动画面显示指定的时间,然后有一个黑屏,持续2-3秒,然后加载index.html

理想情况下,我想要的是在资源加载时显示启动画面。加载资源后,拉动启动屏幕并立即加载index.html文件。

以下是我的代码:

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);

    // Splash screen view
    setContentView(R.layout.splash);

    // Start animating the image
    final ImageView splashImageView = (ImageView) findViewById(R.id.SplashImageView);
    splashImageView.setBackgroundResource(R.drawable.flag);
    final AnimationDrawable frameAnimation = (AnimationDrawable)splashImageView.getBackground();
    splashImageView.post(new Runnable(){
        @Override
        public void run() {
            frameAnimation.start();             
        }           
    });


    final SplashScreen sPlashScreen = this;   

    // The thread to wait for splash screen events
    mSplashThread =  new Thread(){
        @Override
        public void run(){
            try {
                synchronized(this){
                    // Wait given period of time or exit on touch
                    wait(10000);
                }
            } 
            catch(InterruptedException ex){                 
            }

            finish();

            // Run next activity
            Intent intent = new Intent();
            intent.setClass(sPlashScreen, SomeActivity.class);
            startActivity(intent);
            //stop();               
        }
    };      
    mSplashThread.start();      
} 

public class SomeActivity extends DroidGap {                
    @Override
    public void onCreate(Bundle savedInstanceState) {    
     //some code to copy database files.
    }
}

请注意我的开发环境。 android-sdk:api level 17,Phonegap:2.1.0。

1 个答案:

答案 0 :(得分:7)

这可以通过Phonegap / Cordova 1.7版本解决。当您使用2.1时,此解决方案也可供您使用。

在您的主要Phonegap活动类中,初始化初始屏幕和loadURL方法:

super.setIntegerProperty("splashscreen", R.drawable.splash);
super.loadUrl("file:///android_asset/www/index.html", 10000);

10000是以ms为单位的时间,因此在接下来的10秒内将显示splash img。你甚至可以使用30-60。稍后我会解释原因。

要在PhoneGap Android应用程序中设置启动画面,您需要将splash.png文件放入:

res/drawable-ldpi (small: at least 426 x 320)
res/drawable-mdpi (medium: at least 470 x 320)
res/drawable-hdpi (large: at least 640 x 480) 
res/drawable-xdpi (xlarge: at least 960 x 720)

现在主要的是:在你的Javascript中添加deviceready这样的事件:

document.addEventListener("deviceready", onDeviceReady, false);
function onDeviceReady() {
    // Version 1.7 use this method
    cordova.exec(null, null, "SplashScreen", "hide", []);
    // Version 1.8+ use this mehod
    navigator.splashscreen.hide();

因此,即使启动画面仍然可见,navigator.splashscreen.hide();也会隐藏它并显示加载的html。

希望这会对你有所帮助。