gpt4 book ai didi

java - CWAC Camera - 实现自定义 gui

转载 作者:太空狗 更新时间:2023-10-29 12:45:19 26 4
gpt4 key购买 nike

我正在使用由 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/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com