I have a simple app with graphics moving about on a canvas, implemented in a surfaceview, i.e. not using an xml layout.
Is there an easy way to use the camera preview as the background?
You could use a FrameLayout and put first your surfaceView as a child and then some other view with no/transparent background.
A FrameLayout draws its children in the order of adding.
Related
I have a function that will keep on drawing on the surfaceView to simulate the animation of a character on the surfaceView. But after adding a imageView on top of the SurfaceView, the SurfaceView stop drawing.
setZOrderOnTop(true) you have added in the surfaceview init.
because surfaceview should be always on top to draw anything,
Although after that you can't put image on top of that, for that you can use other question ref
add image to surface view in android
for that. let me know if that helps
I am trying to build a simple game. I have a SurfaceView, on which I draw the background for the game:
Canvas canvas = surfaceHolder.lockCanvas();
//draw on canvas....
surfaceHolder.unlockCanvasAndPost(canvas);
I have a View that I want to 'layer' on top of the SurfaceView, but I still want to be able to see the background underneath it. How do I draw the view on top of the SurfaceView. The View has its own onDraw method, with an associated canvas, so I'm assuming that I need to place the View on top of the SurfaceView where I drew the background?
There may be a more efficient way to to do this, but when it comes to stacking views on top of one another, you could put the SurfaceView inside a FrameLayout, and then put the view you want on top after the SurfaceView in the XML, but still inside the FrameLayout (meaning it's drawn later, and thus on top). This will accomplish the stacking you desire.
If you wish to position the view that will go on top of the SurfaceView anywhere else than exactly matching it, or the top left, then you could use a RelativeLayout where you layout the SurfaceView with alightParentLeft, alightParentTop, alignParentBottom, and alightParentRight all set to true. Then again having the view you want drawn on top coming after the SurfaceView in xml will draw it on top, and you can position it wherever you want.
Now as for you wanting to the view on top of the SurfaceView to be see through, you can do that by setting it's alpha value. if you are aiming for 3.0+ you can just use a view's .setAlpha method, but if you are aiming for lower, you could set a zero millisecond AlphaAnimation to set the alpha of the view (There may be a better way to do this, but this is the only way I to set alpha on views pre honeycomb).
Hope this helps! Best of luck!
Hi im having a parent viewgroup which has a background, contains a openglview as a part of that viewgroup.
What I want exactly is, i need a openglview with a transparent background and I want to able to see the parent background and I want to be able to write text on top of openglview, I dont want to use textures.
I tried this options,
Setting the openglview zorderontop as true. It makes my text to go behind the glview. If im not making zorderontop true, the background is black.
Setting the transluent theme to the activity which makes my glview and also the parent view group to become transparent.
I tried to inflate the layout contains the glview and use ContexThemeWrapper class to change the theme of the glview dynamically. But setTheme() not working at runtime. only applying theme in manifest file it works.
Applying the layout backgroud to transparent also didnt work, still black background.
The problem is that you're drawing to two separate surfaces: one surface created by ViewRoot, and one surface created for the GLSurfaceView. These surfaces are composited by SurfaceFlinger, one on top of the other. (By default the ViewRoot surface will be on top and the GLSurfaceView surface will be on bottom.)
Since all of your Views are drawn into the ViewRoot's surface, they must all be on top of the OpenGL surface or beneath the OpenGL surface.
If you don't want to use textures, period, your only choice is to create a third surface, this time with SurfaceView instead of GLSurfaceView, to draw your text above the GLSurfaceView.
In Android 2.2, I want to display a few sprites and clickable animations over an existing view. I just discovered that SurfaceView cannot be transparent
I tried overriding dispatchDraw() of a LinearLayout but that doesn't seem to be callable via a Runnable Thread.
I also tried to extend Canvas directly, but that quickly turned into a mess when trying to place it in my XML layout.
Do I have to use GLSurfaceView to have a drawing view whose background is transparent?
Optimally, I'd like to just have a Canvas on which I can draw Bitmap at various coordinates, instead of dealing with the details of GL (unless GL is the only way I can do transparency).
Silly Rabbit. Just use Views and move them around wherever you want within a FrameLayout. SurfaceView is not designed for what you're trying to do.
I'm going to try Switching from Canvas to OpenGL and toss in parts of TranslucentGLSurfaceView.
I'm still open to selecting an answer that doesn't require OpenGL, just for the sake of it solving my original question.
How do I write code which layouts UI elements (Buttons, etc) over camera preview on Android ?
Put the SurfaceView in a container that allows for Z-axis layering, such as RelativeLayout or FrameLayout. Put the things to appear on top of the SurfaceView later in the XML -- later children of a parent will draw over top of earlier children in the parent.
Here is a project using a SurfaceView for video playback that demonstrates the technique. The same concepts should hold for a SurfaceView for camera preview.
Bear in mind that extra work needs to be done by Android to blend your widgets with the preview, so your preview frame rate may drop.