2012-03-13 9 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に変更すると、<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

+0

申し訳ありませんが、フォーマットエラーです。編集された応答を確認してください – zrgiu

+0

申し訳ありません@zrgiuが、私は本当にアンドロイドに新しいです、私はあなたが意味するものを得るが、私はそれをコードすることを知らない。サンプルコードを教えていただけますか?これは私がXMLで持っているものです。 jayellos

0

インスタンス

を作成する必要はありません
 cameraSurfaceView = new CameraSurfaceView(this); 

レイアウトをsetContentViewで設定するだけです。また、あなたのXMLセットでは、

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

あなたが完了しました。

関連する問題