question

newxamarin-5426 avatar image
0 Votes"
newxamarin-5426 asked RobCaplan edited

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

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

dotnet-xamarin
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

1 Answer

JarvanZhang-MSFT avatar image
0 Votes"
JarvanZhang-MSFT answered

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.


5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.