Android游戏启动屏结束过早,如何手动控制隐藏时机避免启动黑屏
解决方案
核心思路是推迟启动屏的关闭时机,等游戏首次Draw方法执行完成、游戏视图已经有渲染内容后再清除启动屏背景、切换主主题,避免出现空白间隙。
1. 修改Game1.cs 增加首次绘制完成回调
在游戏第一次执行完绘制逻辑后触发回调通知Activity:
namespace splashapp { public class Game1 : Game { private GraphicsDeviceManager _graphics; private SpriteBatch _spriteBatch; // 新增:首次绘制完成回调事件 public Action OnFirstDrawCompleted; private bool _isFirstDraw = true; public Game1() { _graphics = new GraphicsDeviceManager(this); Content.RootDirectory = "Content"; } protected override void Initialize() { base.Initialize(); } protected override void LoadContent() { _spriteBatch = new SpriteBatch(GraphicsDevice); } protected override void Update(GameTime gameTime) { base.Update(gameTime); } protected override void Draw(GameTime gameTime) { GraphicsDevice.Clear(Color.CornflowerBlue); // 原有游戏精灵绘制逻辑保留在此处 base.Draw(gameTime); // 新增:首次绘制完成触发回调,仅执行一次 if (_isFirstDraw) { _isFirstDraw = false; OnFirstDrawCompleted?.Invoke(); } } } }
2. 修改Activity1.cs 推迟主题切换时机
不要在OnCreate初始化阶段就切换主题,等收到首次绘制完成的回调后再执行启动屏关闭逻辑:
namespace splashapp { [Activity( Label = "@string/app_name", MainLauncher = true, Theme = "@style/SplashTheme", Icon = "@drawable/icon", AlwaysRetainTaskState = true, LaunchMode = LaunchMode.SingleInstance, ScreenOrientation = ScreenOrientation.FullUser, ConfigurationChanges = ConfigChanges.Orientation | ConfigChanges.Keyboard | ConfigChanges.KeyboardHidden | ConfigChanges.ScreenSize )] public class Activity1 : AndroidGameActivity { private Game1 _game; private View _view; protected override void OnCreate(Bundle bundle) { // 移除原有的初始化阶段SetTheme调用,推迟到绘制完成后执行 base.OnCreate(bundle); _game = new Game1(); _view = _game.Services.GetService(typeof(View)) as View; // 初始化阶段将游戏视图设为透明,避免未渲染时透出黑屏 _view.SetBackgroundColor(Android.Graphics.Color.Transparent); SetContentView(_view); // 监听游戏首次绘制完成事件 _game.OnFirstDrawCompleted += () => { // 切回UI线程执行视图修改操作 RunOnUiThread(() => { // 切换为主主题,清空启动屏窗口背景 SetTheme(Resource.Style.MainTheme); Window.SetBackgroundDrawable(null); // 恢复游戏视图默认状态 _view.SetBackgroundColor(null); }); }; _game.Run(); } } }
可选优化
如果需要更自然的过渡效果,可以在切换主题时增加数百毫秒的淡入淡出动画,消除视觉跳变。
内容的提问来源于stack exchange,提问作者harry_mitch
相关产品推荐
相关产品推荐

