Zoomin percentage values shows in cameralayout in android xamarin forms
Asked Answered
L

1

0

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);
    }
        }
Laszlo answered 25/8, 2021 at 4:46 Comment(0)
C
1

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";
        }
Concavoconvex answered 25/8, 2021 at 7:43 Comment(3)
If i am using zoomin and zoomout once i release how to stop alert it shows continously like loopLaszlo
i am using custom cameralayout also using TextView but unfortunately not view the valueLaszlo
am already did textview textcolor showing black so UI value cannot shown mrg only i figure out anyway thank you for ur rplyLaszlo

© 2022 - 2024 — McMap. All rights reserved.