0

我正在 Delphi XE5 上创建 Firemonkey 移动应用程序。我想使用 TPaintBox 组件来显示一些文本(2000 多个带有特殊字符的单词、表格)。我创建了带有 TListBox 的表单和type TListBoxItemPaintBox = Class(TListBoxItem)包含 TPaintBox 的特殊 TListBoxItem 。想法很简单——在 TPaintBox 上我将绘制数据,而 TListBox 将负责滚动。

当这个应用程序在 Win32 下编译时,一切运行完美,滚动很快。但是当我在我的手机上用 Android 编译这个应用程序时,应用程序变得毫无用处 - 滚动是令人难以置信的 ss-ll-oooo-www。

这是我的应用程序的完整代码(简化但工作版本)

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.Layouts,
  FMX.ListBox, FMX.StdCtrls, FMX.Objects, System.UIConsts;

type
  TListBoxItemPaintBox = Class(TListBoxItem)
    public
      pbMain: TPaintBox;
      List: TStringList;
      constructor Create(AOwner: TComponent); override;
      procedure pbMainOnPaint(Sender: TObject; Canvas: TCanvas);
  end;

type
  TForm1 = class(TForm)
    Panel1: TPanel;
    Button1: TButton;
    lbMain: TListBox;
    procedure Button1Click(Sender: TObject);
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
{TListBoxItemPaintBox}
//%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
constructor TListBoxItemPaintBox.Create(AOwner: TComponent);
begin
  inherited Create(AOwner);

  self.Height := 200;
  self.Text := '';

  pbMain := TPaintBox.Create( self );
  pbMain.Parent := self;
  pbMain.Align := TAlignLayout.alClient;
  pbMain.OnPaint := pbMainOnPaint;
end;

procedure TListBoxItemPaintBox.pbMainOnPaint(Sender: TObject; Canvas: TCanvas);
// pbMain.OnPaint := pbMainOnPaint;
var
  i, vertPos: Integer;
  s: String;
  rect: TRectF;
begin
  vertPos := 0;

  Canvas.BeginScene;
  Canvas.Clear(claWhite);

  Canvas.Fill.Color := claBlack;
  Canvas.Font.Style := [];
  Canvas.Font.Size := 12;

  for i := 0 to List.Count-1 do
    begin
      s := List[i];
      rect.Create(0,
                  vertPos,
                  Canvas.TextWidth(s),
                  vertPos+Canvas.TextHeight(s));
      Canvas.FillText(rect, s, false, 255, [], 
                    TTextAlign.taLeading ,TTextAlign.taCenter);
      vertPos := vertPos + 15;
    end;

  self.Height := vertPos;
  pbMain.Canvas.EndScene;
end;

//%%%%%%%%%%%%%%%%%%%%%
{TForm1}
//%%%%%%%%%%%%%%%%%%%%%
procedure TForm1.Button1Click(Sender: TObject);
var
  ListTemp: TStringList;
  aListBoxItem: TListBoxItemPaintBox;
  i: Integer;
begin
  ListTemp := TStringList.Create;
  for i := 0 to 80 do
    ListTemp.Add(IntToStr(i));

  aListBoxItem := TListBoxItemPaintBox.Create( lbMain );
  aListBoxItem.List := ListTemp;
  lbMain.AddObject(aListBoxItem);
end;

end.

有谁知道如何使它在Android上运行?是否有更合适的方式使用 TPaintBox 或者我应该使用完全不同的组件?

4

1 回答 1

0

ListBox 不是拥有如此多记录的正确容器,因为它非常慢,但它确实比其他的更可定制。您必须使用 ListView 组件才能实现速度和正常滚动。让它显示你想要的所有东西会有点痛苦,因为它有点受限于它自己。您将必须创建自己的样式,当项目由代码创建时,它们将在该样式上收听。

这是一个非常有用的视频,介绍了 Ray Konopka 如何实现这一目标。

http://www.youtube.com/watch?v=XRj3qjUjBlc&list=PLwUPJvR9mZHiaYvH9Xr7WuFCVYugC4d0w&index=23

于 2014-01-24T07:14:00.210 回答