Dismiss Notice

REGISTRATION IS AFTER ADMIN ACTIVATION

DONATIONS WITH PAYPAL CLICK TO BUTTON

1 MONTH VIP - 5$; 3 MONTHS VIP - 10$; 6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$

DONATIONS WITH Bitcoin Address:3NRs3CK3fhXifrNYxHEZKpETDd9vNLMsMD

Dismiss Notice
The registration is approved by the Administrator. It takes about 1 day to approve your registration
Dismiss Notice
For open hidden message no need write thanks, thank etc. Enough is click to like button on right side of thread.

Using TCanvas in Delphi for Android

Discussion in 'Delphi Android And IOS' started by AdminDF, Feb 10, 2014.

  1. AdminDF
    Online

    AdminDFAdminDF is a Verified Member DelphiFan Administrator Staff Member DF Staff

    Drawing on TCanvas in Delphi XE5 for Android turned out to have some special aspects which at first left me in doubt and I want to share my experience.

    Let’s draw some parallel lines.

    Here I’d like to digress and notice that on Windows Stroke.Kind value is bkSolid by default, but on Android it’s bkNone. Thus, if you haven’t defined Stroke.Kind value, these lines will be visible on Windows, but will not on Android. I have no idea why they chose this approach.


    Code:
    procedure TForm2.FormPaint(Sender: TObject; Canvas: TCanvas;
      const ARect: TRectF);
    var
      I: Integer;
    begin
      if Canvas.BeginScene then
        try
          Canvas.Stroke.Thickness := 1.5;
          Canvas.Stroke.Kind := TBrushKind.bkSolid;
          Canvas.Fill.Color := TAlphaColorRec.Black;
          Canvas.Fill.Kind := TBrushKind.bkSolid;
     
          for I := 1 to 9 do
            Canvas.DrawLine(PointF(50 + I * 25, 0), PointF(50 + I * 25, ClientHeight), 1);
        finally
          Canvas.EndScene;
        end;
    end;
    


    [​IMG]

    Obviously, some lines are thicker than others. On Windows the same code works just perfectly.

    The reason is that unlike it does on Windows, the logical pixel on Android is not always equal to physical pixel. And if a line appears to be «between» physical pixels, it has to be blurred on neighboring pixels. It is a trade-off between accuracy and quality of rendering.

    If we still want to draw equal lines, we could move them by the half of their thikness to ensure getting the appropriate physical pixels.

    That’s how TLine and its ancestor TShape solve the problem:


    Code:
    function TShape.GetShapeRect: TRectF;
    begin
      Result := LocalRect;
      if FStroke.Kind <> TBrushKind.bkNone then
        InflateRect(Result, -(FStroke.Thickness / 2), -(FStroke.Thickness / 2));
    end;
     
    procedure TLine.Paint;
    begin
      case FLineType of
        TLineType.ltTop:
          Canvas.DrawLine(GetShapeRect.TopLeft, PointF(GetShapeRect.Right, GetShapeRect.Top),
            AbsoluteOpacity, FStroke);
        TLineType.ltLeft:
          Canvas.DrawLine(GetShapeRect.TopLeft, PointF(GetShapeRect.Left, GetShapeRect.Bottom),
            AbsoluteOpacity, FStroke);
      else
        Canvas.DrawLine(GetShapeRect.TopLeft, GetShapeRect.BottomRight, AbsoluteOpacity, FStroke);
      end;
    end;
    

    Making the appropriate changes we can draw equal lines too:


    Code:
    procedure TForm2.FormPaint(Sender: TObject; Canvas: TCanvas;
      const ARect: TRectF);
    var
      I: Integer;
    begin
      if Canvas.BeginScene then
        try
          Canvas.Stroke.Thickness := 1.5;
          Canvas.Stroke.Kind := TBrushKind.bkSolid;
          Canvas.Fill.Color := TAlphaColorRec.Black;
          Canvas.Fill.Kind := TBrushKind.bkSolid;
     
          for I := 1 to 9 do
          begin
            Canvas.DrawLine(PointF(50 + I * 25 - (Canvas.Stroke.Thickness / 2), 0),
              PointF(50 + I * 25 - (Canvas.Stroke.Thickness / 2), ClientHeight), 1);
          end;
        finally
          Canvas.EndScene;
        end;
    end;
    

    The result is:

    [​IMG]

    Much better :)

    This can’t be done automatically: in this case it will «jump» during an animation. But anyways I’d like to have some flag to choose between accuracy and quality. It’s quite boring to do this calculation manually.

    Update


    Code:
    function TForm2.RoundLogicPointsToMatchPixel(const LogicPoints: Single;
      const AtLeastOnePixel: Boolean = False): Single;
    var
      ws: IFMXWindowService;
      ScreenScale, Pixels: Single;
    begin
      ws := TPlatformServices.Current.GetPlatformService(IFMXWindowService) as IFMXWindowService;
      ScreenScale := ws.GetWindowScale(Self);
     
      // Maybe you will want to use Ceil or Trunc instead of Round
      Pixels := Round(LogicPoints * ScreenScale);
     
      if (Pixels < 1) and (AtLeastOnePixel) then
        Pixels := 1.0;
     
      Result := Pixels / ScreenScale;
    end;
     
    procedure TForm2.FormPaint(Sender: TObject; Canvas: TCanvas;
      const ARect: TRectF);
    var
      I: Integer;
    begin
      if Canvas.BeginScene then
        try
          Canvas.Stroke.Thickness := RoundLogicPointsToMatchPixel(1.0, True);
          Canvas.Stroke.Kind := TBrushKind.bkSolid;
          Canvas.Fill.Color := TAlphaColorRec.Black;
          Canvas.Fill.Kind := TBrushKind.bkSolid;
     
          for I := 1 to 9 do
            Canvas.DrawLine(PointF(RoundLogicPointsToMatchPixel(50 + I * 25), 0),
              PointF(RoundLogicPointsToMatchPixel(50 + I * 25), ClientHeight), 1);
        finally
          Canvas.EndScene;
        end;
    end;
    
     
  2. Ratthain
    Offline

    Ratthain DF Member

    Could you please show me example code to draw 3d multiple lines in buffer. and render them back on the screen.

    Thank you.!:angel:
     
  3. gamal97
    Offline

    gamal97 Guest

    could you use clblack and clred?
     
     
  4. sismaxi
    Offline

    sismaxi DF Junior

    thanks for sharing searched the internet and had not found.
     

Share This Page

Laws
Our website, law, laws, copyright and aims to be respectful of individual rights. Our site, as defined in the law of 5651 serves as a provider of space. According to the law, contrary to the law of site management has no obligation to monitor content. Therefore, our site has adopted the principle of fit and remove. Works are subject to copyright laws and is shared in a manner that violated their legal rights, or professional associations, rights holders who, adsdelphi@gmail.com I can reach us at e-mail address. Complaints considered to be infringing on the examination of the content will be removed from our site.
Donate Help To Us and Be VIP
DONATIONS WITH PAYPAL CLICK TO BUTTON
6 MONTHS VIP - 20$; 1 YEAR VIP - 30$; 2 YEARS VIP - 50$; GOLD USER FOR LIFE VIP - 150$
Social Block