Splash屏幕之前的黑屏出现在android中

时间:2013-01-13 19:14:08

标签: android xamarin.android splash-screen

我们知道,当应用程序执行一些漫长的过程,例如从互联网上下载某些信息时,它可能会在加载应用程序之前显示启动画面,当应用程序完全加载时,它将显示主页面。 在启动画面活动中,我们必须在线程中加载长进程,以避免在加载应用程序之前显示黑屏。 我做了所有这些。但在显示应用程序之前还会出现黑屏。 这是启动画面活动的onCreate方法:

    protected override void OnCreate (Bundle bundle)
    {
        try {
            base.OnCreate (bundle);
            //_dt = DateTime.Now.AddSeconds (_splashTime);
            SetContentView (Resource.Layout.Splash );
            FirstLoadPB= FindViewById <ProgressBar >(Resource .Id.FirstLoadPB );
            FirstLoadingInfo= FindViewById <TextView >(Resource .Id.FirstLoadInfo );
            LoadApplication ();

        } catch (System.Exception ex) {

            Common.HandleException (ex);
        }
    }

这是LoadApplication方法的代码:

public void LoadApplication()
    {
        new System.Threading.Thread (new ThreadStart (() =>
                                                      {
        //Some Codes to load applications- Downloading from web and accessing the storage(Because was many codes - about 100 line- i was clear them.

        }
        )
                                     ).Start ();
    }

我不明白为什么会出现黑屏,现在应该如何避免。 我有一些代码可以在我的应用程序类的oncreate中访问存储。也许问题的根本原因就在那里。因此,我分享了它的代码:

public override void OnCreate ()
    {
        try {
            base.OnCreate ();
            _typeOfShow = new MapViewType ();
            ListingTypes = new Dictionary<int,ListingTypeItem> ();

            OfflineMode =false;
            PropertyShowWasShown = false;
            MeasutingUnitsChanged =false;
            if(RplXmlSettings .Instance .getVal (AppConstants .XmlSettingShowOnCurrentLocationKey  )== "True")
                typeOfShow .ShowOnCurrentLocation =true ;
            else
                typeOfShow .ShowOnCurrentLocation =false;
            //StorageClass .ctx = ApplicationContext ;
            FillDashboardOnResume =false;
            //initlize image loader 
            ImageLoader = Com.Nostra13.Universalimageloader.Core.ImageLoader.Instance;
            Options = new DisplayImageOptions.Builder ()
                .ShowImageForEmptyUri (Resource.Drawable.ic_tab_map)
                    .CacheOnDisc ()
                    .CacheInMemory ()
                    .ImageScaleType (ImageScaleType.InSampleInt)
                    .BitmapConfig (Bitmap.Config.Rgb565)
                    .Displayer (new FadeInBitmapDisplayer (300))
                    .Build ();
            ImageLoaderConfiguration config;

            ImageLoaderConfiguration .Builder builder =new ImageLoaderConfiguration
                .Builder (ApplicationContext).ThreadPoolSize (3);

            if(RplXmlSettings .Instance .getVal (AppConstants .XmlSettingMemoryCacheKey )== "True")
                builder .ThreadPriority (4).MemoryCacheSize (1500000) ;// 1.5 Mb

            builder .
                DenyCacheImageMultipleSizesInMemory ().
                    DiscCacheFileNameGenerator (new Md5FileNameGenerator ()).
                    MemoryCache (new WeakMemoryCache()).
                    DiscCacheSize (15000000);
            config = builder .Build ();
            ImageLoader.Init (config);

        } catch (Exception ex) {
            Common .HandleException (ex);
        }

    }

好。长话短说。现在问题是这个 - 真的是这个黑屏的根本原因是什么。这是来自启动活动还是来自应用程序类。我们如何解决它并避免表现出来?

6 个答案:

答案 0 :(得分:63)

将包含您正在使用的背景的主题添加到清单文件中的应用程序标记,以防止绘制黑屏。

theme.xml

<resources>
<!-- Base application theme is the default theme. -->
<style name="Theme" parent="android:style/Theme" />

<style name="Theme.MyAppTheme" parent="Theme">
    <item name="android:windowNoTitle">true</item>
    <item name="android:windowContentOverlay">@null</item>
    <item name="android:windowBackground">@drawable/my_app_background</item>

</style>
</resources>

的AndroidManifest.xml

....
<application
        android:name="@string/app_name"
        android:icon="@drawable/ic_launcher"
        android:label="@string/app_name"
        android:theme="@style/Theme.MyAppTheme"
         >
....

