0

Using Xamarin Forms custom renderers, I need to superimpose a crosshairs image on a live camera view (video). I got the iOS renderer working, it is for a BoxView. Android however is fairly new to me. So in the Android BoxView renderer I'm starting with something simple, just trying to display an ImageView or an EditText control for starters.

Struggling with that despite a great deal of googling.

Here's my code. It executes, but no control (image) appears onscreen.

SHARED CODE:

using Xamarin.Forms;

namespace MyApp.Views
{
    public class CameraBoxView : BoxView
    // Uses custom renderer CameraBoxViewRenderer.cs
    {}
}

ANDROID RENDERER:

using System;

using Xamarin.Forms;
using Xamarin.Forms.Platform.Android;

using MyApp.Views;
using MyApp.Droid.Camera;

using Android.Content;
using Android.Widget;
using Android.Views;


[assembly:ExportRenderer (typeof(CameraBoxView), typeof(CameraBoxViewRenderer))]

namespace MyApp.Droid.Camera
{
    public class CameraBoxViewRenderer : BoxRenderer
    {
        private Context context;
        private ImageView imageView;
        private EditText editText;

        public CameraBoxViewRenderer ( Context context ) : base ( context )
        {
            this.context = context;
        }

        protected override void OnElementChanged ( ElementChangedEventArgs<BoxView> e )
        {
            base.OnElementChanged ( e );

            if ( e.OldElement != null || Element == null)
                return;

            CameraBoxView box = Element as CameraBoxView;

            imageView = new ImageView ( context );
            imageView.ContentDescription = "my image";
            imageView.SetImageResource ( Resource.Drawable.myimage );
            imageView.LayoutParameters = new LinearLayout.LayoutParams ( ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.MatchParent );
            imageView.SetScaleType ( ImageView.ScaleType.FitCenter );
            imageView.Visibility = ViewStates.Visible;
            this.AddView ( imageView );

            //editText = new EditText ( context );
            //editText.ContentDescription = "edit_text";
            //editText.LayoutParameters = new LinearLayout.LayoutParams ( ViewGroup.LayoutParams.MatchParent,ViewGroup.LayoutParams.MatchParent );
            //editText.Visibility = ViewStates.Visible;
            //editText.Hint = "Hello world!";
            //this.AddView ( editText );
        }
    }
}
BillF
  • 1,034
  • 3
  • 13
  • 28
  • Could you show a shotscreen for the effect you want ? – Leo Zhu Mar 22 '21 at 09:42
  • @Leo Zhu, I just want the full screen to be filled with the image. Alternatively, with the Edit Text, to see the EditText box with the hint in it. At the moment the screen shows as just the background color. – BillF Mar 22 '21 at 11:33

1 Answers1

1

Adding the following code solved the problem. Kudos to Adam Kemp.

        protected override void OnLayout ( bool changed, int l, int t, int r, int b )
        {
            base.OnLayout ( changed, l, t, r, b );

            GetChildAt ( 0 ).Layout ( 0, 0, r-l, b-t );
        }

        protected override void OnMeasure ( int widthMeasureSpec, int heightMeasureSpec )
        {
            base.OnMeasure ( widthMeasureSpec, heightMeasureSpec );

            GetChildAt (0).Measure ( widthMeasureSpec, heightMeasureSpec );
        }
BillF
  • 1,034
  • 3
  • 13
  • 28