
Share Your Code
Browse Code
- All (914)
-
(95)
-
(87)
-
(45)
-
(4)
-
(42)
-
(136)
-
(22)
-
(40)
-
(13)
-
(66)
-
(153)
-
(211)
Using splash/load screen on Android
When developing apps for both iOS and Android I've wanted a standard way to display a splash screen while the app is loading.
I've written a small module that provides 2 options:
Option 1)
iOS has a nice built-in feature that displays an image (Default.png) while the app is loading.
This option takes advantage of the iOS image files.
Option 2)
If you don't want/need a graphic (which might in itself might take a few seconds to appear), then this option will only display a simple "Loading..." message in the center of the screen.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 | -- splash.lua -- created: 2011-10-02 -- -- version 1.0.1 -- -- Released under the MIT license -- -- Copyright (C) 2011 by Ingemar Bergmark, swipeware.com -- -- Permission is hereby granted, free of charge, to any person obtaining a copy -- of this software and associated documentation files (the "Software"), to deal -- in the Software without restriction, including without limitation the rights -- to use, copy, modify, merge, publish, distribute, sublicense, and/or sell -- copies of the Software, and to permit persons to whom the Software is -- furnished to do so, subject to the following conditions: -- -- The above copyright notice and this permission notice shall be included in -- all copies or substantial portions of the Software. -- -- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR -- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, -- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE -- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER -- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, -- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN -- THE SOFTWARE. -- local Module = {}; local moduleName = "splash.lua: "; local loaderFunction; -- function called to load main screen local splashScreen; -- the splash screen local frameCount = 0; -- keep track of frames local usingSplash = false; -- true when splash screen is used local loadSplashScreen = function(onlyText) local splashObject; splashScreen = display.newGroup(); if (onlyText) then splashObject = display.newText("Loading...", 0, 0, native.systemFontBold, 20); else splashObject = display.newImageRect("Default.png", 320, 480); splashObject.rotation = -90; end splashObject.x = (display.contentWidth / 2); splashObject.y = (display.contentHeight / 2); splashScreen:insert(splashObject); end local enterFrameListener = function() frameCount = frameCount + 1; if (frameCount == 3) then -- skip some frames to allow splash to load if (not loaderFunction) then print(moduleName.."WARNING: loader function not set"); else loaderFunction(); end end; end Module.startSplash = function(onlyText) loadSplashScreen(onlyText); Runtime:addEventListener("enterFrame", enterFrameListener); usingSplash = true; end Module.stopSplash = function() if (usingSplash) then Runtime:removeEventListener("enterFrame", enterFrameListener); splashScreen:removeSelf(); splashScreen = nil; usingSplash = false; end end Module.setLoader = function(loader) if (type(loader) ~= "function") then print(moduleName.."ERROR: loader must be a function"); else loaderFunction = loader; end end return Module; |
The 3 main functions are:
splash.setLoader(func) --> pass the function to load your main screen
splash.startSplash([true | false]) --> show the splash screen (optionally specifying if only a text screen should be used)
splash.stopSplash() --> remove splash screen and free memory
To use the module, just require it in your main.lua, and call setLoader and startSplash:
(if using Option 1 above then Default.png should exist in the root folder of your app)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 | -- main.lua local directives = require("directives"); local splash = require("splash"); local game = require("game"); if (directives.TARGET == "Android") then splash.setLoader(game.startGame); splash.startSplash(); -- by default a graphical screen will be displayed --splash.startSplash(true); -- use this if you only want a simple "Loading..." text (faster display) else game.startGame(); end |
As you can see I only call this module if the app is running on Android. iOS already has this functionality built in, so it's unnecessary to use it for iOS.
Note: The "directives" you see above is just my way to determine what OS the device is running so that I can provide conditional decision-making at runtime.
Finally you should call splash.stopSplash() to remove the splash screen and free up memory. This should be added as the last statement in the startup routine that you passed to setLoader().
Just modify your startup routine and add splash.stopSplash() at the end. Remember to also add a require at the top of your lua.
Hope this code will be helpful.
- Type:
- Tags:
I updated the code to include an option to only display a text message, instead of loading a graphic.