简体   繁体   English

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

[英]Android: How to show animated splash screen while loading resources

I want to show an animated image (currently using set of PNGs) as splash screen while resources are getting loaded. 我希望在加载资源时将动画图像(当前使用的一组PNG)显示为启动画面。

I have been able to show the splash screen using this . 我已经能够使用显示启动画面。 But the problem is: the splash screen shows for a specified time then there is a black screen for 2-3 seconds and then index.html gets loaded. 但问题是:启动画面显示指定的时间,然后有一个黑屏2-3秒,然后index.html加载。

Ideally what I want is to show the splash screen while resources get loaded. 理想情况下,我想要的是在资源加载时显示启动画面。 Once resources are loaded, pull the splash screen and immediately load the index.html file. 加载资源后,拉动启动屏幕并立即加载index.html文件。

Below is my code: 以下是我的代码:

@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.
    }
}

Please note my development environment. 请注意我的开发环境。 android-sdk: api level 17, Phonegap: 2.1.0. android-sdk:api level 17,Phonegap:2.1.0。

This can be solved with Phonegap/Cordova 1.7 version. 这可以通过Phonegap / Cordova 1.7版本解决。 As you are using 2.1 this solution will also be available for you. 在您使用2.1时,此解决方案也可供您使用。

In your main Phonegap activity class, initialize the splash screen and loadURL method: 在主要的Phonegap活动类中,初始化初始屏幕和loadURL方法:

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

10000 is a time in ms, so the splash img will be visible for the next 10 seconds. 10000是以ms为单位的时间,因此在接下来的10秒内将显示splash img。 You can even use 30-60. 你甚至可以使用30-60。 Later I will explain why. 稍后我会解释原因。

In order to have a splash screen in a PhoneGap Android application, you need to put your splash.png file into: 要在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)

Now the main thing is: in your Javascript add the deviceready event like this: 现在主要的是:在你的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();

So even if the splash screen is still visible, navigator.splashscreen.hide(); 因此即使启动画面仍然可见, navigator.splashscreen.hide(); is going to hide it and show the loaded html. 将隐藏它并显示已加载的html。

Hope this helps you. 希望这对你有所帮助。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM