使用 XML 布局和 SurfaceView 的结果不一致

发布于 2025-01-03 02:26:17 字数 2061 浏览 2 评论 0原文

我使用 XML 布局来显示一个自定义类,该类扩展了 SurfaceView 以及其右侧的单个按钮,使用下面的 XML 代码:

ma​​in.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 并设置为显示相机流。

问题是,启动应用程序时,通常会出现不同的结果。要么:

ButtonMySurfaceView 同时绘制到屏幕上,然后相机流开始显示(这就是我希望它每次都能工作的方式)

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。
列表为空,暂无数据
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文