简体   繁体   中英

How to Zoom images in a ListView in Xamarin using MVVM Pattern?

I am new to Xamarin and specially to MVVM. I have a ListView which is showing me Images.

<ListView ItemsSource="{Binding Urls}" IsVisible="True" VerticalScrollBarVisibility="Always" 
          HorizontalScrollBarVisibility="Always" HorizontalOptions="Fill" VerticalOptions="Fill"  
          HasUnevenRows="True">
    <ListView.ItemTemplate>
        <DataTemplate>
            <ViewCell>
                <StackLayout Orientation="Vertical" HorizontalOptions="Fill" VerticalOptions="FillAndExpand" Padding="2">
                    <ffimageloading:CachedImage Source="{Binding .}" Aspect="AspectFill" Margin="15">
                        <ffimageloading:CachedImage.GestureRecognizers>                                        
                            <TapGestureRecognizer Command="{Binding Source={x:Reference ImgListView},Path=BindingContext.ImageDoubleTappedCommand}" />
                        </ffimageloading:CachedImage.GestureRecognizers>
                    </ffimageloading:CachedImage>
                </StackLayout>
            </ViewCell>
        </DataTemplate>
    </ListView.ItemTemplate>
</ListView>

Now I want the zoom behavior with double tap and pinch to zoom. I found different links on internet but those don't implement MVVM. For example: https://www.c-sharpcorner.com/article/zoom-in/ . How can I get the zoom functionality in MVVM?

I just used the code behind to trigger an event and use the Zoom logic over there:

<TapGestureRecognizer Tapped="TapGestureRecognizer_TappedAsync" />

Following logic is from the link: https://www.c-sharpcorner.com/article/zoom-in/

double currentScale = 1;
double startScale = 1;
double xOffset = 0;
double yOffset = 0;
private void TapGestureRecognizer_TappedAsync(object sender, System.EventArgs e)
{
    var Content = ((FFImageLoading.Forms.CachedImage)sender);

    double multiplicator = Math.Pow(2, 1.0 / 10.0);
    startScale = Content.Scale;
    Content.AnchorX = 0;
    Content.AnchorY = 0;

    for (int i = 0; i < 10; i++)
    {
        currentScale *= multiplicator;
        double renderedX = Content.X + xOffset;
        double deltaX = renderedX / Content.Width;
        double deltaWidth = Content.Width / (Content.Width * startScale);
        double originX = (0.5 - deltaX) * deltaWidth;

        double renderedY = Content.Y + yOffset;
        double deltaY = renderedY / Content.Height;
        double deltaHeight = Content.Height / (Content.Height * startScale);
        double originY = (0.5 - deltaY) * deltaHeight;

        double targetX = xOffset - (originX * Content.Width) * (currentScale - startScale);
        double targetY = yOffset - (originY * Content.Height) * (currentScale - startScale);

        Content.TranslationX = Math.Min(0, Math.Max(targetX, -Content.Width * (currentScale - 1)));
        Content.TranslationY = Math.Min(0, Math.Max(targetY, -Content.Height * (currentScale - 1)));

        Content.Scale = currentScale;
        //await Task.Delay(10);
    }

    xOffset = Content.TranslationX;
    yOffset = Content.TranslationY;
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM