以xamarin形式显示图像的最佳方式

时间:2017-07-22 09:28:11

标签: c# xamarin xamarin.forms

我有一个图像,我希望用户在捏合时改变尺寸,在捏合后用户可以在图像周围移动

在xamarin froms中做到这一点的最佳方式是什么? 会很高兴看到一个简单的代码示例

这里我尝试了但是并没有做到最好:

的Xaml:

   <?xml version="1.0" encoding="utf-8" ?>
   <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"
         xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"
         x:Class="ComplexInstructionApp.ShowProgram"
         Title="תוכנית הלימוד שלנו">

   <ContentPage.Content>


</ContentPage.Content>


<Image x:Name="IMI" HorizontalOptions="StartAndExpand"  
VerticalOptions="StartAndExpand"></Image>


</ContentPage>

以下是C#:

public partial class ShowProgram : ContentPage
{

    private double currentScale = 1;
    private double startScale = 1;
    private double xOffset = 0;
    private double yOffset = 0;

    public ShowProgram(string txt)
    {                 
        InitializeComponent();
        switch (txt)
        {
            case "קובני":
                Title = "קובני";
                IMI.Source = "Cuben_Expend.png";
                break;

            case "קובני מצומצם":
                Title = "קובני מצומצם";
                IMI.Source = "Cuben_short.png";
                break;
            case "אל איי":
                Title = "אל איי";
                IMI.Source = "LA_Expend.png";
                break;
            case "אל איי מצומצם":
                Title = "אל איי מצומצם";
                IMI.Source = "LA_short.png";
                break;
            case "בצ'אטה":
                Title = "בצ'אטה";
                IMI.Source = "bachata.png";
                break;
            case "זוק":
                Title = "זוק";
                IMI.Source = "zouk.png";
                break;
        }


        var pinchGesture = new PinchGestureRecognizer();
        pinchGesture.PinchUpdated += (s, e) =>
        {

            if (e.Status == GestureStatus.Started)
            {
                // Store the current scale factor applied to the wrapped user interface element,
                // and zero the components for the center point of the translate transform.
                startScale = Content.Scale;
                Content.AnchorX = 0;
                Content.AnchorY = 0;
            }
            if (e.Status == GestureStatus.Running)
            {
                // Calculate the scale factor to be applied.
                currentScale += (e.Scale - 1) * startScale;
                currentScale = Math.Max(1, currentScale);

                // The ScaleOrigin is in relative coordinates to the wrapped user interface element,
                // so get the X pixel coordinate.
                double renderedX = Content.X + xOffset;
                double deltaX = renderedX / Width;
                double deltaWidth = Width / (Content.Width * startScale);
                double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;

                // The ScaleOrigin is in relative coordinates to the wrapped user interface element,
                // so get the Y pixel coordinate.
                double renderedY = Content.Y + yOffset;
                double deltaY = renderedY / Height;
                double deltaHeight = Height / (Content.Height * startScale);
                double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;

                // Calculate the transformed element pixel coordinates.
                double targetX = xOffset - (originX * Content.Width) * (currentScale - startScale);
                double targetY = yOffset - (originY * Content.Height) * (currentScale - startScale);

                // Apply translation based on the change in origin.
                Content.TranslationX = Clamp1(targetX, -Content.Width * (currentScale - 1), 0);
                Content.TranslationY = Clamp1(targetY, -Content.Height * (currentScale - 1), 0);

                // Apply scale factor.
                Content.Scale = currentScale;
            }
            if (e.Status == GestureStatus.Completed)
            {
                // Store the translation delta's of the wrapped user interface element.
                xOffset = Content.TranslationX;
                yOffset = Content.TranslationY;
            }
        };
        Content.GestureRecognizers.Add(pinchGesture);
    }

    public  double Clamp1(double self, double min, double max)
    {
        return Math.Min(max, Math.Max(self, min));
    }







}

0 个答案:

没有答案