在xamarin中使用skiasharp在位图上绘制

时间:2019-03-24 14:55:46

标签: xamarin.forms skiasharp

我有一个位图,由于图像尺寸大,我将使用SKRect将该位图放置在画布上。 我试图在位图上绘制,但是接触坐标与位图坐标不匹配(即画布上的触摸和绘制的曲线彼此不同),或者我在某处触摸并且绘制的曲线是在不同的坐标上。 这是我正在尝试的代码。

对此的任何帮助将非常有帮助。谢谢

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

    float scale = Math.Min((float)info.Width / bitmap.Width, 
                           (float)info.Height / bitmap.Height);
    float x = (info.Width - scale * bitmap.Width) / 2;
    float y = (info.Height - scale * bitmap.Height) / 2;
    SKRect destRect = new SKRect(x, y, x + scale * bitmap.Width, 
                                       y + scale * bitmap.Height);

    canvas.DrawBitmap(bitmap, destRect);

     foreach (SKPath path in completedPaths)
    {
        canvas.DrawPath(path, paint);
    }

    foreach (SKPath path in inProgressPaths.Values)
    {
        canvas.DrawPath(path, paint);
    }
}

void OnTouchEffectAction(object sender, TouchActionEventArgs args)
{
    switch (args.Type)
    {
        case TouchActionType.Pressed:
            if (!inProgressPaths.ContainsKey(args.Id))
            {
                SKPath path = new SKPath();
                path.MoveTo((args.Location));
                inProgressPaths.Add(args.Id, path);
                canvasView.InvalidateSurface();
            }
            break;

        case TouchActionType.Moved:
            if (inProgressPaths.ContainsKey(args.Id))
            {
                SKPath path = inProgressPaths[args.Id];
                path.LineTo((args.Location));
                canvasView.InvalidateSurface();
            }
            break;

        case TouchActionType.Released:
            if (inProgressPaths.ContainsKey(args.Id))
            {
                completedPaths.Add(inProgressPaths[args.Id]);
                inProgressPaths.Remove(args.Id);
                canvasView.InvalidateSurface();
            }
            break;

        case TouchActionType.Cancelled:
        if (inProgressPaths.ContainsKey(args.Id))
            {
                inProgressPaths.Remove(args.Id);
                canvasView.InvalidateSurface();
            }
            break;
    }
}

0 个答案:

没有答案