Questions tagged [surfaceview]

SurfaceView is a widget on the Android platform which provides a dedicated drawing surface.

Android's SurfaceView class provides a dedicated drawing surface embedded inside of a View hierarchy. You can control the format of this surface and, if you like, its size; the SurfaceView takes care of placing the surface at the correct location on the screen.

A SurfaceView has two parts: the Surface, and the View. The Surface is a separate layer, independent of the View UI layer; by default it is positioned behind the View layer. The View works like any other View element, and is usually completely transparent, serving only to interact with other View elements during layout. In some situations it can be useful to draw on the View (e.g. for a static mask).

Because the Surface is an independent layer, it can be updated outside of the usual invalidate/refresh View cycle, on a dedicated thread.

Allocation of a display Surface is performed by the Window Manager. This requires an IPC round-trip, so the availability of and changes to Surfaces are reported through callbacks. This also makes it difficult to move a SurfaceView around smoothly, as the position of the View and position of the Surface may not update on the same frame.

Other View elements can be positioned to overlap the SurfaceView, including interactive elements like buttons. The Surface's Z-order can be altered to place it on top of the View layer. It isn't possible to sandwich the Surface between View elements; if you need that, consider TextureView instead.

The transparent region that makes the surface visible is based on the layout positions in the view hierarchy. If the post-layout transform properties are used to draw a sibling view on top of the SurfaceView, the view may not be properly composited with the surface.

For many applications, a custom view is a better choice.

Detailed information about SurfaceView can be found in the system-level graphics architecture document.

2733 questions
20
votes
1 answer

Make a SurfaceView larger than the screen (Fitting a camera preview to a SurfaceView larger than the display)

I have a custom camera application and I want for any preview sizes to display it in full screen mode without stretching the camera preview image. For this, I need to make the surfaceView larger than the screen in order to keep aspect ratio, so…
Paul
  • 3,812
  • 10
  • 50
  • 73
20
votes
5 answers

Camera Tutorial for Android (using surfaceview)

Here is my example code: package newslab.video.server; import android.app.Activity; import android.hardware.Camera; import android.os.Bundle; import android.util.Log; import android.view.SurfaceHolder; import android.view.SurfaceView; import…
jjLin
  • 3,281
  • 8
  • 32
  • 55
20
votes
3 answers

Difference between SurfaceView and GLSurfaceView in Android

Can anyone tell me what the basic difference is between SurfaceView and GLSurfaceView? When should I use SurfaceView, and when should I use GLSurfaceView? I read some already answered questions on Stack Overflow, but they did not satisfy my…
AndroDev
  • 3,236
  • 8
  • 35
  • 49
19
votes
3 answers

ANR in SurfaceView on specific devices only -- The only fix is a short sleep time

In my Android application, I use a SurfaceView to draw things. It has been working fine on thousands of devices -- except that now users started reporting ANRs on the following devices: LG G4 Android 5.1 3 GB RAM 5.5" display 2560 x 1440 px…
caw
  • 30,999
  • 61
  • 181
  • 291
19
votes
5 answers

Annoying lags/stutters in an android game

I just started with game development in android, and I'm working on a super simple game. The game is basically like flappy bird. I managed to get everything to work, but I get a lot of stutters and lags. The phone I'm using for testing is LG G2,…
Asaf
  • 2,005
  • 7
  • 37
  • 59
19
votes
7 answers

The surface has been released when I try to setDisplay to MediaPlayer

My xml file: My function to setDisplay: public void playVideo() { MediaPlayer mp =…
Cooosuper
  • 193
  • 1
  • 1
  • 5
19
votes
3 answers

Camera preview stretched even after setting the correct dimensions

I'm creating a camera app that implements it's own camera preview for taking pictures. The app is currently forced into portrait mode. My problem is that the preview from the camera is slightly stretched (the aspect ratio is a bit off). The funny…
Paul
  • 5,163
  • 3
  • 35
  • 43
19
votes
3 answers

Android blur surfaceview used in camera

I have a camera preview in my android app. As you all probably know it is implemented by a surfaceview in android. In my photo app, which allows users to take pictures, I want to blur the camera preview (the surface view) if the user has not logged…
user1233587
  • 2,033
  • 4
  • 24
  • 24
18
votes
1 answer

Custom camera android

I want to use camera preview in an activity. I want to add images(transparent frames on surface view). I tried following code so that i can easily customize the xml layout desirably: package com.demo; import java.io.IOException; import…
Seshu Vinay
  • 13,560
  • 9
  • 60
  • 109
18
votes
2 answers

How to show the camera preview on a SurfaceView?

I am trying to open the camera hardware on a SurfaceView. In the layout, I created a SurfaceView and I open the camera as shown in the code below. When I run the code, the toast in the CameraAvailableCB shows up and says "onCameraAvailable" but…
Amrmsmb
  • 1
  • 27
  • 104
  • 226
18
votes
5 answers

Android zxing - portrait camera preview/surfaceview is stretched/warped

I have managed to use the answer here to rotate the camera preview to portrait: http://code.google.com/p/zxing/issues/detail?id=178#c46 however the preview itself is stretched/warped - the height appears to be stretching to fill the box (but it…
AndroidNoob
  • 2,771
  • 2
  • 40
  • 53
17
votes
0 answers

SurfaceView - Timestamp seems implausible relative to expectedPresent

For my barcode scanner app, I am leveraging parts of the CameraSourcePreview class found in the Google Vision sample code. I then leverage this class in my Fragment using this XML…
mattdonders
  • 1,328
  • 1
  • 19
  • 42
17
votes
1 answer

WaitingInMainSignalCatcherLoop Error in Android Application

I have an android application that refreshes the screen every 33 milliseconds, displaying a rectangle at a pair of coordinates. Here is the code for the custom view: import android.content.Context; import android.graphics.Bitmap; import…
WD40
  • 429
  • 1
  • 7
  • 19
16
votes
2 answers

How to resize a bitmap eficiently and with out losing quality in android

I have a Bitmap with a size of 320x480 and I need to stretch it on different device screens, I tried using this: Rect dstRect = new Rect(); canvas.getClipBounds(dstRect); canvas.drawBitmap(frameBuffer, null, dstRect, null); it works, the image…
user924941
  • 943
  • 3
  • 12
  • 24
16
votes
3 answers

Android Multiple SurfaceViews

I'm trying to work with 3 SurfaceViews on one screen, one on top half (BoardView), one on bottom half (StatusView), and the last one as an extra layer above the top half (TileView) (see main.xml). I created a class MySurfaceView, which is extended…
nhaarman
  • 98,571
  • 55
  • 246
  • 278