Difference between SurfaceView and View?
Asked Answered
O

7

226

When is it necessary, or better to use a SurfaceView instead of a View?

Oldline answered 7/8, 2009 at 7:31 Comment(0)
C
227

Views are all drawn on the same GUI thread which is also used for all user interaction.

So if you need to update GUI rapidly or if the rendering takes too much time and affects user experience then use SurfaceView.

Clerical answered 7/8, 2009 at 7:36 Comment(4)
Check pierr's answer which contains more detail.Cellule
Official detail answer: developer.android.com/guide/topics/graphics/2d-graphics.htmlCellule
It's no longer quite this simple. Check pierr's answer; it has a link to the detailed architecture doc (source.android.com/devices/graphics/architecture.html), and explains why hardware acceleration of Canvas rendering can make custom Views a better choice.Prizewinner
When to use FrameLayout to stack views, instead of SurfaceView?Safekeeping
I
108

A few things I've noted:

  • SurfaceViews contain a nice rendering mechanism that allows threads to update the surface's content without using a handler (good for animation).
  • Surfaceviews cannot be transparent, they can only appear behind other elements in the view hierarchy.
  • I've found that they are much faster for animation than rendering onto a View.

For more information (and a great usage example) refer to the LunarLander project in the SDK 's examples section.

Insurgency answered 9/8, 2009 at 21:9 Comment(2)
FYI...A SurfaceView can now be transparent: #5391589Dessiatine
@Insurgency : What do you mean by Surface Views cannot be transparent? Can other views overlap the Surface view? For example, can I display a ListView on a Surface view temporarily?Tridimensional
W
86

updated 05/09/2014

OK. We have official document now. It talked all I have mentioned, in a better way.


Read more detailed here.

Yes, the main difference is surfaceView can be updated on the background thread. However, there are more you might care.

  • surfaceView has dedicate surface buffer while all the view shares one surface buffer that is allocated by ViewRoot. In another word, surfaceView cost more resources.

  • surfaceView cannot be hardware accelerated (as of JB4.2) while 95% operations on normal View are HW accelerated using openGL ES.

  • More work should be done to create your customized surfaceView. You need to listener to the surfaceCreated/Destroy Event, create an render thread, more importantly, synchronized the render thread and main thread. However, to customize the View, all you need to do is override onDraw method.

  • The timing to update is different. Normal view update mechanism is constraint or controlled by the framework:You call view.invalidate in the UI thread or view.postInvalid in other thread to indicate to the framework that the view should be updated. However, the view won't be updated immediately but wait until next VSYNC event arrived. The easy approach to understand VSYNC is to consider it is as a timer that fire up every 16ms for a 60fps screen. In Android, all the normal view update (and display actually but I won't talk it today), is synchronized with VSYNC to achieve better smoothness. Now,back to the surfaceView, you can render it anytime as you wish. However, I can hardly tell if it is an advantage, since the display is also synchronized with VSYNC, as stated previously.
Walli answered 14/4, 2013 at 3:8 Comment(3)
From your answer I get the feeling it is better to use a class derived from View than SurfaceView. Or am I getting something wrong? This would be opposed to the majority of 2D game development tutorials.Flowage
@Flowage something to take into consideration is that a SurfaceView by design shouldn't block up the UI Thread where a View can only be modified by the UI Thread. With most games, SurfaceViews will be doing a good bit of rendering which would block up the UI Thread with a simple view. That is the primary benefit, from my understanding, of a SurfaceView.Teetotal
What do you mean by create an render thread. We have to create this manually every time?Safekeeping
C
51

The main difference is that SurfaceView can be drawn on by background theads but Views can't. SurfaceViews use more resources though so you don't want to use them unless you have to.

Coadjutant answered 7/8, 2009 at 7:34 Comment(4)
"They use more resources though so you don't want to use them unless you have to." - Who uses more resources? Views or SurfaceViews?Raquel
Surfaceview use more resources than viewsAlroy
@RiteshGune how much?Skippie
When do we have to?Safekeeping
V
18

A SurfaceView is a custom view in Android that can be used to drawn inside it.

The main difference between a View and a SurfaceView is that a View is drawn in the UI Thread, which is used for all the user interaction.

If you want to update the UI rapidly enough and render a good amount of information in it, a SurfaceView is a better choice.

But there are a few technical insides to the SurfaceView:

1. They are not hardware accelerated.

2. Normal views are rendered when you call the methods invalidate or postInvalidate(), but this does not mean the view will be immediately updated (A VSYNC will be sent, and the OS decides when it gets updated. The SurfaceView can be immediately updated.

3. A SurfaceView has an allocated surface buffer, so it is more costly

Valueless answered 9/2, 2016 at 16:20 Comment(1)
Why is it important to have hardware accelerated?Safekeeping
S
13

One of the main differences between surfaceview and view is that to refresh the screen for a normal view we have to call invalidate method from the same thread where the view is defined. But even if we call invalidate, the refreshing does not happen immediately. It occurs only after the next arrival of the VSYNC signal. VSYNC signal is a kernel generated signal which happens every 16.6 ms or this is also known as 60 frame per second. So if we want more control over the refreshing of the screen (for example for very fast moving animation), we should not use normal view class.

On the other hand in case of surfaceview, we can refresh the screen as fast as we want and we can do it from a background thread. So refreshing of the surfaceview really does not depend upon VSYNC, and this is very useful if we want to do high speed animation. I have few training videos and example application which explain all these things nicely. Please have a look at the following training videos.

https://youtu.be/kRqsoApOr9U

https://youtu.be/Ji84HJ85FIQ

https://youtu.be/U8igPoyrUf8

Stereobate answered 18/8, 2017 at 0:25 Comment(0)
S
1

Why use SurfaceView and not the classic View class...

One main reason is that SurfaceView can rapidly render the screen.

In simple words a SV is more capable of managing the timing and render animations.

To have a better understanding what is a SurfaceView we must compare it with the View class.

What is the difference... check this simple explanation in the video

https://m.youtube.com/watch?feature=youtu.be&v=eltlqsHSG30

Well with the View we have one major problem....the timing of rendering animations.

Normally the onDraw() is called from the Android run-time system.

So, when Android run-time system calls onDraw() then the application cant control

the timing of display, and this is important for animation. We have a gap of timing

between the application (our game) and the Android run-time system.

The SV it can call the onDraw() by a dedicated Thread.

Thus: the application controls the timing. So we can display the next bitmap image of the animation.

Suazo answered 24/10, 2018 at 11:58 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.