首页 文章

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

提问于
浏览
26

我们知道,当应用程序做一些漫长的过程,比如从互联网上下载一些信息时,它可能会在加载应用程序之前显示启动画面,当应用程序完全加载时,它将显示主页面 . 在启动画面活动中,我们必须在线程中加载长进程,以避免在加载应用程序之前显示黑屏 . 我做了所有这些 . 但在显示应用程序之前还会出现黑屏 . 这是启动画面活动的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);
        }

    }

OK.Long story short . 现在问题是这个 - 真的是这个黑屏的根本原因是什么 . 这是来自启动活动还是来自应用程序类 . 我们如何解决它并避免形式表现出来?

7 回答

  • 0

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

    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.

  • 8

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

    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/

  • 1

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

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

  • 2

    您可以通过将图像转换为画笔(颜色)来解决此错误 .

    在drawable文件夹中添加新文件xml(splash_bg.xml)文件,如下所示 .

    <?xml version="1.0" encoding="utf-8" ?>
    <layer-list xmlns:android="http://schemas.android.com/apk/res/android">
       <item>
          <color android:color="@color/splash_bg_color"/>
       </item>
       <item>
            <bitmap
                android:src="@drawable/splash_screen"
                android:tileMode="disabled"
                android:gravity="center"/>
       </item>
    </layer-list>
    

    现在添加一个新样式,并将splash_bg.xml应用为背景颜色 .

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

    将此新样式应用于主启动器活动或启动画面 .

    [Activity(Label = "label", MainLauncher = true, Theme = "@style/Theme.SplashBg")]
    public class SplashScreenActivity : Activity
    
  • 48

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

  • 62

    避免这个问题的更好的解决方案是使用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"));
            }
    
  • 1

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

    根据我的经验,这样做的症状是你的应用程序在初始化时会显示一个很长的黑屏,但是经过调试你发现你的所有方法都没有在它可见时被调用 .

相关问题