Thema: Leanback

Einzelnen Beitrag anzeigen

Peter666

Registriert seit: 11. Aug 2007
357 Beiträge
 
#6

AW: Leanback

  Alt 28. Aug 2019, 20:08
Hallo,

ich habe heute mal etwas Zeit gehabt mit Firemonkey zu spielen. Folgender Code erstellt 11 bunte Tiles die, wenn Sie den Focus bekommen animiert sich vergrößern bzw. verkleinern. Das funktioniert auf allen Plattformen, sowohl via Touch, als auch mit Tastatur. Mein Problem ist, dass ich nicht weiß wie man die Scrollbox möglichst angenehm auf den fokussierten Eintrag anpasst. Hat da jemand eine Idee? Das ist so ziemlich das einzigste Problem was ich an dem Code noch habe. Aktuell lade ich in einem Thread die Grafiken habe mehrere Scrollboxen als Rubriken untereinander. Lediglich das horizontale und vertikale scrollen bekomme ich schlichtweg nicht hin.

Delphi-Quellcode:
unit UMain;

interface

uses
  System.SysUtils, System.Types, System.UITypes, System.Classes,
  System.Variants,
  FMX.Types, FMX.Controls, FMX.Forms, FMX.Graphics, FMX.Dialogs, FMX.Layouts;

type
  TTileItem = class(TControl)
  protected
    FZoomFactor: Single;
    FBackgroundColor: TAlphaColor;

    procedure Paint; override;
    procedure DoEnter; override;
    procedure DoExit; override;

    procedure SetZoomFactor(AValue: Single);
    procedure SetBackgroundColor(AValue: TAlphaColor);
  public
    constructor Create(AOwner: TComponent); override;
    destructor Destroy; override;
  published
    property BackgroundColor: TAlphaColor read FBackgroundColor
      write SetBackgroundColor;
    property ZoomFactor: Single read FZoomFactor write SetZoomFactor;
  end;

  TForm3 = class(TForm)
    HorzScrollBox1: THorzScrollBox;
    procedure FormCreate(Sender: TObject);
  private
    { Private-Deklarationen }
  public
    { Public-Deklarationen }
  end;

var
  Form3: TForm3;

implementation

uses FMX.Ani;

{$R *.fmx}

const
  cZoomIn = 0.9;
  cZoomOut = 1.0;
  cZoomTime = 0.4;

constructor TTileItem.Create(AOwner: TComponent);
begin
  inherited;
  CanFocus := true;
  ZoomFactor := 0;
  FBackgroundColor := TAlphaColors.Gray;
end;

destructor TTileItem.Destroy;
begin
  inherited;
end;

procedure TTileItem.DoEnter;
begin
  BringToFront;
  TAnimator.AnimateFloat(self, 'ZoomFactor', cZoomOut, cZoomTime,
    TAnimationType.Out, TInterpolationType.Quartic);
end;

procedure TTileItem.DoExit;
begin
  SendToBack;
  TAnimator.AnimateFloat(self, 'ZoomFactor', cZoomIn, cZoomTime,
    TAnimationType.Out, TInterpolationType.Quartic);
end;

procedure TTileItem.SetBackgroundColor(AValue: TAlphaColor);
begin
  if FBackgroundColor <> AValue then
  begin
    FBackgroundColor := AValue;
    repaint;
  end;
end;

procedure TTileItem.SetZoomFactor(AValue: Single);
begin
  if AValue < cZoomIn then
    AValue := cZoomIn;
  if AValue > cZoomOut then
    AValue := cZoomOut;

  if FZoomFactor <> AValue then
  begin
    FZoomFactor := AValue;
    repaint;
  end;
end;

procedure TTileItem.Paint;
var
  w, h: Single;
  R: TRectF;

begin
  if Locked then
    Exit;

  w := Width * FZoomFactor;
  h := Height * FZoomFactor;
  R := RectF((Width - w) / 2, (Height - h) / 2, w, h);

  Canvas.Fill.Color := FBackgroundColor;
  Canvas.FillRect(R, 5, 5, AllCorners, AbsoluteOpacity);
end;

const
  ModernUIColors: Array [0 .. 10] of TAlphaColor = ($FFFF0097, $FF1BA1E2,
    $FFA200FF, $FF00ABA9, $FF8CBF26, $FFA05000, $FFE671B8, $FFF09609, $FFE51400,
    $FF339933, $FFFFFFFF);

procedure TForm3.FormCreate(Sender: TObject);
var
  i: integer;
  w, h: Single;
  item: TTileItem;
begin
  w := 150;
  h := HorzScrollBox1.Height - 20;

  for i := 0 to high(ModernUIColors) do
  begin
    item := TTileItem.Create(self);
    with item do
    begin
      BackgroundColor := ModernUIColors[i];
      Parent := HorzScrollBox1;
      Position.Point := PointF(i * 0.9 * w, 0);
      Width := w;
      Height := h;
    end;
  end;
end;

end.
  Mit Zitat antworten Zitat