[Android]获取屏幕的物理分辨率

在Windows下,我们可以用Screen对象获取当前分辨率,但在Android等FMX框架下,Screen.Size的值明显不正确。在Delphi.org上看到一篇文章,发现原来是这样子:)

uses FMX.BehaviorManager;

var
  DeviceBehavior: IDeviceBehavior;
  DisplayMetrics: TDeviceDisplayMetrics;
begin
if TBehaviorServices.Current.SupportsBehaviorService(IDeviceBehavior,DeviceBehavior,Self) then
  begin
  DisplayMetrics:=DeviceBehavior.GetDisplayMetrics(Self);
  Label1.Text:='物理分辨率:'+IntToStr(DisplayMetrics.PhysicalScreenSize.cx)+'x'+IntToStr(DisplayMetrics.PhysicalScreenSize.cy)+#13#10+
    '屏幕尺寸:'+FloatToStr(sqrt(
      DisplayMetrics.PhysicalScreenSize.cx*DisplayMetrics.PhysicalScreenSize.cx/DisplayMetrics.PixelsPerInch/DisplayMetrics.PixelsPerInch+
      DisplayMetrics.PhysicalScreenSize.cy*DisplayMetrics.PhysicalScreenSize.cy/DisplayMetrics.PixelsPerInch/DisplayMetrics.PixelsPerInch))+#13#10+
    'PPI:'+FloatToStr(DisplayMetrics.PixelsPerInch);
  end;
end;

TDeviceDisplayMetrics的几个成员:

PhysicalScreenSize记录了物理分辨率,单位是像素,cx为宽度,cy为高度

LogicalScreenSize 记录了逻辑分辨率,单位是英寸,实际上是物理分辨率除以每英寸的像素数(PPI)取整的结果

AspectRatio 屏幕比例
PixelsPerInch 每英寸像素数
ScreenScale 屏幕缩放比例
FontScale 字体缩放比例

分享到: