Android实例-Delphi在运行时更改Android屏幕旋转(IOS也支持,不过我可没有苹果机,测试不了)
相关资料:
https://www.it1352.com/624177.html
PS:
如果手机锁定方向时,只能是竖向、横向、倒转横向。如果方向未锁定时,可以是竖向、横向、倒转竖向、倒转横向。
android实例:
1 unit Unit2; 2 3 interface 4 5 uses 6 System.SysUtils, System.Types, System.UITypes, System.Classes, System.Variants, 7 FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMXTee.Engine, 8 FMXTee.Procs, FMXTee.Chart, FMX.Layouts, FMX.Controls.Presentation, 9 FMX.StdCtrls; 10 11 type 12 TForm2 = class(TForm) 13 Layout1: TLayout; 14 Chart1: TChart; 15 Layout2: TLayout; 16 Button1: TButton; 17 Button2: TButton; 18 Button3: TButton; 19 Button4: TButton; 20 Button5: TButton; 21 procedure Button1Click(Sender: TObject); 22 procedure Button5Click(Sender: TObject); 23 procedure Button2Click(Sender: TObject); 24 procedure Button3Click(Sender: TObject); 25 procedure Button4Click(Sender: TObject); 26 private 27 { Private declarations } 28 public 29 { Public declarations } 30 end; 31 32 var 33 Form2: TForm2; 34 35 implementation 36 uses 37 FMX.Platform; //需要引入 38 {$R *.fmx} 39 {$R *.LgXhdpiPh.fmx ANDROID} 40 41 procedure APPSetScreenOrientation(ADirection: TScreenOrientation); 42 var 43 ScreenService: IFMXScreenService; 44 OrientSet: TScreenOrientations; 45 begin 46 if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService)) 47 then 48 begin 49 //Portrait 竖向,纵向 50 //Landscape 横向 51 //InvertedPortrait 倒转竖向,纵向 52 //InvertedLandscape 倒转横向 53 OrientSet := [ADirection]; 54 ScreenService.SetScreenOrientation(OrientSet); 55 end; 56 end; 57 58 procedure TForm2.Button1Click(Sender: TObject); 59 begin 60 APPSetScreenOrientation(TScreenOrientation.Portrait); 61 end; 62 63 procedure TForm2.Button2Click(Sender: TObject); 64 begin 65 APPSetScreenOrientation(TScreenOrientation.Landscape); 66 end; 67 68 procedure TForm2.Button3Click(Sender: TObject); 69 begin 70 APPSetScreenOrientation(TScreenOrientation.InvertedPortrait); 71 end; 72 73 procedure TForm2.Button4Click(Sender: TObject); 74 begin 75 APPSetScreenOrientation(TScreenOrientation.InvertedLandscape); 76 end; 77 78 procedure TForm2.Button5Click(Sender: TObject); 79 var 80 ScreenService: IFMXScreenService; 81 begin 82 if TPlatformServices.Current.SupportsPlatformService(IFMXScreenService, IInterface(ScreenService)) 83 then 84 begin 85 case ScreenService.GetScreenOrientation of 86 TScreenOrientation.Portrait: ShowMessage('Portrait'); 87 TScreenOrientation.Landscape: ShowMessage('landscape'); 88 TScreenOrientation.InvertedPortrait: ShowMessage('Inverted-Portrait'); 89 TScreenOrientation.InvertedLandscape: ShowMessage('Inverted-Landscape'); 90 else ShowMessage('not set'); 91 end; 92 end; 93 end; 94 95 end.
IOS实例(我没有苹果机,没测试过):
1 Uses: iOSapi.UIKit; 2 3 procedure ChangeiOSorientation(toOrientation: UIInterfaceOrientation; 4 possibleOrientations: TScreenOrientations); 5 var 6 win : UIWindow; 7 App : UIApplication; 8 viewController : UIViewController; 9 oucon: UIViewController; 10 begin 11 Application.FormFactor.Orientations := []; //Change supported orientations 12 App := TUIApplication.Wrap(TUIApplication.OCClass.sharedApplication); 13 win := TUIWindow.Wrap(App.windows.objectAtIndex(0)); //The first Windows is always the main Window 14 15 App.setStatusBarOrientation(toOrientation); 16 {After you have changed your statusbar orientation set the 17 Supported orientation/orientations to whatever you need} 18 Application.FormFactor.Orientations := possibleOrientations; 19 viewController := TUIViewController.Wrap(TUIViewController.alloc.init);//dummy ViewController 20 oucon := TUIViewController.Wrap(TUIViewController.alloc.init); 21 {Now we are creating a new Viewcontroller now when it is created 22 it will have to check what is the supported orientations} 23 oucon := win.rootViewController;//we store all our current content to the new ViewController 24 Win.setRootViewController(viewController); 25 Win.makeKeyAndVisible;// We display the Dummy viewcontroller 26 27 win.setRootViewController(oucon); 28 win.makeKeyAndVisible; 29 {And now we Display our original Content in a new Viewcontroller 30 with our new Supported orientations} 31 end;
调用:
1 ChangeiOSorientation(UIInterfaceOrientationPortrait,[TScreenOrientation.Portrait,TScreenOrientation.Landscape,TScreenOrientation.InvertedLandscape]);
作者:疯狂Delphi
本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,否则保留追究法律责任的权利.
欢迎关注我,一起进步!扫描下方二维码即可加我