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
544
votes
23 answers

How to have a transparent ImageButton: Android

This is what I tried to get a…
Namratha
  • 16,630
  • 27
  • 90
  • 125
188
votes
10 answers

Error inflating when extending a class

I'm trying to create a custom view GhostSurfaceCameraView that extends SurfaceView. Here's my class definition file GhostSurfaceCameraView.java: public class GhostSurfaceCameraView extends SurfaceView implements SurfaceHolder.Callback { …
eccentricbiped
  • 5,701
  • 3
  • 16
  • 8
143
votes
15 answers

Android Camera Preview Stretched

I've been working on making my custom camera activity on Android, but when rotating the camera, the aspect ratio of the surface view gets messed up. In my oncreate for the activity, I set the framelayout which holds the surface view that displays…
scientiffic
  • 9,045
  • 18
  • 76
  • 149
124
votes
3 answers

Understanding Canvas and Surface concepts

I'm struggling to understand the process of drawing to SurfaceView and therefore the whole Surface/Canvas/Bitmap system, which is used in Android. I've read all articles and API documentation pages, which I was able to find on android-developers…
Fyodor
  • 2,793
  • 4
  • 21
  • 24
111
votes
21 answers

Android, canvas: How do I clear (delete contents of) a canvas (= bitmaps), living in a surfaceView?

In order to make a simple game, I used a template that draws a canvas with bitmaps like this: private void doDraw(Canvas canvas) { for (int i=0;i<8;i++) for (int j=0;j<9;j++) for (int k=0;k<7;k++) { …
samClem
  • 1,117
  • 2
  • 8
  • 6
91
votes
4 answers

how to make surfaceview transparent

Hello all i want to make my DrawingSurface view transparent. i tried many thing but it's not working. Here is my xml code to make my surface view transparent
Sunil Pandey
  • 7,042
  • 7
  • 35
  • 48
91
votes
7 answers

How to set Android camera orientation properly?

I want to set the camera orientation according to the device orientation in Android but nothing seems to be working. I tried to rotate the Surface as well as the camera parameters but the camera preview in portrait mode always comes upside down. I…
Abhinav
  • 38,516
  • 9
  • 41
  • 49
80
votes
12 answers

Fragments - The specified child already has a parent. You must call removeView() on the child's parent first

I am getting this error. I tried many solutions but I coudn't solve this. Help me! I need to add the surface view and the button to the activity using fragments. CamActivity.java: public class CamActivity extends FragmentActivity { @Override …
Vivek
  • 911
  • 1
  • 7
  • 9
73
votes
5 answers

How SurfaceHolder callbacks are related to Activity lifecycle?

I've been trying to implement an application that requires camera preview on a surface. As I see the things, both activity and surface lifecycles consist of the following states: When I first launch my Activity:…
krugloid
  • 833
  • 1
  • 8
  • 5
72
votes
3 answers

How to draw an overlay on a SurfaceView used by Camera on Android?

I have a simple program that draws the preview of the Camera into a SurfaceView. What I'm trying to do is using the onPreviewFrame method, which is invoked each time a new frame is drawn into the SurfaceView, in order to execute the invalidate…
Cristian
  • 198,401
  • 62
  • 356
  • 264
56
votes
16 answers

Moving MapFragment (SurfaceView) causes black background flickering

I'm trying to implement new Android Google Maps API (v2). However it doesn't seem to go well with SlidingMenu. As you may know, MapFragment implementation is based on SurfaceView. The problem is that the SurfaceView doesn't like moving it around - I…
47
votes
6 answers

What's differences between Surfaceview and TextureView?

I've been studying Android especially View system. I have a question differences between them but there is no documents or references in my mother language. So I want to know from you guys.
Won Chul Jo
  • 763
  • 2
  • 7
  • 7
47
votes
6 answers

How can I set camera preview size to squared aspect ratio in a squared SurfaceView (like Instagram)

I'm trying to develop my own camera activity, but I have a problem that I'm not unable to solve... What I want, is something very similiar to instagram photo frame, and this is what I get: When I should get something like this: and... when I…
cesards
  • 15,882
  • 11
  • 70
  • 65
43
votes
1 answer

What can I do when the BufferQueue has been abandoned?

I am using a texture view to show the preview of the camera in my android app. What I noticed, however, is that every time my app gets paused, I am getting this error: 03-18 18:23:44.315: W/BufferQueue(19582): [unnamed-19582-20] cancelBuffer:…
user2426316
  • 7,131
  • 20
  • 52
  • 83
41
votes
5 answers

Android Take Screenshot of Surface View Shows Black Screen

I am attempting to Take a Screenshot of my Game through code and Share it through an Intent. I able to do of those things, however the screenshot always appears black. Here is the Code Related to Sharing the Screenshot: View view =…
user3011902
1
2 3
99 100