java - CWAC Camera - 实现自定义 gui

标签 java android android-camera commonsware-cwac

我正在使用由 commonsware 创建的库来制作一个简单的相机应用程序。在自述文件中有一节说明了这一点:

You can subclass CameraFragment and override onCreateView(). Chain to the superclass to get the CameraFragment's own UI, then wrap that in your own container with additional widgets, and return the combined UI from your onCreateView().

我仍然不太了解 Android Fragments,所以我希望有人能更好地向我解释这一点。

我有两个 Activity (A, B)、一个 fragment (CamFragment) 和两个布局 (A, B)。我的第一个 Activity (A) 加载具有单个按钮和 imageView 的布局 (A)。该按钮启动第二个 Activity (B),该 Activity 使用第二个布局 (B) 加载 fragment (CamFragment)。第二种布局取自演示应用:

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity"
    tools:ignore="MergeRootFrame"/>

所以,现在在我的应用程序中,我想要在相机 fragment/第二个 Activity 上有一个按钮,它将在点击时执行 takePicture()。按照自述文件中给出的说明,我需要先将 CameraFragment 子类化。这很简单,我就是这么做的:

public class CamFragment extends CameraFragment {

接下来,我必须重写 onCreateView。同样,就像在 v9 演示中一样,这是一个微不足道的任务。

  @Override
  public void onCreate(Bundle state) {
    super.onCreate(state);

    setHasOptionsMenu(true);
    setHost(new DemoCameraHost(getActivity()));
  }

下一部分是我比较迷茫的地方,希望有人能帮忙。

Chain to the superclass to get the CameraFragment's own UI, then wrap that in your own container with additional widgets, and return the combined UI from your onCreateView().

我只是不完全确定这意味着什么。如果有一个演示,它显示了一个没有 actionBar 的自定义相机 UI,那就太棒了,因为我觉得大多数开发人员不会将控件放入操作栏中。

编辑#1:

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup container,
        Bundle savedInstanceState) {
    // TODO Auto-generated method stub
    View uiFromParent=super.onCreateView(inflater, container, savedInstanceState);
    View yourUi = inflater.inflate(R.layout.main_cam, container, false);
    View theThingThatWillHoldThePreview = yourUi.findViewById(R.id.holder); // or whatever
    ((ViewGroup) theThingThatWillHoldThePreview).addView(uiFromParent); // with some appropriate LayoutParams
    return super.onCreateView(inflater, container, savedInstanceState);
}

主摄像头.xml

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/RelativeLayout1"
    android:layout_width="fill_parent"
    android:layout_height="fill_parent"
    android:orientation="vertical" >

    <FrameLayout
        android:id="@+id/holder"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:layout_alignParentBottom="true"
        android:layout_alignParentLeft="true"
        android:layout_alignParentTop="true"
        android:layout_weight="1"
        android:background="@color/abs__bright_foreground_holo_dark"
        android:keepScreenOn="true"
        tools:ignore="MergeRootFrame" >
    </FrameLayout>

    <ImageButton
        android:id="@+id/button1"
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:layout_alignParentBottom="true"
        android:layout_centerHorizontal="true"
        android:layout_marginBottom="10dp"
        android:background="@color/abs__bright_foreground_holo_dark"
        android:src="@drawable/ic_launcher"
        android:text="Capture" />

</RelativeLayout>

cam_fragment.xml

<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:id="@+id/container"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:ignore="MergeRootFrame"/>

最佳答案

Next, I have to override onCreateView. Again, a trivial task just like in the v9 demo.

您的代码没有覆盖 onCreateView()。它会覆盖 onCreate()

I'm just not entirely sure what that means/entails.

“链接到父类(super class)以获取 CameraFragment 自己的 UI”

View uiFromParent=super.onCreateView(inflater, container, savedInstanceState);

“然后用额外的小部件将其包装在您自己的容器中”

View yourUi=... // inflate something, create straight in Java, whatever
View theThingThatWillHoldThePreview=yourUi.findViewById(...); // or whatever

theThingThatWillHoldThePreview.addView(uiFromParent, ...); // with some appropriate LayoutParams

“并从您的 onCreateView() 返回组合的 UI”

return(yourUi);

关于java - CWAC Camera - 实现自定义 gui,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18924357/

相关文章:

Android - 拍摄照片

java - 我需要通过邮件从相机发送图像

java - 如何从 JarClassLoader 和 Applet Launcher 调用返回字符串数据的 applet 方法

java - 无法使用 Java 中的 Weka 访问 ML 分类的训练数据集

android - 使用错误的验证码进行身份验证时出现异常

Android 启动画面在真实设备上不显示

java - 在 Nexus 上拍摄照片时,自定义相机显示暗图片预览

java - JPanel 中的 Swing 计时器和动画

JAVA比较器抛出错误(多线程执行)

Java解析.txt文件