Adding Pinch & Zoom to PreviewView

Nathan Sokalski 4,111 Reputation points
2022-11-13T02:29:44.773+00:00

I have a CameraX PreviewView in which I want the user to be able to zoom in/out using the standard pinch gesture. I currently have a handler for the Click event attached to the PreviewView, but I also want to allow the user to zoom using the pinch gesture (if this requires me to convert the Click to using Touch I can do that). I have found the following page:

https://learn.microsoft.com/en-us/xamarin/android/app-fundamentals/touch/android-touch-walkthrough

But that seems to be overcomplicating things by adding many different gestures & creating a custom View, and sounded very inefficient for my scenario. I also found the following:

https://medium.com/androiddevelopers/display-a-camera-preview-with-previewview-86562433d86c#:~:text=Another%20common%20camera%20preview%20feature%20is,update%20the%20camera%E2%80%99s%20zoom%20ratio%20accordingly.&text=Another%20common%20camera%20preview,camera%E2%80%99s%20zoom%20ratio%20accordingly.&text=camera%20preview%20feature%20is,update%20the%20camera%E2%80%99s%20zoom

But I was having trouble understanding that, since it was written in Java & only displayed a small part of the code. I am pretty sure I will be inheriting from ScaleGestureDetector.SimpleOnScaleGestureListener to create a new class & overriding OnScale, although I am not completely sure what I will need to put in the override. What exactly do I need to do to add the scale gesture, and is there anything extra I need to do to keep the Click event as well? Thanks.

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,371 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. Leon Lu (Shanghai Wicresoft Co,.Ltd.) 78,671 Reputation points Microsoft Vendor
    2022-11-14T09:03:29.517+00:00

    Hello,

    What exactly do I need to do to add the scale gesture, and is there anything extra I need to do to keep the Click event as well? Thanks.

    You need to set TouchListener with previewView1.SetOnTouchListener(new MyTouchListener(Application.Context)); You need to judge the guesture(click or Pinch & Zoom) by move distance.

    Then, you can inherit from ScaleGestureDetector.SimpleOnScaleGestureListener to create a new class & overriding OnScale. Here is simple code.

       public class MyScaleGestureDetector : ScaleGestureDetector.SimpleOnScaleGestureListener  
           {  
               public override bool OnScale(ScaleGestureDetector detector)  
               {  
                   //add your code here.  
         
                   return true;  
               }  
           }  
           internal class MyTouchListener : Java.Lang.Object, View.IOnTouchListener  
           {  
              private Context context;  
               ScaleGestureDetector myScaleGestureDetector;  
               public MyTouchListener(Context context)  
               {  
                   this.context = context;  
                    myScaleGestureDetector = new ScaleGestureDetector(context, new MyScaleGestureDetector());  
              }  
         
              public bool OnTouch(View v, MotionEvent e)  
               {  
                   float MoveDownX=0F;  
                   if (e.Action==MotionEventActions.Down)  
                   {                 
                       MoveDownX = e.RawX;  
                   }  
                  if (e.Action==MotionEventActions.Move)  
                   {  
                       //execute the Pinch & Zoom  
                       myScaleGestureDetector.OnTouchEvent(e);  
                   }  
                   if (e.Action==MotionEventActions.Up)  
                   {  
                     float tempDX=  e.RawX - MoveDownX;  
                       if (Math.Abs(tempDX)<6)  
                       {  
                           // distance is too low, execute click event.  
                             
                       }  
                   }       
                   return true;  
               }  
           }  
    

    Best Regards,

    Leon Lu


    If the answer is the right solution, please click "Accept Answer" and kindly upvote it. If you have extra questions about this answer, please click "Comment".

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.


Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.