Delphi Android - detect device orientation change

前端 未结 1 1693
太阳男子
太阳男子 2021-01-03 05:14

New to both Delphi Android development. How do I detect that the screen orientation has changed? i.e. from Portrait to Landscape and vice versa? And how do I fire off code w

1条回答
  •  说谎
    说谎 (楼主)
    2021-01-03 05:37

    In your form implement a method

    procedure DoOrientationChanged(const Sender: TObject; const M: TMessage);
    

    where you handle the current orientation. Subscribe for orientation changes in FormCreate like this

    TMessageManager.DefaultManager.SubscribeToMessage(TOrientationChangedMessage, DoOrientationChanged);
    

    and unsubscribe in FormDestroy like this:

    TMessageManager.DefaultManager.Unsubscribe(TOrientationChangedMessage, DoOrientationChanged);
    

    To find out the current screen orientation just ask IFMXScreenService:

    var
      screenService: IFMXScreenService;
    begin
      if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, screenService) then begin
        case screenService.GetScreenOrientation of
          TScreenOrientation.Portrait: ;
          TScreenOrientation.Landscape: ;
          TScreenOrientation.InvertedPortrait: ;
          TScreenOrientation.InvertedLandscape: ;
        end;
      end;
    end;
    

    0 讨论(0)
提交回复
热议问题