简体   繁体   English

SurfaceView 和 ANativeWindow

[英]SurfaceView and ANativeWindow

I have a question regarding creation of a SurfaceView and subsequently getting a ANativeWindow from it.我有一个关于创建 SurfaceView 并随后从中获取 ANativeWindow 的问题。

  1. Is it proper to do这样做是否合适

    mSurfaceView = new SurfaceView(this); in:在:

    • onCreate()
    • onStart()

Reason for asking: as I understand it the SurfaceView will get destroyed when we lose focus (something else covers up the entire screen) so we will need to re-create it every time we gain focus (onStart() is executed).询问原因:据我所知,当我们失去焦点时 SurfaceView 会被破坏(其他东西覆盖了整个屏幕)所以我们每次获得焦点时都需要重新创建它(执行 onStart())。 Or does the SurfaceView remain dormant and reusable?还是 SurfaceView 保持休眠和可重用?

  1. Moving on, now I would like to create a native window from the above-mentioned surface (in native code).继续,现在我想从上述表面(在本机代码中)创建一个本机窗口。 Is it proper to do这样做是否合适

    ANativeWindow* newwindow = ANativeWindow_fromSurface(jniEnv, joSurface) in: ANativeWindow* newwindow = ANativeWindow_fromSurface(jniEnv, joSurface)在:

    • onSurfaceCreated_native(..., jobject surface)
    • onSurfaceChanged_native(..., jobject surface)

Reason for asking: onSurfaceChanged seems to be always called after onSurfaceCreated so we have a choice as to when to create the native window.询问原因: onSurfaceChanged 似乎总是在onSurfaceCreated之后onSurfaceCreated因此我们可以选择何时创建本机窗口。 On one hand, it appears logical to do this in onSurfaceCreated , but the two jobject surface appear to be referencing different objects!一方面,在onSurfaceCreated执行此操作似乎合乎逻辑,但两个作业对象jobject surface似乎引用了不同的对象! (As checked by creating a weak global ref to surface in onSurfaceCreated and checking it against both NULL and surface in onSurfaceChanged, see code below) (通过在 onSurfaceCreated 中创建一个弱全局引用到表面并对照 NULL 和 onSurfaceChanged 中的表面进行检查,请参见下面的代码)

onSurfaceCreated_native(JNIEnv env, ... ,jobject surface) {
myWeakObjectGlobal = env->NewWeakGlobalRef(surface);
}

onSurfaceChanged_native(JNIEnv env, ... ,jobject surface) {

if (env->IsSameObject(surface, myWeakObjectGlobal)) {
    LOGW("onSurfaceChanged_native: new surface is SAME as old surface");
} else {
    LOGW("onSurfaceChanged_native: new surface is DIFFERENT as old surface");
}

if (env->IsSameObject(NULL, myWeakObjectGlobal)) {
    LOGW("    furthermore, old surface is NULL");
} else {
    LOGW("    furthermore, old surface is NOT null");
}

}

Therefore, if there are indeed two distinct surface objects being sent into onSurfaceCreated and onSurfaceChanged, then we want to use the freshest one and not hang on to a stale surface reference, and consequently do ANativeWindow_from_Surface in onSurfaceChanged.因此,如果确实有两个不同的表面对象被发送到 onSurfaceCreated 和 onSurfaceChanged,那么我们希望使用最新鲜的一个而不是停留在陈旧的表面引用上,因此在 onSurfaceChanged 中执行 ANativeWindow_from_Surface。

I would really appreciate it if someone could shine some light on this issue for me.如果有人能为我阐明这个问题,我将不胜感激。

您是否尝试过使用android.view.Surface而不是android.view.SurfaceView

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM