Xamarin - 将相机提要设置为背景
Xamarin - Setting Camera feed as background
所以,我正在尝试以摄像头画面作为背景进行登录 activity。我已经在 https://developer.xamarin.com/guides/android/user_interface/textureview/ 上测试了该示例并使其正常工作,但我需要它成为 activity 的一部分。所以,我尝试了这个:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.Login);
_textureView = this.FindViewById<TextureView>(Resource.Id.textureView);
_textureView.SurfaceTextureListener = this;
}
public void OnSurfaceTextureAvailable(
Android.Graphics.SurfaceTexture surface, int w, int h)
{
_cam = Camera.Open();
_textureView.LayoutParameters =
new FrameLayout.LayoutParams(w, h);
try
{
_cam.SetPreviewTexture(surface);
_cam.StartPreview();
}
catch (Java.IO.IOException ex)
{
Console.WriteLine(ex.Message);
}
}
axml 文件看起来有点像这样:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="SizeProportional" />
<LinearLayout
android:id="@+id/linearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="SizeProportional">
<!-- Some other controls -->
</LinearLayout>
</AbsoluteLayout>
但是,应用现在在退出 OnSurfaceTextureAvailable 块后崩溃,出现未处理的异常。中断异常不起作用,它显然在不再是 运行 的线程中。
有谁知道它崩溃的原因,更重要的是,如何修复它?
此代码有问题:
_textureView.LayoutParameters = new FrameLayout.LayoutParams(w, h);
我不确定您为什么需要重置 TextureView
的布局参数,在 xml 中您已将其设置为与父级匹配。而这里的parent是AbsoluteLayout
,不是FrameLayout
,所以才会出现这个错误。
为了解决这个问题,因为 AbsoluteLayout
现在已经过时了,我建议在你的 xml 中使用 RelativeLayout
作为根容器。然后你可以这样编码:
_textureView.LayoutParameters = new RelativeLayout.LayoutParams(width, height);
当然你也可以把你的xml里的AbsoluteLayout
改成FrameLayout
,这样代码_textureView.LayoutParameters = new FrameLayout.LayoutParams(w, h);
就不用再修改了[=20] =]
所以,我正在尝试以摄像头画面作为背景进行登录 activity。我已经在 https://developer.xamarin.com/guides/android/user_interface/textureview/ 上测试了该示例并使其正常工作,但我需要它成为 activity 的一部分。所以,我尝试了这个:
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
// Create your application here
SetContentView(Resource.Layout.Login);
_textureView = this.FindViewById<TextureView>(Resource.Id.textureView);
_textureView.SurfaceTextureListener = this;
}
public void OnSurfaceTextureAvailable(
Android.Graphics.SurfaceTexture surface, int w, int h)
{
_cam = Camera.Open();
_textureView.LayoutParameters =
new FrameLayout.LayoutParams(w, h);
try
{
_cam.SetPreviewTexture(surface);
_cam.StartPreview();
}
catch (Java.IO.IOException ex)
{
Console.WriteLine(ex.Message);
}
}
axml 文件看起来有点像这样:
<?xml version="1.0" encoding="utf-8"?>
<AbsoluteLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextureView
android:id="@+id/textureView"
android:layout_width="match_parent"
android:layout_height="match_parent"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="SizeProportional" />
<LinearLayout
android:id="@+id/linearLayout"
android:orientation="vertical"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:background="@android:color/transparent"
AbsoluteLayout.LayoutBounds="0,0,1,1"
AbsoluteLayout.LayoutFlags="SizeProportional">
<!-- Some other controls -->
</LinearLayout>
</AbsoluteLayout>
但是,应用现在在退出 OnSurfaceTextureAvailable 块后崩溃,出现未处理的异常。中断异常不起作用,它显然在不再是 运行 的线程中。 有谁知道它崩溃的原因,更重要的是,如何修复它?
此代码有问题:
_textureView.LayoutParameters = new FrameLayout.LayoutParams(w, h);
我不确定您为什么需要重置 TextureView
的布局参数,在 xml 中您已将其设置为与父级匹配。而这里的parent是AbsoluteLayout
,不是FrameLayout
,所以才会出现这个错误。
为了解决这个问题,因为 AbsoluteLayout
现在已经过时了,我建议在你的 xml 中使用 RelativeLayout
作为根容器。然后你可以这样编码:
_textureView.LayoutParameters = new RelativeLayout.LayoutParams(width, height);
当然你也可以把你的xml里的AbsoluteLayout
改成FrameLayout
,这样代码_textureView.LayoutParameters = new FrameLayout.LayoutParams(w, h);
就不用再修改了[=20] =]