drawing on bitmap using skiasharp in xamarin

2019-08-16 06:23发布

I have a bitmap which I am placing on canvas using SKRect as the size of image is large. I am trying to draw on the bitmap but the touching co-ordinates don't match with the bitmap co-ordinates (i.e.the touch on canvas and the drawn curves are different from each other) or I am touching somewhere and the drawn curve is on different co-ordinates. Here is the code I am trying.

Any help on this would be very helpful. Thanks

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条回答
登录 后发表回答