Java CWAC摄像头-实现自定义gui
我正在使用Commonware创建的库来制作一个简单的相机应用程序。在自述中,有一节说明: 您可以将CameraFragment子类化并重写onCreateView()。连锁 获取CameraFragment自己的UI的超类,然后将其封装在 您自己的容器和其他小部件,并返回组合的UI 从您的onCreateView() 我对Android碎片还不太了解,所以我希望有人能更好地向我解释这一点 我有两个活动(A,B)、一个片段(CamFragment)和两个布局(A,B)。我的第一个活动(A)加载一个布局(A),其中有一个按钮和imageView。该按钮启动第二个活动(B),该活动用第二个布局(B)加载片段(CamFragment)。第二个布局取自演示应用程序:Java CWAC摄像头-实现自定义gui,java,android,android-camera,commonsware-cwac,Java,Android,Android Camera,Commonsware Cwac,我正在使用Commonware创建的库来制作一个简单的相机应用程序。在自述中,有一节说明: 您可以将CameraFragment子类化并重写onCreateView()。连锁 获取CameraFragment自己的UI的超类,然后将其封装在 您自己的容器和其他小部件,并返回组合的UI 从您的onCreateView() 我对Android碎片还不太了解,所以我希望有人能更好地向我解释这一点 我有两个活动(A,B)、一个片段(CamFragment)和两个布局(A,B)。我的第一个活动(A)加载一
<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"/>
接下来,我必须重写onCreateView。同样,这是一项与v9演示类似的琐碎任务
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setHasOptionsMenu(true);
setHost(new DemoCameraHost(getActivity()));
}
下一部分是我真正感到困惑的地方,我希望有人能帮助我
链接到超类以获得CameraFragment自己的UI,然后包装
在您自己的容器中添加其他小部件,并返回
来自onCreateView()的组合UI
我只是不完全确定那意味着什么。如果有一个演示,显示一个没有actionBar的定制相机UI,那将是非常棒的,因为我觉得大多数开发人员不会将控件放入actionBar
编辑#1:
main_cam.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"/>
接下来,我必须重写onCreateView。同样,这是一项与v9演示类似的琐碎任务
@Override
public void onCreate(Bundle state) {
super.onCreate(state);
setHasOptionsMenu(true);
setHost(new DemoCameraHost(getActivity()));
}
您的代码没有覆盖onCreateView()
。它覆盖onCreate()
我只是不完全确定那意味着什么
“链接到超类以获取CameraFragment自己的UI”
“然后用其他小部件将其包装在您自己的容器中”
“并从onCreateView()返回组合UI”
很抱歉,对于第一部分
onCreateView
,我是将其插入片段还是活动?@GuyLeStack:“我是将其插入片段还是活动?”--onCreateView()
包含在您的CameraFragment
子类中。好的,对于解决方案的底部部分,我有点困惑。我不太熟悉布局和放大布局之类的东西(除了setContentView)。uiFromParent和yourUi不应该相同吗。对不起,我真的很感激你的回答,但我还是“不明白”。在我的脑海里。。。我应该抓取UI,并以某种方式添加一个按钮。是的,在你的伪代码中似乎有3个视图对象是根布局/视图。@GuyLeStack:“uiFromParent和你的UI不应该相同吗”--不。uiFromParent
是我为UI创建的(aCameraView
)yourUi
是您正在创建(无论从何处)并将我的UI放入其中的UI。@GuyLeStack:当然,请记住,您不必执行任何操作。将我的片段与您的UI的其余部分放在一起。子类化方法是,如果您绝对需要将CameraView
放在CameraFragment
无法到达的地方。或者,引用我的文档,“您可以将该UI作为CameraFragment的对等对象,例如,通过像演示应用程序那样使用操作栏项”。您可以有两个片段(我的和您的),或者三个片段(我的和您的两个),或者我的片段和您的一批其他小部件,等等。
<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"/>
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
return(yourUi);