缩放百分比值以android xamarin 形式显示在cameralayout 中 [英] Zoomin percentage values shows in cameralayout in android xamarin forms

查看:29
本文介绍了缩放百分比值以android xamarin 形式显示在cameralayout 中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

目前正在使用双指实现放大和缩小,放大和缩小按预期工作,但我们在放大和缩小时显示百分比值,如果可能的话,使用 Textview 或任何其他方式显示值以在 android xamarin 视图中显示缩放百分比值

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);
    }
        }

推荐答案

您可以使用 ZoomRatios 来获取所有缩放值的缩放比例.以 1/100 为增量使用缩放比率.该列表从小到大排序.第一个元素总是100,最后一个元素是最大缩放值的缩放比例.

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.

您可以在 handleZoom 方法中的 IsZoomSupported 之后调用它.

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);

您可以像 value + "x" 一样设置百分比:

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.

更新:

添加一个 TextView 以在屏幕中显示缩放值,如下所示.

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" />

然后在您的 OnTouchEvent 中设置值.

And then set the value in your OnTouchEvent.

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

这篇关于缩放百分比值以android xamarin 形式显示在cameralayout 中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