如何使用Android相机缩放我的自定义相机?

时间:2018-03-06 11:17:03

标签: android xamarin camera zoom

我完成了使用xamarin制作自定义相机应用的工作。我渲染相机,现在我必须实现缩放功能。但是,我不知道该怎么做。我搜索并发现有startSmoothZoom(int)。但我怎么能用这个呢?有什么例子吗?当设备识别捏合手势时,我调用该功能。提前谢谢。

1 个答案:

答案 0 :(得分:1)

    private float oldDist = 1f;//to caculate Zoom in/out
    public override bool OnTouchEvent(MotionEvent e)
    {
        if (e.PointerCount == 1) {
        }
        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, mCamera);
                } else if (newDist<oldDist) {
                    handleZoom(false, mCamera);
        }
                oldDist = newDist;
                break;
        }
        return true;
    }

    //get fingers's distance
    private static float getFingerSpacing(MotionEvent e)
    {
        float x = e.GetX(0) - e.GetX(1);
        float y = e.GetY(0) - e.GetY(1);
        return (float)Math.Sqrt(x * x + y * y);
    }
    //Zoom
    private void handleZoom(Boolean isZoomIn, Camera camera)
    {
        Camera.Parameters parameters = camera.GetParameters();
        if (parameters.IsZoomSupported) {
            int maxZoom = parameters.MaxZoom;
            int zoom = parameters.Zoom;
            if (isZoomIn && zoom < maxZoom)
            {
                zoom++;
            }
            else if (zoom > 0)
            {
                zoom--;
            }
            parameters.Zoom=zoom;
            camera.SetParameters(parameters);
        } else {
            Android.Util.Log.Error("lv", "zoom not supported");
        }
    }
}