2016-08-28 4 views
1

私はAndroidで新しく、カメラアプリを構築しようとしています。 SurfaceViewを拡張し、カメラアクティビティでカメラをプレビューするためにSurfaceHolder.Callbackを実装するカメラプレビューを作成します。カメラのプレビューにボタンを追加するには

@Override 
protected void onCreate(Bundle icicle) { 
    super.onCreate(icicle); 
    setContentView(R.layout.activity_camera); 
    mPreview = new CameraPreview(this); 
    FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview); 
    preview.addView(mPreview); 
} 

、最終的にレイアウトXML::

<?xml version="1.0" encoding="utf-8"?> 
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android" 
android:orientation="vertical" 
android:layout_width="fill_parent" 
android:layout_height="fill_parent" 
> 
<FrameLayout 
    android:id="@+id/camera_preview" 
    android:layout_width="fill_parent" 
    android:layout_height="fill_parent" 
    android:layout_weight="1"> 


    <Button 
     android:layout_width="wrap_content" 
     android:layout_height="wrap_content" 
     android:text="New Button" 
     android:id="@+id/button" 
     android:layout_gravity="center_horizontal|bottom" /> 
</FrameLayout> 
</LinearLayout> 

AndroidMainifestレイアウトを初期化

public CameraPreview(Context context) { 
    super(context); 
    mContext = context; 
    mStartRequested = false; 
    mSurfaceAvailable = false; 
    mCamera = null; 
    mHolder = getHolder(); 
    mHolder.addCallback(this); 
} 

と(CameraActivity.java中)のonCreateメソッド: はここ請負業者です。 xml:

<application 
    ... 
    android:theme="@android:style/Theme.Holo.Light.NoActionBar.Fullscreen" > 

私はボタンに追加しようとしましたが、カメラのプレビューが表示されますが、アプリが昼食時にボタンが表示されません。 誰もが間違いを見ることができますか?

ありがとう!

答えて

3

プレビューはおそらくボタンの上に(Zオーダーで)表示されます。このライン:

preview.addView(mPreview); 

は、子ビューのでframeLayoutの内部リストの最後にCameraPreviewを追加します。 FrameLayoutは子ビューを順番にレンダリングします。プレビューがボタンの後に描かれているか、それを「上に」描いています。代わりにこれを試してみてください:

preview.addView(mPreview, 0); 

ます。また、このように、完全修飾クラス名でタグを使用することによって、それは代わりに、XMLから膨らませることによって、あなたのCameraPreviewを注文することができます。

<com.yourdomain.CameraPreview 
.../> 

...ビュー(コンテキストコンテキスト、AttributeSet attrs)をオーバーライドする必要があります。その作業を行うコンストラクタです。

+0

ありがとうございます!やってみます。 –

+0

ありがとう、それは動作します:) –

関連する問題