MapFragment和照相机似乎在同一活动相互干涉 [英] MapFragment and Camera seem to interfere with each other in the same Activity
问题描述
我试图使用从谷歌地图的Android API v2的一个MapFragment连同一个摄像头preVIEW。我需要能够在相机preVIEW和MapFragment之间切换,但我不能使它工作。
I'm trying to use a MapFragment from the Google Maps Android API v2 in conjunction with a Camera preview. I need to be able to switch between the camera preview and the MapFragment, but I can’t make it work.
有关相机preVIEW,我已经从的例如指南。当我想看看相机preVIEW,我使用添加摄像头preVIEW类的实例,以我的活动
For the Camera preview, I’ve copied the CameraPreview class from the example guide. When I want to see the Camera preview, I add an instance of the CameraPreview class to my activity using
CameraPreview mPreview = new CameraPreview(this);
addContentView(mPreview, new LayoutParams(LayoutParams.MATCH_PARENT,LayoutParams.MATCH_PARENT));
这时候我不使用时MapFragment正常工作。
This works fine when when I’m not using the MapFragment.
对于MapFragment,我通过
For the MapFragment, I’ve added it into my activity’s layout via
<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>
它工作正常,没有摄像头preVIEW。我可以隐藏和使用(例如,用于隐藏)取消隐藏MapFragment:
It works fine without the CameraPreview. I can hide and unhide the MapFragment using (e.g. for hide):
FragmentTransaction ft = getFragmentManager().beginTransaction();
ft.hide(map_fragment);
ft.commit();
然而,问题是当我尝试使用两者结合起来,即隐藏MapFragment,然后添加一个摄像头preVIEW比如到我的活动。隐藏不起作用,而且似乎MapFragment莫名其妙劫持相机preVIEW并采取precedence。一个奇怪的特性是,如果我强迫屏幕睡觉,然后唤醒它,当它醒来相机preVIEW是存在的。如果我这样做,反过来,即先添加摄像头preVIEW然后隐藏MapFragment,行为是一样的。
However, the problem comes when I try and use the two together, i.e. hide the MapFragment and then add a CameraPreview instance into my activity. The hide doesn’t work, and it seems that the MapFragment somehow hijacks the CameraPreview and takes precedence. One strange feature is that if I force the screen to sleep and then wake it up, when it wakes up the CameraPreview is there. If I do it the other way round, i.e. add the CameraPreview first and then hide the MapFragment, the behavior is the same.
FYI:我上运行Android 4.1.1版本三星的Galaxy Note 2 LTE测试应用
FYI: I'm testing the app on a Samsung Galaxy Note 2 LTE running Android Version 4.1.1.
谁能告诉我什么,我做错了吗?
Can anyone tell me what I’m doing wrong?
推荐答案
使用setZOrderOnTop(布尔)在您的相机preVIEW像该M preview.setZOrderOnTop(真);.
这对我的作品。
Use setZOrderOnTop(boolean) on your camerapreview like this mPreview.setZOrderOnTop(true);. It works for me.
这篇关于MapFragment和照相机似乎在同一活动相互干涉的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!