简体   繁体   English

Android用户何时真正可以看到Delphi表单?

[英]When is a Delphi form actually visible to the user on Android?

The game that I'm developing does some pre-processing when first loading and I want to show the user a "loading %" to make it clear that the game is not frozen. 我正在开发的游戏在首次加载时会进行一些预处理,因此我想向用户显示一个“加载%”,以表明该游戏未冻结。

I am trying to do this by waiting for the main form to show for the user and then displaying an updating progress bar while performing the pre-processing. 我试图通过等待主表单显示给用户,然后在执行预处理时显示更新进度栏来做到这一点。

For the pre-processing to work, I need two pieces of information: 为了使预处理正常工作,我需要两条信息:
1. The form is in it's final size (maximized across the entire display). 1.表格以其最终尺寸显示(在整个显示屏上最大化)。
2. The form is visible to the user. 2.表单对用户可见。

Here is a log showing the order of the form events being triggered as my game loads. 这是一条日志,显示了在我的游戏加载时触发表单事件的顺序。 Keep in mind that I'm not actually changing the form's size using code in any of these events: 请记住,在以下任何事件中,我实际上都没有使用代码来更改表单的大小:

FormCreate
FormResize, clientSize : 0x0
FormResize, clientSize : 0x0
FormResize, clientSize : 0x0
FormResize, clientSize : 0x0
FormShow
FormActivate
FormResize, clientSize : 360x640

Currently, I try to show the loading screen on the last resize (the one indicating the form is actually the size of the display), but at this point, the main form is still not visible (even after the FormShow and FormActivate) and I end up with the Android's default "gray gradient" screen showing until after my pre-processing code already finished, never showing the progress bar. 当前,我尝试在最后一次调整大小时显示加载屏幕(指示表单实际上是显示的大小),但是此时,主表单仍然不可见(即使在FormShow和FormActivate之后),我最后显示Android的默认“灰色渐变”屏幕,直到我的预处理代码完成后才显示进度条。

I tried calling "application.processmessages" after updating the progress bar, but it doesn't make a difference... 我尝试在更新进度条后调用“ application.processmessages”,但这并没有什么不同...

How do I detect when my main form is actually visible to the Android user? 如何检测我的主表单实际上对Android用户可见?

[Update] [更新]
I created a small application to demonstrate this issue: 我创建了一个小应用程序来演示此问题:
https://github.com/bLightZP/Test_Splash https://github.com/bLightZP/Test_Splash

This example illustrates using OnIdle, and also how it should be implemented. 这个例子说明了使用的OnIdle,还应该如何执行。

unit Unit1;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Controls.Presentation, FMX.StdCtrls;

type
  TForm1 = class(TForm)
    ProgressBar1: TProgressBar;
  private
    FShown: Boolean;
    procedure ApplicationOnIdleHandler(Sender: TObject; var Done: Boolean);
    procedure ExecutePreProcessing;
    procedure ExecutePreProcessingSynchronized;
  public
    constructor Create(AOwner: TComponent); override;
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

constructor TForm1.Create(AOwner: TComponent);
begin
  inherited;
  // Method 1: Use OnIdle to execute code when the main form is visible:
  Application.OnIdle := ApplicationOnIdleHandler;
  // Method 2: Use a thread to execute the preprocessing. Comment out the line above if using this method, and uncomment the line below
  // TThread.CreateAnonymousThread(ExecutePreProcessingSynchronized).Start;
end;

procedure TForm1.ApplicationOnIdleHandler(Sender: TObject; var Done: Boolean);
begin
  if not FShown then
  begin
    FShown := True;
    ExecutePreProcessing;
  end;
end;

procedure TForm1.ExecutePreProcessing;
var
  I: Integer;
begin
  for I := 1 to 100 do
  begin
    Sleep(50);
    ProgressBar1.Value := I;
    Application.ProcessMessages; // <---- Not the best idea
  end;
end;

procedure TForm1.ExecutePreProcessingSynchronized;
var
  I: Integer;
begin
  for I := 1 to 100 do
  begin
    Sleep(50);
    // Ensure UI updates happen in the main thread
    TThread.Synchronize(nil,
      procedure
      begin
        ProgressBar1.Value := I;
      end
    );
  end;
end;

end.

How about BecameActive event (ApplicationEvent)? BecameActive事件(ApplicationEvent)怎么样? Use this article, translate it with Google Translate http://delphifmandroid.blogspot.com/2016/09/delphi-android.html 使用本文并将其翻译为Google翻译http://delphifmandroid.blogspot.com/2016/09/delphi-android.html

Starting app: 启动应用程序:

09-20 20:13:43.151: I/info(15893): FMX: Project1: FormCreate
09-20 20:13:43.151: I/info(15893): FMX: Project1: FormResize
09-20 20:13:43.151: I/info(15893): FMX: Project1: FormResize
09-20 20:13:43.161: I/info(15893): FMX: Project1: FormResize
09-20 20:13:43.161: I/info(15893): FMX: Project1: FormShow
09-20 20:13:43.161: I/info(15893): FMX: Project1: FormActivate
09-20 20:13:43.161: I/info(15893): FMX: Project1: Finished Launching
09-20 20:13:43.221: I/info(15893): FMX: Project1: FormPaint
09-20 20:13:43.231: I/info(15893): FMX: Project1: Became Active
09-20 20:13:43.261: I/info(15893): FMX: Project1: FormPaint
09-20 20:13:43.281: I/info(15893): FMX: Project1: FormPaint
09-20 20:13:43.311: I/info(15893): FMX: Project1: FormPaint

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

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