英文:
Is it possible to synchronize the content draw by OpenGL and the position of a View in Android?
问题
在FrameLayout中有两个视图:
- SurfaceView
 - View
 
SurfaceView的内容是在另一个线程中使用OpenGL绘制的:
- 从Java将SurfaceView的Surface发送到C++
 - 创建ANativeWindow:ANativeWindow_fromSurface(env, surface);
 - 创建surface:eglCreateWindowSurface()
 - 其他准备工作并绘制到surface上
 - 在主线程中为View设置LayoutParams()
 
我试图使View的位置与SurfaceView的内容对齐。例如,如果我绘制一个移动的正方形,我希望在正方形移动时将View的位置设置为与正方形匹配。
但是当我尝试时,View在SurfaceView的内容之前移动。
我尝试用TextureView或使用ImageReader提供surface给本地的CustomView替换SurfaceView,但没有成功。
有人有办法实现吗?
英文:
There are 2 views in a FrameLayout:
- SurfaceView
 - View
 
The content of SurfaceView is drawn using OpenGL in another thread:
- Send the Surface of SurfaceView from java to c++
 - Create ANativeWindow: ANativeWindow_fromSurface(env, surface);
 - Create surface: eglCreateWindowSurface()
 - Other preparation stuff and draw to the surface
 - setLayoutParams() for the View in the main thread.
 
I'm trying to make the position of View align with the content of SurfaceView. E.g if I draw a moving square, I want to set the position of View to match the square when it is moving.
But when I try it, the View moves before the content of SurfaceView.
I've tried to replace SurfaceView with a TextureView or a CustomView which uses ImageReader to provide surface to native, but no luck.
Does anyone have an idea how to make it possible?
答案1
得分: 0
我通过使用ImageReader的Surface来完成了这个操作:
- 在绘制之前,将位置和纳秒时间从工作线程发送到主线程。
 - 绘制到
ImageReader的Surface上。 - 通过
ImageReader.OnImageAvailableListener获取Image。 - 获取
Image.getTimestamp()的值,这是创建Image时的纳秒时间。将其与之前的纳秒时间进行比较,以获取相应的位置。更新位置并将相应的Image绘制到View上。 - 在
View.onDraw之后,由Choreographer.FrameCallback在屏幕上绘制Image后关闭Image。 
英文:
I was able to do it by using a surface from ImageReader:
- Before drawing, send the position and nano time from worker thread to main thread.
 - Draw to the 
SurfaceofImageReader - Acquire the 
ImagefromImageReader.OnImageAvailableListener - Get the 
Image.getTimestamp()value, this is nano time when theImageis created. Compare it with nano time from earlier to get the corresponding position. Update the position and draw the correspondingImageto theView. - Close the 
Imageafter it is drawn on screen by theChoreographer.FrameCallbackafterView.onDraw. 
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。


评论