加载中…
正文 字体大小:

TCustomMultiResBitmap.ItemByScale提升版本

(2016-07-27 07:48:39)
分类: Delphi10.1
QC有人提出的改进版本,测试真是提升fmx可视控件的效率。
复制FMX.MultiResBitmap.pas单元文件到自己的项目目录,打开并找到这个方法,用下面的代码替换:

function TCustomMultiResBitmap.ItemByScale(const AScale: Single; const ExactMatch: Boolean; const IncludeEmpty: Boolean):TCustomBitmapItem;
var
  LScale, LCurrentScale: Single;
  I, LCount: Integer;
  LScaleArray: TArray<Single>;//这里sina blog不认,我改全角字符,你用的话,需要改过来。
begin
  LScale := RoundTo(AScale, TCustomBitmapItem.ScaleRange);
  Result := nil;
  if not ExactMatch then
  begin
    //-----
    //i do this step below because ScaleArray is little slow (around 0.1 ms on android dependly of the quality of the phone)
    //but in most of the case we don't need it because common resolution are only 1x, 1.5x, 2x, 3x, 4x
    //and in most of the case (at least in the style) we already have such resolutions, this will reduce the speed
    //to 0.01 ms and it's particularly important to be fast here because this function is called on each paint of styled controls
    for I := 0 to Count - 1 do
      if SameValue(LScale, Items[I].Scale, TEpsilon.Scale) and (IncludeEmpty or (not Items[I].IsEmpty)) then
      begin
        Result := Items[I];
        Exit;
      end;
    //-----
    LScaleArray := ScaleArray(IncludeEmpty);
    LCount := Length(LScaleArray);
    if LCount > 0 then
    begin
      I := -1;
      repeat
        Inc(I)
      until (LScale < LScaleArray[I]) or (I >= LCount - 1);
      if I > 0 then
      begin
        LCurrentScale := LScaleArray[I - 1] + (LScaleArray[I] - LScaleArray[I - 1]) / 5;
        if LScale <= LCurrentScale then
          Dec(I);
      end;
      LScale := LScaleArray[I];
    end;
  end;
  for I := 0 to Count - 1 do
    if SameValue(LScale, Items[I].Scale, TEpsilon.Scale) and (IncludeEmpty or (not Items[I].IsEmpty)) then
    begin
      Result := Items[I];
      Exit;
    end
end;

后记:QDAC作者提供了一个更好的版本,同时对该方法做了进一步的性能分析,在这里,可以去看看。不得不佩服王大侠!

0

阅读 评论 收藏 转载 喜欢 打印举报
  • 评论加载中,请稍候...
发评论

    发评论

    以上网友发言只代表其个人观点,不代表新浪网的观点或立场。

      

    新浪BLOG意见反馈留言板 电话:4006900000 提示音后按1键(按当地市话标准计费) 欢迎批评指正

    新浪简介 | About Sina | 广告服务 | 联系我们 | 招聘信息 | 网站律师 | SINA English | 会员注册 | 产品答疑

    新浪公司 版权所有