首页 > 解决方案 > SkiaSharp 在可点击的路径上制作位图

问题描述

我已经绘制了一个圆形路径,并且每 1 度角添加一个位图。现在,我想使用 Xamarin 表单收听每个位图单击该圆形路径!我怎样才能实现这一点。我已经在 xamarin 和 amble 上进行操作以获取触摸坐标,但无法准确获取选择了哪个位图?

编辑:

 foreach (var item in bitmapPoints)
  {
    using (Stream streami = assembly.GetManifestResourceStream(resID))
    {
      redBitmap = SKBitmap.Decode(streami);
    }
      canvas.DrawBitmap(redBitmap,item, null);
  }

现在我正在尝试为画布上绘制的每个位图启用点击事件。我应该制作具有 HiTest 方法的自定义 BitMap 类吗?如果是这样,应该通过什么 Rect x 和 y 来获取 HiTest?

标签: xamarinskiasharpxamarin-forms-4

解决方案


这被称为“命中测试” - Xamarin在他们的 SkiaSharp 文档中有一个示例

public bool HitTest(SKPoint location)
{
    // Invert the matrix
    SKMatrix inverseMatrix;

    if (Matrix.TryInvert(out inverseMatrix))
    {
        // Transform the point using the inverted matrix
        SKPoint transformedPoint = inverseMatrix.MapPoint(location);

        // Check if it's in the untransformed bitmap rectangle
        SKRect rect = new SKRect(0, 0, bitmap.Width, bitmap.Height);
        return rect.Contains(transformedPoint);
    }
    return false;
}

推荐阅读