首页 > 解决方案 > FMX 中的径向渐变问题

问题描述

我正在尝试重现此处给出的渐变演示 http://docwiki.embarcadero.com/CodeExamples/Sydney/en/FMXGradient_(Delphi)

我已经完全复制了所有内容并进行了调试以确认函数被调用。线性渐变效果很好。但是,当我设置 RotationCenter.X 或 RotationCenter.Y 时,径向渐变不会改变,如网页所示。我没有找零钱。但是,当我改变 RotationAngle 时,我确实得到了改变。我之前注意到在 TTransform 中设置 RotationCenter 似乎什么都不做。查看 FMX.Types 中的 TTransform 类,我发现根本没有使用 RotationCenter。我正在使用 Delphi 10.4 悉尼。这是自网页上的演示创建以来潜入的代码更改吗?

我尝试设置 TTransform.Position 而不是 RotationCenter 因为我可以看到它被使用了。然而,这似乎也没有任何作用,我可以在 FMX.Types 中看到 SetPosition 没有调用 MatrixChanged()。总之,它对我来说似乎很糟糕,或者至少与演示不同。其他人可以确认并建议我如何让它工作吗?

单位来源

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.Objects,
  FMX.StdCtrls, FMX.Edit, FMX.ListBox, FMX.Colors, FMX.Controls.Presentation;

type
  TForm1 = class(TForm)
    Rectangle1: TRectangle;
    Ellipse1: TEllipse;
    Label1: TLabel;
    Label2: TLabel;
    Label3: TLabel;
    Label4: TLabel;
    Label5: TLabel;
    Edit1: TEdit;
    Edit2: TEdit;
    Edit3: TEdit;
    Edit4: TEdit;
    ComboBox1: TComboBox;
    ListBoxItem1: TListBoxItem;
    ListBoxItem2: TListBoxItem;
    ColorComboBox1: TColorComboBox;
    ColorComboBox2: TColorComboBox;
    procedure ColorComboBox1Change(Sender: TObject);
    procedure ColorComboBox2Change(Sender: TObject);
    procedure Edit1Change(Sender: TObject);
    procedure Edit2Change(Sender: TObject);
    procedure Edit3Change(Sender: TObject);
    procedure Edit4Change(Sender: TObject);
    procedure FormCreate(Sender: TObject);
    procedure ListBoxItem1Click(Sender: TObject);
    procedure ListBoxItem2Click(Sender: TObject);
    procedure ComboBox1Change(Sender: TObject);
  private
    { Private declarations }
  public
    { Public declarations }
  end;

var
  Form1: TForm1;

implementation

{$R *.fmx}

procedure TForm1.ColorComboBox1Change(Sender: TObject);
begin
  if Ellipse1.Visible then
    Ellipse1.Fill.Gradient.Color := ColorComboBox1.Color
  else
    Rectangle1.Fill.Gradient.Color := ColorComboBox1.Color;
end;

procedure TForm1.ColorComboBox2Change(Sender: TObject);
begin
  if Ellipse1.Visible then
    Ellipse1.Fill.Gradient.Color1 := ColorComboBox2.Color
  else
    Rectangle1.Fill.Gradient.Color1 := ColorComboBox2.Color;
end;

procedure TForm1.Edit1Change(Sender: TObject);
begin
  if Ellipse1.Visible then
    Ellipse1.Fill.Gradient.RadialTransform.RotationAngle :=
      StrToFloat(Edit1.Text)
  else
    Rectangle1.Fill.Gradient.StartPosition.X := StrToFloat(Edit1.Text);
end;

procedure TForm1.Edit2Change(Sender: TObject);
begin
  if Ellipse1.Visible then
    Ellipse1.Fill.Gradient.RadialTransform.RotationCenter.X :=
      StrToFloat(Edit2.Text)
  else
    Rectangle1.Fill.Gradient.StartPosition.Y := StrToFloat(Edit2.Text);
end;

