2012-03-13 75 views
0

嗨如何使此代码的作品?我想要这个类CameraSurfaceView放入一个相对的布局?Android布局:如何将此ViewGroup放入RelativeLayout中?

代码:

public class CameraSurfaceView extends ViewGroup implements SurfaceHolder.Callback{ 
    private final String TAG = "Preview"; 

    private SurfaceView mSurfaceView; 
    private SurfaceHolder mHolder; 
    private Size mPreviewSize; 
    private List<Size> mSupportedPreviewSizes; 
    private Camera mCamera; 

    public CameraSurfaceView(Context context) { 
     super(context); 

     mSurfaceView = new SurfaceView(context); 
     addView(mSurfaceView); 

     mHolder = mSurfaceView.getHolder(); 
     mHolder.addCallback(this); 
     mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    } 
    ... 
} 

MainActivity;

public class MainActivity extends Activity{ 
    RelativeLayout rlCamWrapper; 
    CameraSurfaceView cameraSurfaceView; 


    private SurfaceView surfaceView; 

    private boolean isRecording = false; 



    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Hide the window title. 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 


     cameraSurfaceView = new CameraSurfaceView(this); 

     setContentView(R.layout.main); 
    } 
    ... 
} 

XML:提前

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:id="@+id/relativeLayout1" 
    android:orientation="horizontal" 
    android:weightSum="1.0" 
    > 

    <VideoView 
     android:id="@+id/videoView1" 
     android:layout_alignParentTop="true" 
     android:layout_height="fill_parent" android:layout_width="fill_parent" 
     android:layout_weight=".5"/> 

    <RelativeLayout 
     android:id="@+id/surfaceViewWrapper" 
     android:layout_alignParentTop="true" 
     android:layout_height="fill_parent" android:layout_width="fill_parent" 
     android:layout_weight=".5"> 

     <SurfaceView 
      android:id="@+id/surfaceView2" 
      android:layout_alignParentTop="true" 
      android:layout_height="fill_parent" android:layout_width="fill_parent" 
      /> 

    </RelativeLayout> 
</LinearLayout> 

感谢您的帮助球员。

回答

2

只需在您的xml中更改<SurfaceView<your.package.CameraSurfaceView应该做的伎俩。然后,只需用findViewById()得到它在你的代码

public class MainActivity extends Activity{ 
    RelativeLayout rlCamWrapper; 
    CameraSurfaceView cameraSurfaceView; 


    private SurfaceView surfaceView; 

    private boolean isRecording = false; 



    /** Called when the activity is first created. */ 
    @Override 
    public void onCreate(Bundle savedInstanceState) { 
     super.onCreate(savedInstanceState); 

     // Hide the window title. 
     requestWindowFeature(Window.FEATURE_NO_TITLE); 
     getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 

     setContentView(R.layout.main); 

     // this is the important part 
     cameraSurfaceView = (CameraSurfaceView) findViewById(R.id.surfaceView2); 
    } 
    ... 
} 

还,请确保您超载其他构造你的看法(是这样):

public class CameraSurfaceView extends ViewGroup implements SurfaceHolder.Callback{ 
    private final String TAG = "Preview"; 

    private SurfaceView mSurfaceView; 
    private SurfaceHolder mHolder; 
    private Size mPreviewSize; 
    private List<Size> mSupportedPreviewSizes; 
    private Camera mCamera; 

    public CameraSurfaceView(Context context) { 
     super(context); 

     mSurfaceView = new SurfaceView(context); 
     addView(mSurfaceView); 

     mHolder = mSurfaceView.getHolder(); 
     mHolder.addCallback(this); 
     mHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS); 

    } 

    public CameraSurfaceView(Context context, AttributeSet attrs, int defStyle) { 
     super(context, attrs, defStyle); 
     ... 
    } 

    public CameraSurfaceView(Context context, AttributeSet attrs) { 
     super(context, attrs); 
     ... 
    } 
    ... 
} 
+0

你是什么意思@zrgiu? – jayellos 2012-03-13 03:26:21

+0

抱歉,格式化错误。请检查编辑的回复 – zrgiu 2012-03-13 03:30:57

+0

对不起@zrgiu,但我真的是新的android,我明白你的意思,但我不知道要编码。你能给我一些示例代码吗?这就是我在XML: jayellos 2012-03-13 03:37:04

0

无需创建实例

 cameraSurfaceView = new CameraSurfaceView(this); 

只需通过setContentView设置布局即可。同样在你的xml中设置

 <SurfaceView in your xml with <your.package.CameraSurfaceView 

而你完成了。