一切正常,但相机需要一段时间才能加载,这给了我一个黑屏.
我想加载新的布局.并且在装载后启动相机…@H_404_4@
因此,我在后台线程中完成所有相机加载,但仍然,我得到一个黑屏……
这是我的布局:@H_404_4@
<?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>
这是我的Activity中的方法,它加载新视图:@H_404_4@
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();}
为什么新的布局没有显示,直到线程完成加载相机?@H_404_4@
编辑:我已经尝试Thread中的Thread.sleep(200)睡了一段时间…当我这样做时,新的布局显示出来,但相机永远不会启动…@H_404_4@解决方法 好吧,问题是我在xml布局中使用了SurfaceVIEw.
你打电话的那一刻:setContentVIEw(your_layout) – > XML文件被夸大了.
这意味着,SurfaceVIEw也会膨胀.这再次意味着调用SurfaceVIEw onSurfaceCreated方法,触发打开相机等.
因此,整个过程需要一段时间,因此,您之前的Activity(例如,使用SurfaceVIEw启动Activity的那个)似乎没有响应……@H_404_4@
我在BG线程中创建Cameraview的解决方案解决了无响应问题.但未能在SurfaceVIEw中显示Camera输出.@H_404_4@
解决方案是从xml中删除SurfaceVIEw.这将立即启动您的活动(因为SurfaceVIEw& Camera未实例化).
加载新的“活动”布局后,可以以编程方式将新的SurfaceVIEw添加到屏幕上.当然,这也需要时间,但您的UI会快速切换到新活动,并且您可以在SurfaceVIEw和Camera加载时显示加载程序!@H_404_4@
所以:从XML中删除SURFACEVIEW – >在程序上添加它:
发布活动:@H_404_4@
public class Launch extends Activity implements OnClickListener { @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); button btn = (button)findVIEwByID(R.ID.button1); btn.setonClickListener(this); } @OverrIDe public voID onClick(VIEw v) { Intent intent = new Intent(Launch.this,SurfaceTestActivity.class); startActivity(intent); }}
Main.xml(只是一个启动新活动的按钮)@H_404_4@
<?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="#ff6600"> <button androID:ID="@+ID/button1" androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="button" /></relativeLayout>
这是第二个活动(包含SurfaceVIEw)@H_404_4@
public class SurfaceTestActivity extends Activity { private Context mContext; private Cameraview cameraview; private Handler mHandler = new Handler(); private final Runnable mloadCamera = new Runnable() { public voID run() { startCamera(); } }; /** Called when the activity is first created. */ @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContent(); mContext = getApplicationContext(); } private voID startCamera() { relativeLayout rl = (relativeLayout)findVIEwByID(R.ID.surface_camera); SurfaceVIEw surfaceVIEw = new SurfaceVIEw(mContext); final SurfaceHolder mSurfaceHolder = surfaceVIEw.getHolder(); try { cameraview = new Cameraview(); mSurfaceHolder.addCallback(cameraview); mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); } catch(Exception e) { Log.d("deBUG","Another exception"); e.printstacktrace(); } if(rl != null && surfaceVIEw != null) rl.addVIEw(surfaceVIEw); } private voID setContent() { setContentVIEw(R.layout.scan); // Post the Runnable with a Slight delay -> than your layout will be // shown. Without the delay -> your UI will feel inresponsive mHandler.postDelayed(mloadCamera,100); }}
这是第二个Activity的布局(没有SURFACEVIEW)@H_404_4@
<?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="#ff6600"> <relativeLayout androID:ID="@+ID/header" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Explanation Txt"></TextVIEw> </relativeLayout> <relativeLayout androID:ID="@+ID/footer" androID:layout_wIDth="fill_parent" androID:layout_height="wrap_content" androID:layout_alignParentBottom="true"> <TextVIEw androID:layout_wIDth="wrap_content" androID:layout_height="wrap_content" androID:text="Explanation Txt"></TextVIEw> </relativeLayout> <relativeLayout androID:ID="@+ID/surface_camera" androID:layout_wIDth="fill_parent" androID:layout_height="match_parent" androID:layout_above="@+ID/footer" androID:layout_below="@+ID/header" androID:background="#ff0066"> </relativeLayout></relativeLayout>
最后,为了完成答案,这里是Cameraview()的代码.打开相机并显示内容真的只是一个简单的实现:@H_404_4@
public class Cameraview implements SurfaceHolder.Callback{ // Variables private Camera mCamera = null; private boolean mPrevIEwRunning = false; private boolean mProcessing = false; private int mWIDth = 0; private int mHeight = 0; @OverrIDe public voID surfaceChanged(SurfaceHolder holder,int format,int wIDth,int height) { if(mPrevIEwRunning ) { mCamera.stopPrevIEw(); } // Store wIDth and height mWIDth = wIDth; mHeight = height; // Set camera parameters Camera.Parameters p = mCamera.getParameters(); mCamera.setParameters(p); if(androID.os.Build.VERSION.SDK_INT >= 8) { // If API >= 8 -> rotate display... mCamera.setdisplayOrIEntation(90); } try { mCamera.setPrevIEwdisplay(holder); } catch(IOException e) { e.printstacktrace(); } mCamera.startPrevIEw(); mPrevIEwRunning = true; } @OverrIDe public voID surfaceCreated(final SurfaceHolder holder) { try { mCamera = Camera.open(); mCamera.setPrevIEwdisplay(holder); } catch (IOException e) { mCamera.release(); mCamera = null; e.printstacktrace(); } } @OverrIDe public voID surfaceDestroyed(SurfaceHolder holder) { if(mCamera != null) { mCamera.setPrevIEwCallback(null); mCamera.stopPrevIEw(); mCamera.setPrevIEwCallback(null); mPrevIEwRunning = false; mCamera.release(); mCamera = null; } }}总结
以上是内存溢出为你收集整理的android – 在SurfaceView加载Camera Preview时显示我的布局全部内容,希望文章能够帮你解决android – 在SurfaceView加载Camera Preview时显示我的布局所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)