Read why there is a black screen here On app launch, Android displays a simple preview window (based on your activity theme) as an immediate response to the user action. Then the preview window crossfades with your actual UI, once that has fully loaded. To ensure a smooth visual transition, your activity theme should match your full UI as closely as possible. The below image shows how the experience can be jarring if not handled properly.

答案 1 :(得分:48)

您看到的初始屏幕称为“预览”屏幕。您可以通过在主题中声明这一点来完全禁用它:

android:windowDisablePreview

<style name="Theme.MyTheme" parent="android:style/Theme.Holo">
    <!-- This disables the black preview screen -->
    <item name="android:windowDisablePreview">true</item>
</style>

此处发布了有关如何处理此屏幕的说明:http://cyrilmottier.com/2013/01/23/android-app-launching-made-gorgeous/

答案 2 :(得分:8)

AndroidManifest.xml中的这一行添加到启动器活动:

android:theme="@android:style/Theme.Translucent.NoTitleBar.Fullscreen

答案 3 :(得分:1)

如果你在onCreate中调用一些“重代码”,屏幕将显示为黑色,直到完成加载。您可以考虑使用AsyncTask并使onCreate句柄设置setContentView等,并使AsyncTask处理“重代码”。

答案 4 :(得分:1)

避免此问题的更好解决方案是使用AsyncTask,这是我在其中一个ListActivity中使用的示例代码:

 private class YoutubeTask extends AsyncTask<URL, Integer, String> {
     protected void onPreExecute() {
            super.onPreExecute();
            mLoadingProgress.startAnimation(mDisappear);
        mLoadingProgress.setVisibility(View.GONE);
            showDialogProgress();
    }

     protected String doInBackground(URL... url) {

         youtubeData = VersionParser.readFromUrl(url[0]);;

        try {

            JSONObject jsono = new JSONObject(youtubeData);
            JSONObject feed = jsono.getJSONObject("feed");
            JSONArray entry = feed.getJSONArray("entry");

            for(int i = 0 ; i < entry.length() ; i++ ){

                JSONObject item = entry.getJSONObject(i);

                JSONArray AUTHOR = item.getJSONArray(TAG_AUTHOR);
                JSONObject Author = AUTHOR.getJSONObject(0);
                JSONObject author = Author.getJSONObject("name");
                String author_name = author.getString(TAG_TITRE);

                JSONObject Statistics = item.getJSONObject("yt$statistics");
                String Views = Statistics.getString(TAG_VIEWS);

                JSONObject Media = item.getJSONObject("media$group");

                JSONObject MediaTitle = Media.getJSONObject("media$title");
                String title = MediaTitle.getString(TAG_TITRE);

                JSONObject DURATION = Media.getJSONObject("yt$duration");
                String duration = DURATION.getString(TAG_DURATION);

                JSONArray Thumbinail = Media.getJSONArray("media$thumbnail");
                JSONObject IMAGE = Thumbinail.getJSONObject(0);
                String image = IMAGE.getString(TAG_CONTENT);
                String id = image.substring(22,33);

                 map = new HashMap<String, String>();

                    map.put(TAG_TITRE , title ); 
                    map.put(TAG_ID , id );
                    map.put(TAG_DURATION , duration );
                    map.put(TAG_IMAGE , image);
                    map.put(TAG_VIEWS , Views );
                    map.put(TAG_AUTHOR , author_name);

                   CURRENCY.add(map);
                }


        } catch (JSONException e) {

            e.printStackTrace();
        }
            return null;
        }


     @Override
        protected void onPostExecute(String result) { 

             dismisDialogProgress(); 
             mListView.setVisibility(View.VISIBLE);
            mListView.startAnimation(mAppear);
            mAdapter = new MAdapter(youtubeSearch.this , CURRENCY);
            mListView.setSelector(R.drawable.home_bg);
            mListView.setAdapter(mAdapter);

             } 
    }

并在onCreate Methode内实现:

            @Override

    public void onCreate(Bundle savedInstanceState) {

        if (Build.VERSION.SDK_INT < 11)
        setTheme(android.R.style.Theme_Black_NoTitleBar);
    }
        super.onCreate(savedInstanceState);
        setContentView(R.layout.main);


    new YoutubeTask().execute(new URL("https://gdata.youtube.com/feeds/api/videos?q=Adele&max-results=15&v=2&alt=json"));
        }

答案 5 :(得分:0)

这里有一些值得思考的东西;也许你没有在你的应用程序中有一个相当大的初始化延迟;事实上,你可能 waiting for the instant run service

根据我的经验,其症状是您的应用程序在初始化时显示了一个很长的黑屏,但在调试时您发现没有Application / Activity&# 39; s onCreate方法尚未被称为,但它可见。