Xamarin在Skiasharp帆布上形成位图坐标

发布于 2025-01-20 13:40:35 字数 2198 浏览 4 评论 0原文

我有一个简单的Xamarin表单页面,其中包含Skiasharp画布。我允许用户加载一个平面图作为画布的背景,并且可以添加较小的位图以在需要的地方拖动帆布。

我正在Microsoft页面上遵循此指南,以进行触摸操作。 https:https://学习。 microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/transforms/touch

如果我在单个设备大小上使用它,一切都可以很好地工作。当我以不同的设备尺寸启动时,分辨率和画布大小不同,并且导致较小的位图不一致。

这是我看到的结果。左侧是iPhone 13 Pro Max,右边是iPhone 13 Pro。如您所见,当我在不同尺寸的设备上查看它时,结果已经关闭。

这是包含SkCanvas的网格:

<Grid BackgroundColor="White" Grid.Row="1">
  <skia:SKCanvasView x:Name="canvasView" PaintSurface="OnCanvasViewPaintSurface" />
    <Grid.Effects>
      <tt:TouchEffect Capture="True" TouchAction="OnTouchEffectAction" />
    </Grid.Effects>
</Grid>

这是PaintSurface处理程序:

void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
{
  SKImageInfo info = args.Info;
  SKSurface surface = args.Surface;
  SKCanvas canvas = surface.Canvas;

  canvas.Clear();

  //set background
  SKRect dest = new SKRect(0, 0, info.Width, info.Height);
  canvas.DrawBitmap(backgroundBitmap, dest, BitmapStretch.Uniform,
                    BitmapAlignment.Start, BitmapAlignment.Start);

  //Set the coordinates for the pin bitmap
  var transX = 1005;
  var transY = 1189;
  bitmap.Matrix = SKMatrix.CreateTranslation(transX, transY);

  // Display the bitmap
  bitmap.Paint(canvas);

  // Display the matrix in the lower-right corner
  SKSize matrixSize = matrixDisplay.Measure(bitmap.Matrix);

  matrixDisplay.Paint(canvas, bitmap.Matrix,
                      new SKPoint(info.Width - matrixSize.Width,
                            info.Height - matrixSize.Height));
}

我在Stackoverflow以及MS论坛上都曾经浏览过几篇文章。我不知道在不同尺寸设备上始终如一地使该节目始终如一的方法。

是否有人建议如何将这个较小的位图保持在背景/SKCanvas的同一位置,而不管设备尺寸如何?

感谢任何反馈。

I have a simple Xamarin Forms page that contains a SkiaSharp Canvas. I am allowing the user to load a floor plan bitmap to use as the background for the Canvas, and they can add smaller bitmaps to be dragged around the canvas where they need.

I am following this guidance on the Microsoft page for touch manipulation.
https://learn.microsoft.com/en-us/xamarin/xamarin-forms/user-interface/graphics/skiasharp/transforms/touch

Everything works perfectly if I use it on a single device size. When I launch it on a different device size, the resolution and Canvas size are different, and it is causing the smaller bitmaps to be placed inconsistently.

Here is the result I am seeing. The left is an iPhone 13 Pro Max, and the right is a iPhone 13 Pro. As you can see, the result is off when I view it on different size devices.

Screenshot of iPhone 13 Pro Max and Pro

Here is the Grid that contains the SKCanvas:

<Grid BackgroundColor="White" Grid.Row="1">
  <skia:SKCanvasView x:Name="canvasView" PaintSurface="OnCanvasViewPaintSurface" />
    <Grid.Effects>
      <tt:TouchEffect Capture="True" TouchAction="OnTouchEffectAction" />
    </Grid.Effects>
</Grid>

Here is the PaintSurface handler:

void OnCanvasViewPaintSurface(object sender, SKPaintSurfaceEventArgs args)
{
  SKImageInfo info = args.Info;
  SKSurface surface = args.Surface;
  SKCanvas canvas = surface.Canvas;

  canvas.Clear();

  //set background
  SKRect dest = new SKRect(0, 0, info.Width, info.Height);
  canvas.DrawBitmap(backgroundBitmap, dest, BitmapStretch.Uniform,
                    BitmapAlignment.Start, BitmapAlignment.Start);

  //Set the coordinates for the pin bitmap
  var transX = 1005;
  var transY = 1189;
  bitmap.Matrix = SKMatrix.CreateTranslation(transX, transY);

  // Display the bitmap
  bitmap.Paint(canvas);

  // Display the matrix in the lower-right corner
  SKSize matrixSize = matrixDisplay.Measure(bitmap.Matrix);

  matrixDisplay.Paint(canvas, bitmap.Matrix,
                      new SKPoint(info.Width - matrixSize.Width,
                            info.Height - matrixSize.Height));
}

I've been through several posts on StackOverflow as well as in MS forums. I can't figure out a way to make this show consistently on different size devices.

Does anyone have a suggestion on how I can keep this smaller bitmap in the same location on the background/SKCanvas regardless of device size?

I appreciate any feedback.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

溺深海 2025-01-27 13:40:35

位图的坐标不应该是硬编码,您需要根据背景画布的大小来调整值(x/y)。

这样的东西

 var transX = info.Width - 20;
 var transY = info.Height - 20;

The coordinate of the bitmap is not supposed to be hard-coded , you need to adjust the value (x/y) according to the background canvas's size.

Something like this

 var transX = info.Width - 20;
 var transY = info.Height - 20;
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文