Android:通过片段进行相机预览。从活动中确定
Posted
技术标签:
【中文标题】Android:通过片段进行相机预览。从活动中确定【英文标题】:Android: camera preview through a fragment. Ok from activity 【发布时间】:2016-04-01 18:01:35 【问题描述】:我正在尝试使用片段显示相机预览,但它不显示预览,而是显示“捕获”按钮旁边的白色表面。
显示活动的预览没有任何问题。这是我的代码:
来自活动:
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_
android:layout_
>
<FrameLayout
android:id="@+id/camera_preview"
android:layout_
android:layout_
android:layout_weight="1"
/>
<Button
android:id="@+id/button_capture"
android:text="Capture"
android:layout_
android:layout_
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_
android:layout_
>
<FrameLayout
android:id="@+id/camera_preview"
android:layout_
android:layout_
android:layout_weight="1"
/>
<Button
android:id="@+id/button_capture"
android:text="Capture"
android:layout_
android:layout_
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);
这里是调试时的值变量:
【问题讨论】:
【参考方案1】:问题似乎在于您正在重新扩充 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;
【讨论】:
以上是关于Android:通过片段进行相机预览。从活动中确定的主要内容,如果未能解决你的问题,请参考以下文章
Android:是不是可以在启动它的同一活动中使用从相机拍摄的照片?