我正在尝试使用Visual Studio和Xamarin为zxing条形码扫描器创建一个覆盖层,但我不知道如何实际实现。
我为此创建了一个小型布局,名为overlay.axml,我希望它可以在相机视图的顶部绘制出来。
Overlay.axml:
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:minWidth="25px"
android:minHeight="25px">
<Space
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/space1"
android:layout_weight="1" />
<ToggleButton
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:id="@+id/tbtnTorch"
android:layout_weight="0"
android:textOn="Torch On"
android:textOff="Torch Off" />
</LinearLayout>
此外,我认为我需要创建一个从View继承的类来将布局与其绑定,但我对此不确定,这是我到目前为止得到的内容。
Overlay.cs:
public Overlay(Context context) : base(context)
{
}
public Overlay(Context context, IAttributeSet attrs) : base(context, attrs)
{
}
public Overlay(Context context, IAttributeSet attrs, int defStyle) : base(context, attrs, defStyle)
{
}
protected override void OnDraw(Android.Graphics.Canvas canvas)
{
base.OnDraw(canvas);
}
我现在希望将遮罩层绑定到我的ZXing扫描器实例上,但是我不确定该如何做。这是我目前的代码。
MainActivity.cs:
scanner = new MobileBarcodeScanner(this);
options = new MobileBarcodeScannerOptions();
scanner.UseCustomOverlay = true;
scanner.CustomOverlay = new Overlay(this);
[...]
var result = await scanner.scan(options);
如果不使用叠加层,则可以像预期的那样正常工作,但是在使用叠加层时,当我开始扫描时,只会得到黑屏。编辑:在模拟器上出现黑屏,在真实设备上没有任何叠加层(如预期的那样)。