我正在尝试将 Google Maps Android API v2 中的 MapFragment 与相机预览结合使用。我需要能够在相机预览和 MapFragment 之间切换,但我无法使其工作。
对于相机预览,我从以下位置复制了 CameraPreview 类示例指南 http://developer.android.com/guide/topics/media/camera.html。当我想查看相机预览时,我使用以下命令将 CameraPreview 类的实例添加到我的活动中
CameraPreview mPreview = new CameraPreview(this);
addContentView(mPreview, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
当我不使用 MapFragment 时,这工作得很好。
对于 MapFragment,我已通过以下方式将其添加到我的活动布局中
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/activity_main"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical"
tools:context=".MainActivity" >
<fragment
android:id="@+id/map"
android:layout_width="fill_parent"
android:layout_height="match_parent"
class="com.google.android.gms.maps.MapFragment" />
</LinearLayout>
没有 CameraPreview 也能正常工作。我可以使用以下方法隐藏和取消隐藏 MapFragment(例如隐藏):
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.hide(map_fragment);
ft.commit();
但是,当我尝试同时使用两者时,问题就出现了,即隐藏 MapFragment,然后将 CameraPreview 实例添加到我的活动中。隐藏不起作用,并且 MapFragment 似乎以某种方式劫持了 CameraPreview 并取得了优先权。一个奇怪的功能是,如果我强制屏幕休眠然后唤醒它,当它唤醒时,CameraPreview 就在那里。如果我以相反的方式执行此操作,即先添加 CameraPreview,然后隐藏 MapFragment,则行为是相同的。
仅供参考:我正在运行 Android 版本 4.1.1 的 Samsung Galaxy Note 2 LTE 上测试该应用程序。
谁能告诉我我做错了什么?