Two SurfaceViews in a FrameLayout

8450 views
Skip to first unread message

Chris

unread,
Jul 27, 2009, 10:34:20 AM7/27/09
to Android Developers
Hi,

I have two SurfaceViews in one FrameLayout. So they are stacked upon
another. What I want to achieve is to use the lower SurfaceView as
kind of background that is changing heavily, and the upper SurfaceView
as annotation overlay. So the upper SurfaceView has a (small) number
of quickly changing elements that I will draw.

Somehow, I can always only see one of the SurfaceViews, depending on
which one is top. I thought that SurfaceViews are always transparent,
but it seems I am missing something here.

Any thoughts on why I can't overlay two SurfaceViews?

Thanks,
Chris

Dianne Hackborn

unread,
Jul 27, 2009, 2:07:49 PM7/27/09
to android-d...@googlegroups.com
Sorry, you can't do this -- because surface views are very special and not really views (the surface is a separate window Z-ordered with your own), their Z-ordering does not match your views.  A surface view is a big, heavy object; you are not intended to treat SurfaceView like a regular view in this way.

As far as transparency, the surface is transparent if you make its pixel format transparent.  It's your surface, you can do with it what you want.  The default is to be the same pixel format as the screen, since that is the most efficient and it is very rare for someone to want a transparent surface view.

The way you really are intended to do this is to own the surface, and draw all your compositing in there.  So you would just have one surface view that you are updating in the places where either the background or overlay is changing.  To be honest, I think you are going to get better performance if you do it this way anyway, especially if you use OpenGL.

For simple overlays, one thing that is reasonable to do is to stick the SurfaceView in a frame layout, Z-ordering it below other -regular- views that contain the overlay.  There is an example in ApiDemos of this.
--
Dianne Hackborn
Android framework engineer
hac...@android.com

Note: please don't send private questions to me, as I don't have time to provide private support, and so won't reply to such e-mails.  All such questions should be posted on public forums, where I and others can see and answer them.

Chris

unread,
Jul 28, 2009, 5:18:14 AM7/28/09
to Android Developers
Thanks for the indepth reply!

Maybe you as Android Framework Developer can give a recommendation on
how I best implement the following: I have camera preview and want to
overlay my own item over this that change very quickly (about every
200ms).

The camera preview uses a SurfaceView as in the samples. As first
approach I used a normal View as the overlay part and called
postInvalidate from another thread very often to redraw the View. This
caused heavy CPU load (90%) of my application. Now, I managed to use
two SurfaceViews and making one transparent. This also works and the
CPU load is about 30% now.

However: is this the best way to implement such functionality?

Thanks!
Chris
> hack...@android.com

Chris

unread,
Jul 30, 2009, 9:33:36 AM7/30/09
to Android Developers, hac...@android.com
I have looked through the docs again and find no explanation what the
best way is where I can really have two seperate views overlay and
update both at righ rates. Maybe someone can comment on the use-case
that I have described below.

Thanks!
Chris

Dianne Hackborn

unread,
Jul 30, 2009, 9:40:01 AM7/30/09
to android-d...@googlegroups.com
It's probably not the best way, but right now you simply can not have multiple overlapping surface views in a window, so the only way to achieve this is to render the overlay in the main window.  Fyi, using a separate thread and postInvalidate() is extra work that is not needed, just call invalidate() from the main thread.  And you'll want to make sure that at least the part of your view hierarchy involved with drawing the overlay is as simply as possible (not deep and not a lot of views to iterate over) to reduce the overhead as much as possible.
hac...@android.com

sonic

unread,
Sep 17, 2009, 9:55:41 AM9/17/09
to Android Developers
On 28 Jul., 11:18, Chris <chrismc...@hotmail.com> wrote:
> Now, I managed to use two SurfaceViews and making one transparent.

It would be very nice if you could post some code showing how you do
this!

SChaser

unread,
Sep 20, 2009, 9:14:11 PM9/20/09
to Android Developers
How would one render the overlay in the main window while the other
views are active (or at all, for that matter)? I am interesting in
overlaying OpenGL over a camera preview. What is the way to get the
GLSurfaceView object when it is an overlay?

Dianne Hackborn

unread,
Sep 20, 2009, 9:56:29 PM9/20/09
to android-d...@googlegroups.com
Multiple active overlapping surface views, of any sort, are not currently supported by the framework.  You may get them to work, but it is mostly due to luck -- the view hierarchy does not define the Z-ordering of those surfaces, nor try to ensure they are Z-ordered in any particular way, so this may change for whatever reason.

SChaser

unread,
Sep 20, 2009, 10:54:20 PM9/20/09
to Android Developers
Thanks for the prompt response.

How does one do augmented reality, then?

On Sep 20, 6:56 pm, Dianne Hackborn <hack...@android.com> wrote:
> Multiple active overlapping surface views, of any sort, are not currently
> supported by the framework.  You may get them to work, but it is mostly due
> to luck -- the view hierarchy does not define the Z-ordering of those
> surfaces, nor try to ensure they are Z-ordered in any particular way, so
> this may change for whatever reason.
>
> On Sun, Sep 20, 2009 at 6:14 PM, SChaser <crotalistig...@gmail.com> wrote:
>
> > How would one render the overlay in the main window while the other
> > views are active (or at all, for that matter)? I am interesting in
> > overlaying OpenGL over a camera preview. What is the way to get the
> > GLSurfaceView object when it is an overlay?
>
> > On Jul 30, 6:40 am, Dianne Hackborn <hack...@android.com> wrote:
> > > It's probably not the best way, but right now you simply can not have
> > > multiple overlapping surface views in a window, so the only way to
> > achieve
> > > this is to render the overlay in the main window.
>
> --
> Dianne Hackborn
> Android framework engineer
> hack...@android.com
Reply all
Reply to author
Forward
0 new messages