title: How to implement visual indicator when camera is focused
body: <p>I want to display a basic circle when the user focuses manually (tap to focus) for the camera page in my app. </p>
  
  <p>I already have autofocus implemented below on tap but I'm not sure how to draw the circle on focus and dismiss it when the view becomes unfocused as well keep redrawing it when the camera is focused. The indicator should not be part of the final photo, just as a guide to the user when the camera is focused or not. </p>
  
  <p>Here's what I have so far: </p>
  
  <pre><code>public class AutoFocusCallback : Java.Lang.Object, IAutoFocusCallback
  {
      public void OnAutoFocus(bool success, Android.Hardware.Camera camera)
      {
          var parameters = camera.GetParameters();
          var supportedFocusModes = parameters.SupportedFocusModes;
  
          if (string.IsNullOrEmpty(parameters.FocusMode))
          {
              string focusModeContinuous = Android.Hardware.Camera.Parameters.FocusModeContinuousPicture;
              string focusModeAuto = Android.Hardware.Camera.Parameters.FocusModeAuto;
  
              if (supportedFocusModes != null &amp;&amp; supportedFocusModes.Any())
              {
                  if (supportedFocusModes.Contains(focusModeContinuous))
                  {
                      parameters.FocusMode = focusModeContinuous;
                  }
                  else if (supportedFocusModes.Contains(focusModeAuto))
                  {
                      parameters.FocusMode = focusModeAuto;
                  }
              }
          }
  
          if (supportedFocusModes != null &amp;&amp; supportedFocusModes.Any())
          {
              if (parameters.MaxNumFocusAreas &gt; 0)
              {
                  parameters.FocusAreas = null; 
              }
  
               if (success)
               {
                  CameraPage cameraPage = new CameraPage();
                  Canvas canvas = new Canvas(); 
                  cameraPage.Draw(canvas); 
              }
              camera.SetParameters(parameters);
              camera.StartPreview();
          }
      }
  }
  
  public bool OnTouch(Android.Views.View v, MotionEvent e)
  {
      if (camera != null)
      {
          var parameters = camera.GetParameters();
          camera.CancelAutoFocus();
          Rect focusRect = CalculateTapArea(e.GetX(), e.GetY(), 1f);
  
          if (parameters.FocusMode != Android.Hardware.Camera.Parameters.FocusModeAuto)
          {
              parameters.FocusMode = Android.Hardware.Camera.Parameters.FocusModeAuto;
          }
          if (parameters.MaxNumFocusAreas &gt; 0)
          {
              List&lt;Area&gt; mylist = new List&lt;Area&gt;();
              mylist.Add(new Android.Hardware.Camera.Area(focusRect, 1000));
              parameters.FocusAreas = mylist;
          }
  
          try
          {
              camera.CancelAutoFocus();
              camera.SetParameters(parameters);
              camera.StartPreview();
              camera.AutoFocus(new AutoFocusCallback());
          }
          catch (System.Exception ex)
          {
              Console.WriteLine(ex.ToString());
              Console.Write(ex.StackTrace);
          }
          return true;
      }
      else
      {
          return false;
      } 
  }
  
  private Rect CalculateTapArea(object x, object y, float coefficient)
  {
      var focusAreaSize = Math.Max(textureView.Width, textureView.Height) / 8; //Recommended focus area size from the manufacture is 1/8 of the image
      int areaSize = focusAreaSize * (int)coefficient;
  
      int left = clamp(Convert.ToInt32(x) - areaSize / 2, 0, textureView.Width - areaSize);
      int top = clamp(Convert.ToInt32(y) - areaSize / 2, 0, textureView.Height - areaSize);
  
      RectF rectF = new RectF(left, top, left + areaSize, top + areaSize);
      Matrix.MapRect(rectF);
  
      return new Rect((int)System.Math.Round(rectF.Left), (int)System.Math.Round(rectF.Top), (int)System.Math.Round(rectF.Right), (int)System.Math.Round(rectF.Bottom));
  }
  
  private int clamp(int x, int min, int max)
  {
      if (x &gt; max)
      {
          return max;
      }
      if (x &lt; min)
      {
          return min;
      }
      return x;
  }
  
  public override void Draw(Canvas canvas)
  {
      base.Draw(canvas);
      Paint p = new Paint();
      p.Color = Android.Graphics.Color.White; 
      p.SetStyle(Paint.Style.Stroke);
      p.StrokeWidth = 3; 
      canvas.DrawCircle(300, 300, 100, p);
  }
  </code></pre>
  
  <p>E.g. like in this picture: </p>
  
  <p><a href="https://i.stack.imgur.com/DFtxz.png" rel="nofollow noreferrer"><img src="https://i.stack.imgur.com/DFtxz.png" alt="enter image description here"></a></p>
  
  <p>This is for my Xamarin.Forms Android app. </p>
  
  <p>EDIT: Here's a copy of the Android Source code for the camera but not sure where they're making the indicator: </p>
  
  <p><a href="https://android.googlesource.com/platform/packages/apps/Camera.git/+/refs/heads/marshmallow-release/src/com/android/camera" rel="nofollow noreferrer">https://android.googlesource.com/platform/packages/apps/Camera.git/+/refs/heads/marshmallow-release/src/com/android/camera</a></p>
  
  <p>Similar SO posts with no official answers: </p>
  
  <p><a href="https://stackoverflow.com/questions/33095364/how-to-implement-tap-to-focus-indicator-in-android-camera">How to implement tap to focus indicator in android camera?</a></p>
  
  <p><a href="https://stackoverflow.com/questions/33030736/implement-indicator-on-tap-to-focus-in-camera-android">Implement indicator on tap to focus in camera [android]</a></p>
  
  <p><a href="https://stackoverflow.com/questions/40510161/android-camera-2-draw-circle-focus-area">Android Camera2 - Draw circle focus area</a></p>
  
  <p>As you can see, multiple people have the same question but no answers! Please let me know if I'm still being unclear?</p>