Layered SurfaceViews in FrameLayout in Android

I am trying to create an augmented reality application for Android and am faced with the problem of surface layers. I need a surface view to display a preview of the camera on which I will overlay the graphics, and I need to get a surface view to draw my graphics. This second surface should also be drawn above the camera preview, but has a transparent background. In my current implementation, I have both surface views that work, and appear as they should, but the background is not transparent, and therefore there is no appearance of a second surface with painted graphics superimposed on the view of the camera’s viewing surface. How can this be achieved? When searching for numerous questions, as well as other forums, I came across many conflicting opinions on this issue. Some say that representing a surface on a panel in Android is not possible, while others say that it is a matter of using a different layout (FrameLayout vs LinearLayout?). In particular, my implementation includes two kinds: a class, CustomCameraView, which extends SurfaceView, and a class, CustomDrawView, which also extends SurfaceView, contained in FrameLayout, with CustomDrawView appearing after CustomCameraView. How can they be layered, so that CustomDrawView seems to be superimposed on CustomCameraView?

+5
java android transparency augmented-reality surfaceview
source share
2 answers

I think many people have tried this. Google Engineer has stated ( here ) that you should avoid stacking Surface Views. Even if someone found some trick, this is probably incompatible and will lead to problems.

I think this gives three options, depending on your requirements:

  • Top view Surface view

Use Surface View to preview the camera and stack. The disadvantage of this approach is that the imposition of “normal” representations a) is slower and b) occurs in the user interface thread. You can get around b) if you yourself implement the topics. But usually this is probably the way to go if your overlays contain something like user interface elements or multiple Drawables that do not need to be updated frequently.

  • Do it all in SurfaceView

This will give you better performance and less runtime overhead. You have only one SurfaceView. Make overlays on SurfaceView and draw everything. Of course, you can combine both approaches.

  • Do everything in GLSurfaceView

This is probably the path to real productivity. As above, but with rendering the camera view into OpenGL texture in GLSurfaceView .

+7
source share

I got this working by having 2 transparent views over the SurfaceView camera using the following approach:

My activity sets all of its views in the onCreate() method, I do not use any layout file for this. It looks like this:

 protected void onCreate(Bundle savedInstanceState) { super.onCreate(null); //savedInstanceState); requestWindowFeature(Window.FEATURE_NO_TITLE); getWindow().setFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN, WindowManager.LayoutParams.FLAG_FULLSCREEN); cameraPreview = new CameraPreview(this); addContentView(cameraPreview, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); animationView = new AnimationView(this); addContentView(animationView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); crosslinesView = new CrosslinesView(this); addContentView(crosslinesView, new LayoutParams(LayoutParams.FILL_PARENT, LayoutParams.FILL_PARENT)); } 

cameraPreview is of type SurfaceView , animationView and crosslinesViews are of type View . onDraw() from the last two views is as follows:

 protected void onDraw(Canvas canvas) { // Have the view being transparent canvas.drawARGB(0, 0, 0, 0); // Your drawing code goes here // ... } 

Good luck

+2
source share

All Articles