使用 XML 布局和 SurfaceView 的结果不一致
我使用 XML 布局来显示一个自定义类,该类扩展了 SurfaceView 以及其右侧的单个按钮,使用下面的 XML 代码:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<com.my.name.MySurfaceView android:id="@+id/camera_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"/>
</LinearLayout>
然后我使用以下代码设置我的活动的布局:
MyActivity.java
public class MyActivity extends Activity {
private MySurfaceView mySurfaceView;
private Button button;
public MyActivity() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mySurfaceView = (MySurfaceView) findViewById(R.id.camera_view);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View v) {
mySurfaceView.setClick(true);
}
});
}
}
然后,MySurfaceView
类扩展 SurfaceView
并设置为显示相机流。
问题是,启动应用程序时,通常会出现不同的结果。要么:
Button
和 MySurfaceView
同时绘制到屏幕上,然后相机流开始显示(这就是我希望它每次都能工作的方式)
或
MySurfaceView
加载并显示相机流几秒钟,然后最终显示 Button
。
我知道在显示按钮之前必须创建和测量 SurfaceView 等,因此我应该预计会有轻微的延迟,但如上所述,在某些情况下 SurfaceView在按钮最终绘制到屏幕上之前,会显示摄像头流长达 5 秒。
我怎样才能避免这种情况?
提前感谢您的帮助。
编辑:
尽管SurfaceView
已显示在屏幕上,但似乎尚未完全完成创建。我正在使用 OpenCV 处理相机流,在最初绘制 SurfaceView 后,这似乎仍在初始化,因此在完成所有这些之前不会绘制 Button完成了。
I'm using an XML layout to show a custom class which extends SurfaceView along with a single button to the right of it, using the XML code below:
main.xml
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:orientation="horizontal">
<com.my.name.MySurfaceView android:id="@+id/camera_view"
android:layout_width="wrap_content"
android:layout_height="wrap_content"/>
<Button android:id="@+id/button"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:text="@string/button_text"/>
</LinearLayout>
I'm then using the following code to set the layout for my activity:
MyActivity.java
public class MyActivity extends Activity {
private MySurfaceView mySurfaceView;
private Button button;
public MyActivity() {
}
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
mySurfaceView = (MySurfaceView) findViewById(R.id.camera_view);
button = (Button) findViewById(R.id.button);
button.setOnClickListener(new android.view.View.OnClickListener() {
public void onClick(View v) {
mySurfaceView.setClick(true);
}
});
}
}
The MySurfaceView
class then extends SurfaceView
and is setup to show the camera stream.
The problem is, when starting the app, there are often different outcomes. Either:
Both the Button
and MySurfaceView
are drawn to the screen at the same time, then the camera stream starts displaying (This is how I'd like it to work every time)
or
MySurfaceView
loads and shows the camera stream for a few seconds, before the Button
is eventually displayed.
I understand that the SurfaceView
has to be created and measured etc before the button can be displayed so I should expect a slight delay, but as mentioned above, in some cases the SurfaceView
displays the camera stream for up to 5 seconds before the button is eventually drawn to the screen.
How I can avoid this?
Thanks for your help in advance.
EDIT:
It seems as though the SurfaceView
is not completely finished being created, despite being shown on the screen. I'm using OpenCV to process the camera stream and this seems to still be initialising things after the SurfaceView
is initially drawn, and so the Button
is not drawn until all of this is finished.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论