【问题标题】:Android v2 MapFragment shaking when scrolling in Scrollview在 Scrollview 中滚动时 Android v2 MapFragment 抖动
【发布时间】:2013-07-23 16:12:34
【问题描述】:

我正在使用 SupportMapFragment 在 ScrollView 中显示静态地图。我不喜欢移动/缩放地图,只显示位置。

当我向下/向上滚动时,地图会在其范围内晃动,感觉很迟钝。我的问题是,如何才能消除这种滞后,或者如何使用 v2 api 映射的静态版本。

这是我的布局:

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:layout_margin="@dimen/margin_half"
    android:background="@color/white"
    android:orientation="vertical" >

  ...

    <fragment
        android:id="@+id/fragmentMap"
        android:name="com.google.android.gms.maps.SupportMapFragment"
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignParentLeft="true"
        android:layout_below="@id/viewDivider"
        android:layout_margin="@dimen/margin_half"

         />

    <View
        android:layout_width="match_parent"
        android:layout_height="300dp"
        android:layout_alignBottom="@id/fragmentMap"
        android:layout_alignLeft="@id/fragmentMap"
        android:layout_alignRight="@id/fragmentMap"
        android:layout_alignTop="@id/fragmentMap"
        android:background="@android:color/transparent" />

   ...

</RelativeLayout>

这与MapFragment in ScrollView 并没有真正的关系,尽管我使用这个技巧去除了旧设备上的黑色剪辑,正如您在透明视图中看到的那样。

我还禁用了所有手势和地图视图的点击能力:

getMap().getUiSettings().setAllGesturesEnabled(false);
getMap().getUiSettings().setZoomControlsEnabled(false);
getMap().getUiSettings().setMyLocationButtonEnabled(false);

...
mapView.setClickable(false);
mapView.setFocusable(false);
mapView.setDuplicateParentStateEnabled(false);

【问题讨论】:

    标签: android scrollview android-maps-v2


    【解决方案1】:

    随着新版 Google Play 服务的发布,Google 添加了一个snapshot 方法来检索当前显示地图的位图,而该地图可能会被显示。所以互动是不可能的,但至少地图不再晃动了。

    使用

    GoogleMap.snapshot (GoogleMap.SnapshotReadyCallback callback)
    

    并实现 SnapshotReadyCallback。交付快照后,将 MapFragment 替换为包含快照的 ImageView。

    这个例子在 onResume 方法中效果很好:

    @Override
        public void onResume() {
            super.onResume();
            notifyDataChanged();
            final ViewTreeObserver viewTreeObserver = fragmentMap.getView()
                    .getViewTreeObserver();
    
            if (viewTreeObserver.isAlive()) {
                viewTreeObserver
                        .addOnGlobalLayoutListener(new OnGlobalLayoutListener() {
    
                            @Override
                            public void onGlobalLayout() {
                                onMesuredMap(this);
                            }
                        });
            }
    
        }
    
        private void onMesuredMap(OnGlobalLayoutListener listener) {
            if (fragmentMap.getView().getWidth() != 0
                    && fragmentMap.getView().getHeight() != 0) {
    
                fragmentMap.getView().getViewTreeObserver()
                        .removeOnGlobalLayoutListener(listener);
    
                makeSnapShot();
            }
        }
    
            private void makeSnapShot() {
        Runnable action = new Runnable() {
            @Override
            public void run() {
    
                fragmentMap.getMap().snapshot(new SnapshotReadyCallback() {
    
                    @Override
                    public void onSnapshotReady(Bitmap arg0) {
                        imageViewStaticMap.setImageBitmap(arg0);
                        removeMapFragment();
                    }
    
                });
            }
        };
                //litle hack to make sure that the map is loaded for sure
        fragmentMap.getView().postDelayed(action, 1500);
    }
    
        private void removeMapFragment() {
            if (fragmentMap.isVisible()) {
                getChildFragmentManager()
                        .beginTransaction()
                        .remove(fragmentMap)
                        .commit();
            }
        }
    

    旁注: 要使用新版本,请运行 Android SDK 管理器的更新,并在使用 maven 时运行 maven-android-sdk-deployer

    mvn install -fae
    

    【讨论】:

      【解决方案2】:

      尝试使用这个类,至少在我们的滚动视图中左右滚动时它应该看起来更好:

      public class TransparentSupportMapFragment extends SupportMapFragment {
          public TransparentSupportMapFragment() {
      
              super();
          }
      
          @Override
          public View onCreateView(LayoutInflater inflater, ViewGroup view, Bundle savedInstance) {
      
              View layout = super.onCreateView(inflater, view, savedInstance);
              FrameLayout frameLayout = new FrameLayout(getActivity());
              frameLayout.setBackgroundColor(getResources().getColor(android.R.color.transparent));
              ((ViewGroup) layout).addView(frameLayout, new ViewGroup.LayoutParams(LayoutParams.MATCH_PARENT, LayoutParams.MATCH_PARENT));
              return layout;
          }
      }
      

      【讨论】:

      • 遗憾的是它并没有解决垂直滚动滞后,但它是透明覆盖的更漂亮的解决方案。谢谢。
      • 关于您的问题,我暂时没有找到解决方案,我一直在活动中全屏使用谷歌地图
      • 这是个坏消息。也许我会再次使用 API v1。
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-07-29
      • 2014-10-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多