在main.xml中显示自定义surfaceview

时间:2012-02-01 20:58:09

标签: android surfaceview

我有一个自定义的surfaceview,我需要添加到我当前的main.xml布局中。此surfaceview用于流式摄像机实时视图。我有问题做这项工作。

如果我将代码作为

运行,我没有任何问题
     cameraPreview = new HttpCameraPreview(this, null, viewWidth, viewHeight);
setContentView(cameraPreview);

但是,我不希望我的自定义surfaceview成为主视图。我希望它是我的main.xml中的surfaceview。

希望得到一些建议/建议。我试着参考这个链接,但也没有帮助。 Draw SurfaceView from layout xml

谢谢!

main.xml的一部分如下:

<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
          android:orientation="vertical"
          android:layout_width="fill_parent"
          android:layout_height="fill_parent">
                    <ImageView
               android:id="@+id/preview_image_view"
               android:layout_height="wrap_content"
               android:layout_weight="3.0"
                   android:layout_width="fill_parent"
               />
<SurfaceView Class ="org.saboteur.nikonshooter.HttpCameraPreview"
android:layout_width="fill_parent" 
android:id="@+id/surface_preview" 
android:layout_height="10dip" 
android:layout_weight="3"/>

我的自定义surfaceview类部分代码如下所示:

  public class HttpCameraPreview extends SurfaceView implements SurfaceHolder.Callback {
    private static final String TAG = "test";
    private static final String url = "http://bijint.com/jp/tokei_images/0022.jpg";
    private CanvasThread canvasThread;

    private SurfaceHolder holder;
    private HttpCamera camera;

    private HttpCameraPreview mSurfaceView;

    private int viewWidth;
    private int viewHeight;

    public HttpCameraPreview(Context context, AttributeSet attributeSet, int viewWidth, int viewHeight) {
        super(context, attributeSet);
        Log.e("HttpCameraPreview", "inside HttpCameraPreview()");
        Log.e("HttpCameraPreview", "create surface view to r.id");


        try{
        holder = getHolder();
        holder.addCallback(this);
        holder.setType(SurfaceHolder.SURFACE_TYPE_NORMAL);
        this.viewWidth = viewWidth;
        this.viewHeight = viewHeight;
        canvasThread = new CanvasThread();
        catch (Exception e){
            Log.e("HttpCameraPreview", "--->Exception = "+e);
        }
    }

1 个答案:

答案 0 :(得分:-1)

为什么不以编程方式将HttpCameraView添加到main.xml中的布局。给你的LinearLayout一个像“@ + id / parent_view”的id,然后通过

调用它
LinearLayout parentView = (LinearLayout) findViewById(R.id.parent_view);
cameraPreview = new HttpCameraPreview(this, null, viewWidth, viewHeight);
parentView.addView( cameraPreview );