Stretching a Custom View

Nathan Sokalski 4,111 Reputation points
2022-02-26T19:36:24.317+00:00

I have a custom view (inherits from View) that overrides OnDraw. In this override, I make multiple calls to DrawPath, similar to the following:

canvas.DrawPath(PathParser.CreatePathFromPathData($"M0 {y}H{this.Bids.Count - 1}"), linepaint);

However, because my values for the path data are relatively small, I want to clip the Canvas to the area containing my values, and then have it stretched in the designer. Intellisense claims there is a method named SetViewport, but that causes the Designer to give a java.lang.NoSuchMethodError (which seems correct, since I could not find any documentation for SetViewport, so I don't know why intellisense lists it). If I create a VectorDrawable resource with the appropriate viewportWidth & viewportHeight, it does what I want, but because I need to dynamically determine the path, using a static a VectorDrawable resource will not work. How can I use OnDraw (or some other method, I don't care if I create a custom view or do it some other way) to use path data with dynamic values and be able to stretch it?

Developer technologies | .NET | Xamarin
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,971 Reputation points
    2022-02-28T07:56:36.71+00:00

    Intellisense claims there is a method named SetViewport, but that causes the Designer to give a java.lang.NoSuchMethodError

    Hi, njsokalski. Canvas provides the SetViewport method, you could use it directly in OnDraw method.

    protected override void OnDraw(Canvas canvas)
    {
        base.OnDraw(canvas);
        canvas.SetViewport(width, height);
    }
    

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.