我正在尝试使用片段显示相机预览,但它不显示预览,而是“捕获”按钮旁边的白色表情。
显示活动预览没有任何问题。这是我的代码:
来自活动:
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);
}
以下是调试时的值变量:
答案 0 :(得分:2)
看起来问题是您在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;
}
}