procedure TForm1.Edit3Change(Sender: TObject);
begin
  if Ellipse1.Visible then
    Ellipse1.Fill.Gradient.RadialTransform.RotationCenter.Y :=
      StrToFloat(Edit3.Text)
  else
    Rectangle1.Fill.Gradient.StopPosition.X := StrToFloat(Edit3.Text);
end;

procedure TForm1.Edit4Change(Sender: TObject);
begin
  Rectangle1.Fill.Gradient.StopPosition.Y := StrToFloat(Edit4.Text);
end;

procedure TForm1.FormCreate(Sender: TObject);
begin
  Ellipse1.Visible := False;
  Rectangle1.Visible := False;

  // Fill initialization so Gradient can be applied
  Ellipse1.Fill.Kind := TBrushKind.Gradient;
  Rectangle1.Fill.Kind := TBrushKind.Gradient;

  Ellipse1.Fill.Gradient.Style := TGradientStyle.Radial;
  Rectangle1.Fill.Gradient.Style := TGradientStyle.Linear;
  Label1.Text := 'Choose the gradient type';
  ListBoxItem1.Text := 'Linear';
  ListBoxItem2.Text := 'Radial';
  Label2.Text := '';
  Label3.Text := '';
  Label4.Text := '';
  Label5.Text := '';
  Edit1.Visible := False;
  Edit2.Visible := False;
  Edit3.Visible := False;
  Edit4.Visible := False;
end;

procedure TForm1.ComboBox1Change(Sender: TObject);
begin
  if ComboBox1.Selected.Text = 'Linear' then ListBoxItem1Click(Self) else
  if ComboBox1.Selected.Text = 'Radial' then ListBoxItem2Click(Self);
end;

procedure TForm1.ListBoxItem1Click(Sender: TObject);
begin
  Label2.Text := 'StartPosition.X: ';
  Label3.Text := 'StartPosition.Y: ';
  Label4.Text := 'StopPosition.X: ';
  Label5.Text := 'StopPosition.Y: ';
  Edit1.Visible := True;
  Edit2.Visible := True;
  Edit3.Visible := True;
  Edit4.Visible := True;
  Ellipse1.Visible := False;
  Rectangle1.Visible := True;

  // Set the Gradient colors
  Rectangle1.Fill.Gradient.Color1 := ColorComboBox2.Color;
  Rectangle1.Fill.Gradient.Color := ColorComboBox1.Color;
  // Populate the Edit Boxes
  Edit1.Text := FloatToStr(Rectangle1.Fill.Gradient.StartPosition.X);
  Edit2.Text := FloatToStr(Rectangle1.Fill.Gradient.StartPosition.Y);
  Edit3.Text := FloatToStr(Rectangle1.Fill.Gradient.StopPosition.X);
  Edit4.Text := FloatToStr(Rectangle1.Fill.Gradient.StopPosition.Y);
end;

procedure TForm1.ListBoxItem2Click(Sender: TObject);
begin
  Label2.Text := 'RotationAngle:';
  Label3.Text := 'RotationCenter.X:';
  Label4.Text := 'RotationCenter.Y:';
  Label5.Text := '';
  Edit1.Visible := True;
  Edit2.Visible := True;
  Edit3.Visible := True;
  Edit4.Visible := False;
  Ellipse1.Visible := True;
  Rectangle1.Visible := False;

  // Set the Gradient colors
  Ellipse1.Fill.Gradient.Color1 := ColorComboBox2.Color;
  Ellipse1.Fill.Gradient.Color := ColorComboBox1.Color;

  // Populate the edit boxes
  Edit1.Text := FloatToStr(Ellipse1.Fill.Gradient.RadialTransform.
    RotationAngle);
  Edit2.Text := FloatToStr(Ellipse1.Fill.Gradient.RadialTransform.
    RotationCenter.X);
  Edit3.Text := FloatToStr(Ellipse1.Fill.Gradient.RadialTransform.
    RotationCenter.Y);

end;

end.

表单源

