Xamarin renders a scalable image (cross platform)

Is there a way to use a pinch to augment generic Xamarin Forms, I've found an implementation for each platform.

+3


source to share


1 answer


You can use Gesture to implement it. There is a good example of wrapping an image in PanContainer available here - Adding a Hard Harness Recognizer .

The pan gesture is used to detect dragging and is performed using the PanGestureRecognizer class . A common scenario for panorama gestures is to move the image horizontally and vertically, so that all of the image's content can be viewed when it is displayed in the viewport smaller than the image size. This is achieved by moving the image in the viewport.

Simple example:

<Image Source="MonoMonkey.jpg">
  <Image.GestureRecognizers>
    <PanGestureRecognizer PanUpdated="OnPanUpdated" />
  </Image.GestureRecognizers>
</Image>

      



Sample XAML container container:

<AbsoluteLayout>
    <local:PanContainer>
        <Image Source="MonoMonkey.jpg" WidthRequest="1024" HeightRequest="768" />
    </local:PanContainer>
</AbsoluteLayout>

      

Code behind:

public class PanContainer : ContentView
{
  double x, y;

  public PanContainer ()
  {
    // Set PanGestureRecognizer.TouchPoints to control the
    // number of touch points needed to pan
    var panGesture = new PanGestureRecognizer ();
    panGesture.PanUpdated += OnPanUpdated;
    GestureRecognizers.Add (panGesture);
  }

 void OnPanUpdated (object sender, PanUpdatedEventArgs e)
 {
   switch (e.StatusType) {
   case GestureStatus.Running:
     // Translate and ensure we don't pan beyond the wrapped user interface element bounds.
     Content.TranslationX =
      Math.Max (Math.Min (0, x + e.TotalX), -Math.Abs (Content.Width - App.ScreenWidth));
     Content.TranslationY =
      Math.Max (Math.Min (0, y + e.TotalY), -Math.Abs (Content.Height - App.ScreenHeight));
     break;

   case GestureStatus.Completed:
     // Store the translation applied during the pan
     x = Content.TranslationX;
     y = Content.TranslationY;
     break;
   }
 }
}

      

+2


source







All Articles