Insights from Our Experts

Blog image

View versus SurfaceView in Android

What ?

View or surfaceview in android comes in to the screen only when you think about a custom design in the android layout other than the built-in widgets provided by android. In a custom view/surface view we can draw images lines or shapes as we require. Also the format can be controlled pragmatically, also the size .

How ?

View

To implement a custom view you have to create a class which extends View, View is the base class for all the building blocks in android like the buttons or textvews or any other widgets.
Here you have to override the methods of the View and write your own implementation to it, not all the methods are compulsory, but the onDraw(Canvas canvas) should be overriden compulsorily, this is the method which actually does the drawing of your custom view. You can handle the touch events from the view by implementing onClickListener.
please see the below code snippet which shows how to write a custom view.

public class CustomView extends View
There are two constructors to the custom view class.

public CustomView(Context context)
{
super(context);
}
This is called when you initialize a view from the code

View myview = new CustomView(context);
 
public CustomView(Context context, AttributeSet attrs){
 
super(context, attrs);
 
}
This constructor is called when a view is called from the layout.

onDraw method will draw the view in a canvas.

@Override
 
protected void onDraw(Canvas canvas) {
 
canvas.drawBitmap(srcBitmap,left,top,paint);// draw the bitmap image or shapes here
}
In the layout xml you have to include your custom view just like the other widgets

Surface View

Create the class which extends surface view .

public class CustomSurface extends SurfaceView implements SurfaceHolder.Callback
{
 
write the constructor :
 
public Panel(Context context, AttributeSet attrs) {
super(context, attrs);
getHolder().addCallback(this);
 
}
Implement the onDraw method

@Override
public void onDraw(Canvas canvas) {
canvas.drawColor(Color.BLACK);
canvas.drawBitmap(bitmap, left, top, paint);
}
override the methods

public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
// TODO Auto-generated method stub
}
 
@Override
public void surfaceCreated(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
 
@Override
public void surfaceDestroyed(SurfaceHolder holder) {
// TODO Auto-generated method stub
}
Now we need a thread which will controll the drawings

@Override
public void run() {
Canvas c;
while (_run) {
c = null;
try {
c = _surfaceHolder.lockCanvas(null);
synchronized (_surfaceHolder) {
_panel.onDraw(c);
}
} finally {
// do this in a finally so that if an exception is thrown
// during the above, we don't leave the Surface in an
// inconsistent state
if (c != null) {
_surfaceHolder.unlockCanvasAndPost(c);
}
}
}
}
Now you can include the surface in a layout just like the custom view, or you can set the content as a surface view

setContentView(new CustomSurface(context));
Difference*

The main difference in a View and SurfaceView is the drawing threads, a View is drawn in the android UI thread and SurfaceView needs to be drawn in a separate thread which runs in synchronous with the ui thread.
It is mainly preferred when the surface requires more animations ad event handling which may not work smoothly in a view which is in the UI thread.
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.
SurfaceView can be drawn on by background theads but Views can’t. They use more resources though so you don’t want to use them unless you have to.