我正在尝试将 Google Maps Android API v2 中的 MapFragment 与相机预览结合使用。我需要能够在相机预览和 MapFragment 之间切换,但我做不到。
对于相机预览,我从 example guide 复制了 CameraPreview 类.当我想查看相机预览时,我使用以下方法将 CameraPreview 类的实例添加到我的 Activity 中
CameraPreview mPreview = new CameraPreview(this);
addContentView(mPreview, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
当我不使用 MapFragment 时,这工作正常。
对于 MapFragment,我已通过以下方式将其添加到我的 Activity 布局中
<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 实例添加到我的 Activity 中。隐藏不起作用,而且 MapFragment 似乎以某种方式劫持了 CameraPreview 并取得了优先权。一个奇怪的功能是,如果我强制屏幕休眠然后唤醒它,当它唤醒时,CameraPreview 就在那里。如果我反过来做,即先添加 CameraPreview 然后隐藏 MapFragment,行为是相同的。
仅供引用:我正在运行 Android 版本 4.1.1 的三星 Galaxy Note 2 LTE 上测试该应用。
谁能告诉我我做错了什么?
最佳答案
在您的相机预览中使用 setZOrderOnTop(boolean),就像这样 mPreview.setZOrderOnTop(true);。 这个对我有用。
关于android - MapFragment 和 Camera 似乎在同一个 Activity 中相互干扰,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15370624/