动态创建组件时TFloatAnimation遗留残影问题求助
修复FireMonkey快速FloatAnimation的残影问题
针对你遇到的鼠标离开矩形后出现残影的问题,以下是几个无需延长动画时长的可行修复方案:
方案1:启用控件双缓冲
FireMonkey控件默认未开启双缓冲,快速动画时开启双缓冲可以避免绘制过程中的中间帧残留。在创建矩形时添加DoubleBuffered := True;:
procedure TForm1.Button1Click(Sender: TObject); var Rectangle:TRectangle; Animation: TFloatAnimation; begin Rectangle := TRectangle.Create(Self); With Rectangle do begin Parent := Form1; Height := 65; Width := 65; DoubleBuffered := True; // 启用双缓冲 end; Animation := TFloatAnimation.Create(Rectangle); with Animation do begin Parent := Rectangle; PropertyName := 'Size.Width'; StartValue := 65; StopValue := 120; Trigger := 'IsMouseOver=true'; TriggerInverse := 'IsMouseOver=false'; Interpolation := TInterpolationType.Cubic; end; end;
方案2:动画结束后强制重绘
在动画的OnFinish事件中调用控件的Repaint方法,确保反向动画结束后立刻刷新控件,清除残留像素:
procedure TForm1.Button1Click(Sender: TObject); var Rectangle:TRectangle; Animation: TFloatAnimation; begin Rectangle := TRectangle.Create(Self); With Rectangle do begin Parent := Form1; Height := 65; Width := 65; end; Animation := TFloatAnimation.Create(Rectangle); with Animation do begin Parent := Rectangle; PropertyName := 'Size.Width'; StartValue := 65; StopValue := 120; Trigger := 'IsMouseOver=true'; TriggerInverse := 'IsMouseOver=false'; Interpolation := TInterpolationType.Cubic; OnFinish := procedure(Sender: TObject) begin Rectangle.Repaint; // 动画结束后强制重绘 end; end; end;
方案3:改用TScaleAnimation替代尺寸动画
直接修改控件尺寸可能导致渲染时的布局重绘压力,改用缩放动画(基于控件变换矩阵),FireMonkey对这类动画的渲染优化更好,能减少残影:
procedure TForm1.Button1Click(Sender: TObject); var Rectangle:TRectangle; Animation: TScaleAnimation; begin Rectangle := TRectangle.Create(Self); With Rectangle do begin Parent := Form1; Height := 65; Width := 65; Anchors := [akLeft, akTop]; // 固定锚点避免位置偏移 end; Animation := TScaleAnimation.Create(Rectangle); with Animation do begin Parent := Rectangle; Scale.X := 1.85; // 120/65≈1.85,对应宽度从65到120的缩放比例 Trigger := 'IsMouseOver=true'; TriggerInverse := 'IsMouseOver=false'; Interpolation := TInterpolationType.Cubic; end; end;
方案4:调整插值类型
Cubic插值会导致动画前后阶段速度变化大,快速动画时容易出现帧跳跃。换成线性插值(TInterpolationType.Linear)可以让帧变化更均匀,降低渲染压力:
Interpolation := TInterpolationType.Linear;
内容的提问来源于stack exchange,提问作者Jayden404
相关产品推荐
相关产品推荐