object Form1: TForm1
  Left = 0
  Top = 0
  Caption = 'Form1'
  ClientHeight = 480
  ClientWidth = 640
  FormFactor.Width = 320
  FormFactor.Height = 480
  FormFactor.Devices = [Desktop]
  OnCreate = FormCreate
  DesignerMasterStyle = 0
  object Rectangle1: TRectangle
    Position.X = 184.000000000000000000
    Position.Y = 8.000000000000000000
    Size.Width = 449.000000000000000000
    Size.Height = 465.000000000000000000
    Size.PlatformDefault = False
  end
  object Ellipse1: TEllipse
    Position.X = 184.000000000000000000
    Position.Y = 8.000000000000000000
    Size.Width = 449.000000000000000000
    Size.Height = 465.000000000000000000
    Size.PlatformDefault = False
  end
  object Label1: TLabel
    Position.X = 24.000000000000000000
    Position.Y = 40.000000000000000000
    Size.Width = 177.000000000000000000
    Size.Height = 17.000000000000000000
    Size.PlatformDefault = False
    Text = 'Choose the gradient type'
  end
  object Label2: TLabel
    Position.X = 32.000000000000000000
    Position.Y = 208.000000000000000000
    Text = 'Label2'
  end
  object Label3: TLabel
    Position.X = 32.000000000000000000
    Position.Y = 264.000000000000000000
    Text = 'Label3'
  end
  object Label4: TLabel
    Position.X = 32.000000000000000000
    Position.Y = 312.000000000000000000
    Text = 'Label4'
  end
  object Label5: TLabel
    Position.X = 32.000000000000000000
    Position.Y = 368.000000000000000000
    Text = 'Label5'
  end
  object Edit1: TEdit
    Touch.InteractiveGestures = [LongTap, DoubleTap]
    Position.X = 72.000000000000000000
    Position.Y = 224.000000000000000000
    OnChange = Edit1Change
  end
  object Edit2: TEdit
    Touch.InteractiveGestures = [LongTap, DoubleTap]
    Position.X = 72.000000000000000000
    Position.Y = 280.000000000000000000
    OnChange = Edit2Change
  end
  object Edit3: TEdit
    Touch.InteractiveGestures = [LongTap, DoubleTap]
    Position.X = 72.000000000000000000
    Position.Y = 336.000000000000000000
    OnChange = Edit3Change
  end
  object Edit4: TEdit
    Touch.InteractiveGestures = [LongTap, DoubleTap]
    Position.X = 72.000000000000000000
    Position.Y = 392.000000000000000000
    OnChange = Edit4Change
  end
  object ComboBox1: TComboBox
    Position.X = 16.000000000000000000
    Position.Y = 72.000000000000000000
    Size.Width = 161.000000000000000000
    Size.Height = 22.000000000000000000
    Size.PlatformDefault = False
    OnChange = ComboBox1Change
    object ListBoxItem1: TListBoxItem
      Text = 'ListBoxItem1'
      OnClick = ListBoxItem1Click
    end
    object ListBoxItem2: TListBoxItem
      Text = 'ListBoxItem2'
      OnClick = ListBoxItem2Click
    end
  end
  object ColorComboBox1: TColorComboBox
    DropDownKind = Custom
    Color = claNull
    DisableFocusEffect = False
    Position.X = 16.000000000000000000
    Position.Y = 112.000000000000000000
    Size.Width = 161.000000000000000000
    Size.Height = 22.000000000000000000
    Size.PlatformDefault = False
    OnChange = ColorComboBox1Change
  end
  object ColorComboBox2: TColorComboBox
    DropDownKind = Custom
    Color = claNull
    DisableFocusEffect = False
    Position.X = 16.000000000000000000
    Position.Y = 144.000000000000000000
    Size.Width = 161.000000000000000000
    Size.Height = 22.000000000000000000
    Size.PlatformDefault = False
    OnChange = ColorComboBox2Change
  end
end

标签: delphifiremonkey

解决方案


推荐阅读