Android Delphi中的组件大小 [英] Component sizes in Delphi Android

查看:106
本文介绍了Android Delphi中的组件大小的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用Firemonkey在Delphi XE5中创建一个Android应用,对于我的一个表单,我需要一个TImage来填充整个表单,以便在其上绘制Bitmap。

I am creating an Android app in Delphi XE5 using Firemonkey, and for one of my forms I need a TImage that will fill the entire form where I can draw on it's Bitmap.

我所做的是>

What I did was this:


  1. 我添加了一个TImage组件设置为我的表单并将其设置为Align:= alClient。在设计器中填充整个表单。

  1. I added a TImage component to my form and set it's Align := alClient. In the designer it fills the entire form.

在我的FormCreate过程中,我通过将TImage的大小设置为TImage的大小来初始化TImage的位图

In my FormCreate procedure I initialized the Bitmap of the TImage by setting it's size to the size of the TImage

procedure TDrawForm.FormCreate(Sender: TObject);
begin
    Image1.Bitmap.SetSize(Trunc(Image1.Width), Trunc(Image1.Height));
    DrawFromList(Image1);
end;


  • 然后我在TImage的位图上绘制。画布

  • Then I draw on the TImage's Bitmap.Canvas

    procedure TDrawForm.DrawFromList(image: TImage);
    var draw: TDraw;
    begin
       image.Bitmap.Canvas.BeginScene;
       image.Bitmap.Canvas.Clear(claBlue);
       image.Bitmap.Canvas.Stroke.Color := claBlack;
       image.Bitmap.Canvas.Fill.Color := claYellow;
       for draw in DrawList do
       begin
          image.Bitmap.Canvas.FillPath(draw.fpath, 1);
          image.Bitmap.Canvas.DrawPath(draw.fpath, 1);
       end;
       image.Bitmap.Canvas.EndScene;
    end;
    


  • 此代码在我的Nexus 7上效果很好平板电脑,但无法填充整个屏幕:

    This code draws just fine on my Nexus 7 tablet, but it does not fill the entire screen:

    在我使用的第二张图片中

    In the second picture I used

    Image1.Bitmap.SetSize(Trunc(Image1.Width * 1.34), Trunc(Image1.Height * 1.34));
    

    通过反复试验发现1.34,使其覆盖整个屏幕。

    where 1.34 was found by trial and error, to make it fill the entire screen.

    然后我尝试打印出图像和屏幕的大小。对于我的Nexus 7(1280 x 800像素),填充整个屏幕的TImage组件的宽度为601,高度为880。因此,当我将位图的大小设置为TImage的大小时,位图也是601 x 880,这说明了为什么我需要缩放位图以使其覆盖整个屏幕。屏幕大小报告为600 x 905(使用Screen.Size)。

    I then tried to print out the size of the image and the screen. For my Nexus 7 (which is 1280 x 800 pixels) the TImage component which fills the entire screen, has a width of 601 and a height of 880. Thus, when I set the size of the Bitmap to be the size of my TImage, the Bitmap is also 601 x 880, which explains why I needed to scale the Bitmap to make it fill the entire screen. The screen size is reported as 600 x 905 (using Screen.Size).

    我的问题是为什么仅TImage组件的大小我的设备的实际分辨率是800 x 1280时是601 x 880(屏幕只有600 x 905)?如果这些尺寸不是像素,那么它们是什么?更重要的是,如何将这些数字转换为实际的屏幕像素(请注意,1.34仅适用于Nexus 7-在我的HTC Sensation上需要更大)?

    My question is why is the size of the TImage component only 601 x 880 (and the screen only 600 x 905) when the actual resolution of my device is 800 x 1280? If these sizes are not pixels, then what are they? And more importantly, how do I convert from these numbers to actual screen pixels (bear in mind, 1.34 only works for Nexus 7 - on my HTC Sensation it needed to be even bigger)?

    我的一位同事建议说,这可能与屏幕的DPI有关,但我找不到任何能使我获得DPI的方法。

    One of my co-workers suggested that it might have to do with the DPI of the screen, but I couldn't find any method that will give me the DPI of the current device.

    我也尝试过在FormResize过程中更新位图的大小,但这没什么区别。

    I have also tried updating the size of the bitmap in the FormResize procedure, but that doesn't make a difference.

    推荐答案

    我找到了解决问题的方法。显然,在组件上有两个将返回比例的属性。幸运的是,此比例乘以组件的宽度和高度将返回宽度和高度以像素为单位

    I found a solution to my problem. Apparently there are, on the component, two properties that will return a scale. Luckily, this scale multiplied with the width and height of the component will return the width and height in pixels.

    Image1.Scene.GetSceneScale
    Image1.Canvas.Scale
    

    对于我来说,我必须像这样初始化我的位图:

    In my case, I had to initialize my Bitmap like this:

    Image1.Bitmap.SetSize(Trunc(Image1.Width * Image1.Canvas.Scale), 
                          Trunc(Image1.Height * Image1.Canvas.Scale));
    

    这使我的Image1的位图充满了整个屏幕,因为图像设置为Align:= alClient

    This makes the Bitmap of my Image1 fill the entire screen since the image is set to Align := alClient.

    我仍然不知道Image1.Width和Image1.Height(或者说Screen.Size)返回的原始大小是多少,但是并非总是像素。如果在某处记录下来,那将为我节省大约两个星期的头痛。

    I still don't know what the original size returned by Image1.Width and Image1.Height (or Screen.Size for that matter) is measured in, but it is not always pixels. It would have saved me about two weeks of headache if this was documented somewhere.

    这篇关于Android Delphi中的组件大小的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

    查看全文
    登录 关闭
    扫码关注1秒登录
    发送“验证码”获取 | 15天全站免登陆