When Autofocus enable how to show icon using imageview camerarenderer in android xamarin

newxamarin 1 Reputation point
2021-08-13T11:59:48.11+00:00

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.

public void OnAutoFocus(bool success, Camera camera)
{
var parameters = camera.GetParameters();
if (parameters.FocusMode != Android.Hardware.Camera.Parameters.FocusModeContinuousPicture)
{
parameters.FocusMode = Android.Hardware.Camera.Parameters.FocusModeContinuousPicture;

        if (parameters.MaxNumFocusAreas > 0)
        {
            parameters.FocusAreas = null;
        }
        camera.SetParameters(parameters);
        camera.StartPreview();
    }
}

<ImageView android:id="@+id/focusedarea"
android:layout_height="80dp"
android:layout_width="80dp"
android:layout_centerInParent="true"
android:src="@drawable/FocusCircle"
android:visibility="invisible" />

Xamarin
Xamarin
A Microsoft open-source app platform for building Android and iOS apps with .NET and C#.
5,301 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. JarvanZhang 23,951 Reputation points
    2021-08-16T07:40:48.387+00:00

    Hello,​

    Welcome to our Microsoft Q&A platform!

    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.

    Try to get the circle view in the renderer class and then change the visibility of the view to hide/show the circle. Here is the related code, you could refer to it.

       ImageView focus_circle;  
         
       void SetupUserInterface()  
       {  
           activity = this.Context as Activity;  
           view = activity.LayoutInflater.Inflate(Resource.Layout.CameraLayout, this, false);  
           textureView = view.FindViewById<TextureView>(Resource.Id.textureView);  
           ...  
           textureView.Touch += TextureView_Touch; ;  
         
           focus_circle = view.FindViewById<ImageView>(Resource.Id.focus_circle);  
       }  
         
       private void TextureView_Touch(object sender, TouchEventArgs e)  
       {  
           if (camera != null)  
           {  
               var parameters = camera.GetParameters();  
               camera.CancelAutoFocus();  
               Android.Graphics.Rect focusRect = CalculateTapArea(e.Event.GetX(), e.Event.GetY(), 1f);  
               if (parameters.FocusMode != Android.Hardware.Camera.Parameters.FocusModeAuto)  
               {  
                   parameters.FocusMode = Android.Hardware.Camera.Parameters.FocusModeAuto;  
               }  
               if (parameters.MaxNumFocusAreas > 0)  
               {  
                   List<Area> mylist = new List<Area>();  
                   mylist.Add(new Android.Hardware.Camera.Area(focusRect, 1000));  
                   parameters.FocusAreas = mylist;  
               }  
         
               try  
               {  
                   camera.CancelAutoFocus();  
                   camera.SetParameters(parameters);  
                   camera.StartPreview();  
                   camera.AutoFocus(this);  
         
                   //you could define the layoutParameters of the circle and then set the visibility to visible  
                   focus_circle.Visibility = ViewStates.Visible;  
               }  
               catch (System.Exception ex)  
               {  
                   Console.WriteLine(ex.ToString());  
                   Console.Write(ex.StackTrace);  
               }  
           }  
           else  
           {  
               //return false;  
           }  
       }  
         
       public void OnAutoFocus(bool success, Android.Hardware.Camera camera)  
       {  
           ...  
           //hide the circle  
           if (success)  
           {  
               Task.Delay(1000);  
               focus_circle.Visibility = ViewStates.Invisible;  
           }  
       }  
    

    For more details, you could refer to this thread: https://stackoverflow.com/a/41053193/11083277

    Best Regards,

    Jarvan Zhang


    If the response is helpful, please click "Accept Answer" and upvote it.

    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments