[英]Android send Camera Preview from Activity to Fragment TextureView
[英]Android: camera preview through a fragment. Ok from activity
我正在嘗試使用片段顯示相機預覽,但是它不顯示預覽,而是“捕捉”按鈕旁邊的白色表面。
我沒有任何問題可以顯示活動的預覽。 這是我的代碼:
從活動中:
MainActivity.java
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
// Create an instance of Camera
mCamera = getCameraInstance();
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(this, mCamera);
FrameLayout preview = (FrameLayout) findViewById(R.id.camera_preview);
preview.addView(mPreview);
}
activity_main.xml中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
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:id="@+id/button_capture"
android:text="Capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
從片段中:
public class MainActivity extends AppCompatActivity
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
if (findViewById(R.id.fragment_container) != null) {
ButtonFragment buttonFragment = new ButtonFragment();
getSupportFragmentManager().beginTransaction().add(R.id.fragment_container, buttonFragment).commit();
}
fragment_button.xml
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:orientation="horizontal"
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:id="@+id/button_capture"
android:text="Capture"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center"
/>
</LinearLayout>
ButtonFragment.java
public class ButtonFragment extends Fragment {
//...
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create an instance of Camera
mCamera = getCameraInstance();
View rootView = inflater.inflate(R.layout.fragment_button, container, false);
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(container.getContext(), mCamera);
FrameLayout preview = (FrameLayout) rootView.findViewById(R.id.camera_preview);
preview.addView(mPreview);
// Inflate the layout for this fragment
return inflater.inflate(R.layout.fragment_button, container, false);
}
以下是調試時的值變量:
看來問題在於您要重新擴大onCreateView()
的布局,並返回該布局而不是附加了相機預覽的布局。
返回rootView
:
public class ButtonFragment extends Fragment {
@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
Bundle savedInstanceState) {
// Create an instance of Camera
mCamera = getCameraInstance();
View rootView = inflater.inflate(R.layout.fragment_button, container, false);
// Create our Preview view and set it as the content of our activity.
mPreview = new CameraPreview(container.getContext(), mCamera);
FrameLayout preview = (FrameLayout) rootView.findViewById(R.id.camera_preview);
preview.addView(mPreview);
// Inflate the layout for this fragment
//Don't inflate again and return:
//return inflater.inflate(R.layout.fragment_button, container, false);
//Instead, return the View with the camera preview:
return rootView;
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.