Zoom Image Functionality In Android Using Xamarin.Forms

Introduction
 
This article demonstrates how to add the Zoom Image functionality in an Android application using Xamarin.Forms. Xamarin is a platform that allows us to create a multi-platform mobile applications for platforms, like Android, Windows, iOS through a single integrated development environment (IDE). And with Xamarin. Forms, the interface design for all three platform can be accomplished within its XAML-based standard, native user-interface control.
 
 
 
Step 1 
 
Open Visual Studio and go to New Project >> Installed >> Visual C# >> Cross-Platform.
 
Select Cross-Platform app, then give your project a name and location and click "OK" button.
 
 
  
Step 2 

Next, add an image to Solution Explorer >> Project Name.Android >> Resources >> Right Click >> Drawable >> Add >> Existing Item. When we click an existing item button, it opens a dialogue box.
 
 
 
Choose image location and add images.
 
.
 
The image is added successfully. Then move the cursor to that image and just verify the image.
 
 
Step 3 
 
Next, go to Project Name (Portable) >> Right-Click >> Add >> NewFolder. Select the new folder, the Dialogue Box will open. Just give the folder Name and Click "Add" button. The folder name is"Extensions".
 
 
 
Step 4 
 
Now, go to Open Solution Explorer >> Project Name (Portable) >> Extensions >> Right-Click >> Add >> Class. When you click the class button it will open the dialogue. 
 
 
 
The class name is "DoubleExtensions".
 
 
 
Step 5 
 
Next, go to Open Solution Explorer >> Project Name (Portable) >> Extensions >> DubleExtensions.cs >> Left-Click in Open the DoubleExtensions.cs page.
 
 
The code is given below just copy it. 
 
 
C# Code 
  1. using System;  
  2. using System.Collections.Generic;  
  3. using System.Linq;  
  4. using System.Text;  
  5. using System.Threading.Tasks;  
  6.   
  7. namespace Image_Zoom.Extensions  
  8. {  
  9.     public static class DoubleExtensions  
  10.     {  
  11.   
  12.         public static double Clamp(this double self, double min, double max)  
  13.         {  
  14.             return Math.Min(max, Math.Max(self, min));  
  15.         }  
  16.   
  17.     }  
  18. }   
Step 6 
 
Now, Open Solution Explorer >>  Project Name(Portable) >> MainPage.Xaml. Double click for opening the design view of this page.
 
 
 
The code is given below just copy it.
 
 
 
XAML Code 
 
We are adding an image inside the grid. 
  1. <?xml version="1.0" encoding="utf-8" ?>  
  2. <ContentPage xmlns="http://xamarin.com/schemas/2014/forms"  
  3.              xmlns:x="http://schemas.microsoft.com/winfx/2009/xaml"  
  4.              xmlns:local="clr-namespace:Image_Zoom"  
  5.              x:Class="Image_Zoom.MainPage">  
  6.     <ContentPage.Content>  
  7.         <Grid Padding="20">  
  8.             <local:PinchToZoomContainer>  
  9.                 <local:PinchToZoomContainer.Content>  
  10.                     <Image Source="Android.png" />  
  11.                 </local:PinchToZoomContainer.Content>  
  12.             </local:PinchToZoomContainer>  
  13.         </Grid>  
  14.     </ContentPage.Content>  
  15. </ContentPage>  
  16.       
  17.        
Step 7
 
Next, go to  Open Solution Explorer >> Project Name >> Right-Click >> Add >> Class. 
 
 
 
The class name is "PinchToZoomContainer.cs".
 
 
 
Step 8 
 
Open Solution Explorer >> Project Name(Portable) >> PinchToZoomContainer.cs. Double click for opening this page.
 
 
 
The code is given below; just copy it.
 
 
 
C# Code 
  1. using Image_Zoom.Extensions;  
  2. using System;  
  3. using System.Collections.Generic;  
  4. using System.Linq;  
  5. using System.Text;  
  6. using System.Threading.Tasks;  
  7. using Xamarin.Forms;  
  8.   
  9. namespace Image_Zoom  
  10. {  
  11.     public class PinchToZoomContainer : ContentView  
  12.     {  
  13.         double currentScale = 1;  
  14.         double startScale = 1;  
  15.         double xOffset = 0;  
  16.         double yOffset = 0;  
  17.   
  18.         public PinchToZoomContainer()  
  19.         {  
  20.             var pinchGesture = new PinchGestureRecognizer();  
  21.             pinchGesture.PinchUpdated += OnPinchUpdated;  
  22.             GestureRecognizers.Add(pinchGesture);  
  23.         }  
  24.   
  25.         void OnPinchUpdated(object sender, PinchGestureUpdatedEventArgs e)  
  26.         {  
  27.             if (e.Status == GestureStatus.Started)  
  28.             {  
  29.                 // Store the current scale factor applied to the wrapped user interface element,  
  30.                 // and zero the components for the center point of the translate transform.  
  31.                 startScale = Content.Scale;  
  32.                 Content.AnchorX = 0;  
  33.                 Content.AnchorY = 0;  
  34.             }  
  35.             if (e.Status == GestureStatus.Running)  
  36.             {  
  37.                 // Calculate the scale factor to be applied.  
  38.                 currentScale += (e.Scale - 1) * startScale;  
  39.                 currentScale = Math.Max(1, currentScale);  
  40.   
  41.                 // The ScaleOrigin is in relative coordinates to the wrapped user interface element,  
  42.                 // so get the X pixel coordinate.  
  43.                 double renderedX = Content.X + xOffset;  
  44.                 double deltaX = renderedX / Width;  
  45.                 double deltaWidth = Width / (Content.Width * startScale);  
  46.                 double originX = (e.ScaleOrigin.X - deltaX) * deltaWidth;  
  47.   
  48.                 // The ScaleOrigin is in relative coordinates to the wrapped user interface element,  
  49.                 // so get the Y pixel coordinate.  
  50.                 double renderedY = Content.Y + yOffset;  
  51.                 double deltaY = renderedY / Height;  
  52.                 double deltaHeight = Height / (Content.Height * startScale);  
  53.                 double originY = (e.ScaleOrigin.Y - deltaY) * deltaHeight;  
  54.   
  55.                 // Calculate the transformed element pixel coordinates.  
  56.                 double targetX = xOffset - (originX * Content.Width) * (currentScale - startScale);  
  57.                 double targetY = yOffset - (originY * Content.Height) * (currentScale - startScale);  
  58.   
  59.                 // Apply translation based on the change in origin.  
  60.                 Content.TranslationX = targetX.Clamp(-Content.Width * (currentScale - 1), 0);  
  61.                 Content.TranslationY = targetY.Clamp(-Content.Height * (currentScale - 1), 0);  
  62.   
  63.                 // Apply scale factor  
  64.                 Content.Scale = currentScale;  
  65.             }  
  66.             if (e.Status == GestureStatus.Completed)  
  67.             {  
  68.                 // Store the translation delta's of the wrapped user interface element.  
  69.                 xOffset = Content.TranslationX;  
  70.                 yOffset = Content.TranslationY;  
  71.             }  
  72.         }  
  73.     }  
  74. }   
Step 9 
 
Next, select the built and deploy option followed by selecting from the list of Android Emulators or Simulator. You can choose any API (Application Program Interface) level emulator or simulator to run it.
 
Output 
 
After a few seconds, you will see your app working. The result is displayed below.
 
 
 
Finally, we have successfully created the desired Xamarin.Forms application.
 
Conclusion 
 
I hope you have learned how to add Zoom functionality in Android using Xamarin.Forms with Visual Studio and C#.


Similar Articles