Pinch and Zoom UWP default position and size

喜你入骨 提交于 2020-01-06 08:07:10

问题


I drawn some arc and lines in a canvas using WIN2D.To implements pan and pinch in the canvas image I was used the code below...

XAML

                    </Canvas>-->
                <ScrollViewer x:Name="scrollViewer"
                  VerticalScrollBarVisibility="Auto"
                  HorizontalScrollBarVisibility="Auto"
                              Margin="-0,0,0,0"
                              Height="806"
                  ZoomMode="Enabled"
                  MinZoomFactor="0.5"
                  MaxZoomFactor="8000"
                  ViewChanged="scrollViewer_ViewChanged">
                    <Grid  Margin="-10,0,0,0">

                        <canvas:CanvasControl  x:Name="ManipulateMe" 
                             Draw="Preview_Draw"
                                 Margin="-11,1,0,0"
                             IsDoubleTapEnabled="True"
                             IsTapEnabled="True" 

                             DoubleTapped="Preview_DoubleTapped"
                         Height="855" 
                         Width="427.5">

                    </canvas:CanvasControl>
                    </Grid>
                </ScrollViewer>
                <!--</controls:ManipulationContentControl>-->
            </Canvas>

.CS

private void Preview_Draw(Microsoft.Graphics.Canvas.UI.Xaml.CanvasControl sender, Microsoft.Graphics.Canvas.UI.Xaml.CanvasDrawEventArgs args) { try

        {
            string GCodefile = ((ltvSelectExecution).SelectedItem as FolderFilesInfo).Name + ((ltvSelectExecution).SelectedItem as FolderFilesInfo).Extension;
            var EntityArcLines = execution.GetArcLinesParams(GCodefile, null, false);
            var pathGeometric = execution.GetPathGeometry(EntityArcLines);
            int flag;

            CanvasPathBuilder pathBuilder = new CanvasPathBuilder(ManipulateMe.Device);
            pathGeometric.ForEach(t =>
            {
                Vector2 finalPoints = new Vector2()
                {
                    X = (float)t.Xf,
                    Y = (float)t.Yf
                };
                float r = (float)Math.Sqrt(Math.Pow(t.I, 2) + Math.Pow(t.J, 2));
                float x = 7;
                float y = 7;
                var clockwise = t.DirectionRotate;


                CanvasSweepDirection canvasSweepDirection = clockwise == 0 ? CanvasSweepDirection.CounterClockwise : CanvasSweepDirection.Clockwise;
                flag = t.G == 0 ? 0 : 1;
                if (t.I != 0 || t.J != 0)
                {
                    pathBuilder.BeginFigure((float)t.Xi, (float)t.Yi);
                    pathBuilder.AddArc(finalPoints, x, y, (float)0, canvasSweepDirection, CanvasArcSize.Small);
                    pathBuilder.EndFigure(CanvasFigureLoop.Open);
                }
                else
                {
                    pathBuilder.BeginFigure((float)t.Xi, (float)t.Yi);
                    pathBuilder.AddLine(finalPoints);
                    pathBuilder.EndFigure(CanvasFigureLoop.Open);

                }



            });
            CanvasGeometry triangleGeometry = CanvasGeometry.CreatePath(pathBuilder);

            using (var drawSession = args.DrawingSession)
            {
                //Vector2 vectorScale = new Vector2()
                //{
                //    X = (float)_compositeTransform.ScaleX,
                //    Y = (float)_compositeTransform.ScaleY

                //};

                //ScaleEffect scaleEffect = new ScaleEffect()
                //{
                //    Scale = vectorScale
                //};

                drawSession.DrawGeometry(triangleGeometry, Colors.White, (float)1);




                //};
            }




        }



        catch (Exception ex)
        {
            var teste = 0;
        };


    }

    private void Page_Loaded(object sender, RoutedEventArgs e)
    {
        // Initialize the display DPI, and listen for events in case this changes.
        var display = DisplayInformation.GetForCurrentView();
        display.DpiChanged += Display_DpiChanged;
        Display_DpiChanged(display, null);

        // Initialize the help text depending on what input devices are available.
        var toZoom = new List<string>();

        if (new TouchCapabilities().TouchPresent > 0)
        {
            toZoom.Add("Pinch");
        }

        if (new KeyboardCapabilities().KeyboardPresent > 0)
        {
            toZoom.Add("A/Z");

            if (new MouseCapabilities().VerticalWheelPresent > 0)
            {
                toZoom.Add("Ctrl+Wheel");
            }
        }

        //helpText.Text = "To zoom:\n  " + string.Join("\n  ", toZoom);

        // Set focus to our control, so it will receive keyboard input.
        ManipulateMe.Focus(FocusState.Programmatic);

    }

    private void Page_Unloaded(object sender, RoutedEventArgs e)
    {
        // Explicitly remove references to allow the Win2D controls to get garbage collected
        ManipulateMe.RemoveFromVisualTree();
        ManipulateMe = null;

        DisplayInformation.GetForCurrentView().DpiChanged -= Display_DpiChanged;
    }




    void Display_DpiChanged(DisplayInformation sender, object args)
    {
        displayDpi = sender.LogicalDpi;

        // Manually call the ViewChanged handler to update DpiScale.
        scrollViewer_ViewChanged(null, null);
    }
    private void scrollViewer_ViewChanged(object sender, ScrollViewerViewChangedEventArgs e)
    {
        // Cancel out the display DPI, so our fractal always renders at 96 DPI regardless of display
        // configuration. This boosts performance on high DPI displays, at the cost of visual quality.
        // For even better performance (but lower quality) this value could be further reduced.
        float dpiAdjustment = 96 / displayDpi;

        // Adjust DPI to match the current zoom level.
        float dpiScale = dpiAdjustment * scrollViewer.ZoomFactor;

        // To boost performance during pinch-zoom manipulations, we only update DPI when it has
        // changed by more than 20%, or at the end of the zoom (when e.IsIntermediate reports false).
        // Smaller changes will just scale the existing bitmap, which is much faster than recomputing
        // the fractal at a different resolution. To trade off between zooming perf vs. smoothness,
        // adjust the thresholds used in this ratio comparison.
        var ratio = ManipulateMe.DpiScale / dpiScale;

        if (e == null || !e.IsIntermediate || ratio <= 0.8 || ratio >= 1.25)
        {
            ManipulateMe.DpiScale = dpiScale;
        }
    }

I would like my image to start in the center with the same width as the screen.

My picture starts like this:

enter image description here

I would like that it started like this:

enter image description here

来源:https://stackoverflow.com/questions/59286258/pinch-and-zoom-uwp-default-position-and-size

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!