I have a simple screen with a start button. When the Start button is pressed, I want to go to a new Screen with a SurfaceView to show the Camera in.
Everything works fine, but the Camera takes a while to load, and this gives me a black screen.
I would like the new layout to load. And than start the camera after it has been loaded…
Therefor, I do all Camera loading in a background thread, but still, I get a black screen…
Here’s my layout:
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout android:id="@+id/RelativeLayout1"
xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="vertical" android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:background="@drawable/blue_bg">
<SurfaceView
android:id="@+id/surface_camera"
android:layout_width="fill_parent"
android:layout_height="fill_parent"
android:layout_marginLeft="25dp"
android:layout_marginRight="25dp"
android:layout_below="@id/scan_header"
android:layout_above="@id/scan_footer">
</SurfaceView>
</RelativeLayout>
Here is the method from my Activity, which loads the new view:
private void setContent()
{
setContentView(R.layout.scan)
Thread t = new Thread()
{
public void run()
{
final SurfaceView mSurfaceView = (SurfaceView)findViewById(R.id.surface_camera);
final SurfaceHolder mSurfaceHolder = mSurfaceView.getHolder();
try
{
cameraView = new CameraView();
mSurfaceHolder.addCallback(cameraView);
cameraView.setPictureListener(SunpluggedActivity.this);
mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
} catch(Exception e)
{
Log.d(TAG, "Another exception");
e.printStackTrace();
}
}
};
t.start();
}
How come, the new layout is not shown, until the thread has finished loading the camera?
EDIT: I’ve tried Thread.sleep(200) within the Thread to sleep for some time… When I do that, the new Layout is shown immedeately, but the Camera never starts…
Allright, the problem is that I used my SurfaceView within the xml layout.
The moment you call: setContentView(your_layout) -> the XML file is inflated.
That means, the SurfaceView is inflated as well. That, again, means that the SurfaceView onSurfaceCreated Methods is called, which triggers opening the Camera etc.
So, this whole process takes a while, hence, your previous Activity (e.g. the one launching the Activity with the SurfaceView) seems to be unresponsive…
My solution, of creating the CameraView in a BG thread solves the inresponsiveness. But failed to show the Camera output in the SurfaceView.
The solution is to remove your SurfaceView from your xml. This will start your activity immedeately (since the SurfaceView & Camera are not instantiated).
Once your new Activities layout is loaded, you can programmatically add a new SurfaceView to your screen. Off course, this takes time as well, but your UI switches to the new activity quickly, and you can show a loader while the SurfaceView and Camera are loading!
SO: REMOVE THE SURFACEVIEW FROM THE XML -> ADD IT PROGRAMATICALLY:
Launch Activity:
Main.xml (just a button to launch the new activity)
Here’s the Second Activity (Which contains the SurfaceView)
And here’s the second Activity’s layout (WITHOUT A SURFACEVIEW)
Finally, to complete the answer, here’s the code for the CameraView(). It really is just a simple implementation to get open the Camera and Display the contents: