Search code examples
xamarin.androidzoomingpercentagecalculation

Zoomin percentage values shows in cameralayout in android xamarin forms


Currently am implementing zoomin and zoomout using twofingers zoomin and zoomout works as expected but we are showing percentage values in while zoomin and zoomout if possible to show the values using Textview or anyother way to show the zooming percentage values in view in android xamarin

public override bool OnTouchEvent(MotionEvent e)
            {
                
                switch (e.Action & MotionEventActions.Mask)
                {
                    case MotionEventActions.Down:
                        oldDist = getFingerSpacing(e);
                        break;
                    case MotionEventActions.Move:
                        float newDist = getFingerSpacing(e);
                        if (newDist > oldDist)
                        {
                            //mCamera is your Camera which used to take picture, it should already exit in your custom Camera
                            handleZoom(true, camera);
                        }
                        else if (newDist < oldDist)
                        {
                            handleZoom(false, camera);
                        }
                        oldDist = newDist;
                        break;
                }
                return true;
            }
    
    private void handleZoom(bool isZoomIn, global::Android.Hardware.Camera camera)
            {

                    global::Android.Hardware.Camera.Parameters parameters = camera.GetParameters();
var s = camera.GetParameters().ZoomRatios;
                    if (parameters.IsZoomSupported)
                    {
                        int maxZoom = parameters.MaxZoom;
                        int zoom = parameters.Zoom;
                        
                        if (isZoomIn && zoom < maxZoom)
                        {
                            zoom++;
                        }
                        else if(zoom > 0)
                        {
                            zoom--;
                        }
                        parameters.Zoom = zoom;
 var zoomValue = Convert.ToDecimal(s[zoom]);
                    zoomValue = Math.Round((zoomValue / 100), 2);
                    Toast toast = Toast.MakeText(Android.App.Application.Context, _languageCache.Translate(zoomValue + "%"), ToastLength.Short);
                    toast.SetGravity(GravityFlags.Center, 0, 0);
                    toast.Show();
                    camera.SetParameters(parameters);
        PrepareAndStartCamera();
                }
                else
                {
                    Android.Util.Log.Error("lv", "zoom not supported");
                }
            }
        private static float getFingerSpacing(MotionEvent e)
            {
         if(e.PointerCount==2)
    {
               int pointerIndex = e.FindPointerIndex(_activePointerId);
                float x = e.GetX(pointerIndex);
                float y = e.GetY(pointerIndex);
        return (float)Math.Sqrt(x * x + y * y);
    }
        }

Solution

  • You could use the ZoomRatios to get the zoom ratios of all zoom values. Use the zoom ratios in 1/100 increments. The list is sorted from small to large. The first element is always 100. The last element is the zoom ratio of the maximum zoom value.

    You could call this after IsZoomSupported in handleZoom method.

       var s = camera.GetParameters().ZoomRatios;
                var zoomValue = Convert.ToDecimal(s[zoom]);
                value = Math.Round((zoomValue / 100), 2);
    

    You could set the percentage like value + "x":

     2.5x  // a zoom of 2.5x is returned as 250.
    

    After that you could show this value with a dialpg alert or custom the camera layout as your own.

    Update:

    Add a TextView to show the zoom value in the screen like below.

     <TextView
        
        android:layout_width="50dp"
        android:layout_height="50dp"
        android:elevation="10dp"
        android:id="@+id/textView1" />
    

    And then set the value in your OnTouchEvent.

      if (value != 0)
            {
                textView.Text = value+"x";
            }