4

我之前问过这个问题并删除了,只是因为 1)这似乎比我想做的工作要多,2)我问我的问题很糟糕并且它被关闭了。但是,在做了更多研究之后,我决定重新审视这个功能/问题/操作方法

我正在尝试/想要创建一个模糊的叠加层,如下图所示。使用的明显 FMX.effect 将是“模糊”效果。我的问题是:如何渲染覆盖层将覆盖的图像,或者以有效的方式复制图像以模糊覆盖层?

我曾考虑过只使用两个相同的位图,一个用于背景,一个用于模糊,但这样我就不会在原始背景之上捕获控件或其他任何东西的“模糊性”。我还认为,如果我让叠加层滚动进出视图,那么它不会像我想要的那样看起来/出现。

考虑到上述情况,这一切都让我相信我需要在覆盖滚动/进入视野时动态捕捉要模糊的背景。如何在 Delphi XE6 中执行此操作并捕获当前显示的屏幕内容?甚至不知道从哪里开始。

我不拥有图像*

在此处输入图像描述

4

2 回答 2

7

在对如何捕获父控件背景进行了一些研究之后,我根据 FMX 的 TMagnifierGlass 类提出了以下代码(注意我在 XE5 中制作了此代码,您需要检查 XE6 兼容性):

TGlass = class(TControl)
private
  FBlur: TBlurEffect;
  FParentScreenshotBitmap: TBitmap;
  function GetSoftness: Single;
  procedure SetSoftness(Value: Single);
protected
  procedure Paint; override;
public
  constructor Create(AOwner: TComponent);
  destructor Destroy; override;
  property Softness: Single read GetSoftness write SetSoftness;
end;


{ TGlass }

constructor TGlass.Create(AOwner: TComponent);
begin

  inherited Create(AOwner);

  // Create parent background
  FParentScreenshotBitmap := TBitmap.Create(0, 0);

  // Create blur
  FBlur := TBlurEffect.Create(nil);
  FBlur.Softness := 0.6;

end;

destructor TGlass.Destroy;
begin

  FBlur.Free;
  FParentScreenshotBitmap.Free;

  inherited Destroy;

end;

function TGlass.GetSoftness: Single;
begin

  Result := FBlur.Softness;

end;

procedure TGlass.SetSoftness(Value: Single);
begin

  FBlur.Softness := Value;

end;

procedure TGlass.Paint;
var
  ParentWidth: Single;
  ParentHeight: Single;

  procedure DefineParentSize;
  begin
    ParentWidth := 0;
    ParentHeight := 0;
    if Parent is TCustomForm then
    begin
      ParentWidth := (Parent as TCustomForm).ClientWidth;
      ParentHeight := (Parent as TCustomForm).ClientHeight;
    end;
    if Parent is TControl then
    begin
      ParentWidth := (Parent as TControl).Width;
      ParentHeight := (Parent as TControl).Height;
    end;
  end;

  function IsBitmapSizeChanged(ABitmap: TBitmap; const ANewWidth, ANewHeight: Single): Boolean;
  begin
    Result := not SameValue(ANewWidth * ABitmap.BitmapScale, ABitmap.Width) or
              not SameValue(ANewHeight * ABitmap.BitmapScale, ABitmap.Height);
  end;

  procedure MakeParentScreenshot;
  var
    Form: TCommonCustomForm;
    Child: TFmxObject;
    ParentControl: TControl;
  begin
    if FParentScreenshotBitmap.Canvas.BeginScene then
      try
        FDisablePaint := True;
        if Parent is TCommonCustomForm then
        begin
          Form := Parent as TCommonCustomForm;
          for Child in Form.Children do
            if (Child is TControl) and (Child as TControl).Visible then
            begin
              ParentControl := Child as TControl;
              ParentControl.PaintTo(FParentScreenshotBitmap.Canvas, ParentControl.ParentedRect);
            end;
        end
        else
          (Parent as TControl).PaintTo(FParentScreenshotBitmap.Canvas, RectF(0, 0, ParentWidth, ParentHeight));
      finally
        FDisablePaint := False;
        FParentScreenshotBitmap.Canvas.EndScene;
      end;
  end;

begin

  // Make screenshot of Parent control
  DefineParentSize;
  if IsBitmapSizeChanged(FParentScreenshotBitmap, ParentWidth, ParentHeight) then
    FParentScreenshotBitmap.SetSize(Round(ParentWidth), Round(ParentHeight));
  MakeParentScreenshot;

  // Apply glass effect
  Canvas.BeginScene;
  try
    FBlur.ProcessEffect(Canvas, FParentScreenshotBitmap, FBlur.Softness);
    Canvas.DrawBitmap(FParentScreenshotBitmap, ParentedRect, LocalRect, 1, TRUE);
  finally
    Canvas.EndScene;
  end;

end;

要使用,只需在任何控件之上实例化 TGlass,它应该会产生您正在寻找的所需“玻璃状”效果

于 2014-05-28T00:13:10.903 回答
2

我创建了一个 TFrostGlass 组件,它还可以:

  • 缓存
  • 有背景颜色
  • 圆角
  • 边界

你可以在这里查看/下载它:https ://github.com/Spelt/Frost-Glass

于 2017-03-05T15:40:23.317 回答