SurfaceView 和 ANativeWindow
我有一个关于创建 SurfaceView 并随后从中获取 ANativeWindow 的问题。
- 在以下位置执行
mSurfaceView = new SurfaceView(this);
是否正确:onCreate()
onStart()
询问原因:据我了解,当我们失去焦点时,SurfaceView 将被销毁(其他东西覆盖了整个屏幕)因此,每次获得焦点(执行 onStart())时,我们都需要重新创建它。或者 SurfaceView 是否保持休眠状态并可重用?
继续,现在我想从上述表面(以本机代码)创建一个本机窗口。在以下位置执行
ANativeWindow* newwindow = ANativeWindow_fromSurface(jniEnv, joSurface)
是否正确:onSurfaceCreated_native(..., jobject 表面)
onSurfaceChanged_native(..., jobject 表面)
询问原因:onSurfaceChanged 似乎总是在之后调用onSurfaceCreated
因此我们可以选择何时创建本机窗口。一方面,在 onSurfaceCreated
中执行此操作似乎合乎逻辑,但两个 jobject surface
似乎引用了不同的对象! (通过在 onSurfaceCreated 中创建对 Surface 的弱全局引用并在 onSurfaceChanged 中检查 NULL 和 Surface 进行检查,请参见下面的代码)
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");
}
}
因此,如果有实际上有两个不同的表面对象被发送到 onSurfaceCreated 和 onSurfaceChanged 中,然后我们想要使用最新的一个而不是挂在过时的表面引用上,因此在中执行 ANativeWindow_from_Surface onSurface 已更改。
如果有人能为我阐明这个问题,我将非常感激。
I have a question regarding creation of a SurfaceView and subsequently getting a ANativeWindow from it.
- 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). Or does the SurfaceView remain dormant and reusable?
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: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. On one hand, it appears logical to do this in onSurfaceCreated
, but the two jobject surface
appear to be referencing different objects! (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_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.
I would really appreciate it if someone could shine some light on this issue for me.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
您是否尝试过使用
android.view.Surface
而不是android.view.SurfaceView
?Have you tried using
android.view.Surface
instead ofandroid.view.SurfaceView
?