首页 文章

在Scrollview中滚动时,Android v2 MapFragment会抖动

提问于
浏览
2

我使用SupportMapFragment在ScrollView中显示静态 Map . 我不喜欢移动/缩放 Map ,只显示位置 .

当我向下/向上滚动时, Map 在其边界内摇晃,感觉非常迟钝 . 我的问题是,如何才能消除这种滞后,或者如何使用静态版本的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并不真正相关,即使我使用此技巧来删除旧设备上的黑色剪辑,正如您可以看到的透明视图 .

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

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

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

2 回答

  • 0

    随着新版Google Play服务的推出,Google添加了一个snapshot方法,以便检索当前显示的 Map 的位图,而该 Map 可能会显示 . 所以互动是不可能的,但在租用时, Map 不再动摇 .

    使用

    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();
            }
        }
    

    Sidenote: 要使用新版本,请运行Android SDK Manager的更新,并在使用maven时运行maven-android-sdk-deployer

    mvn install -fae
    
  • 1

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

    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;
        }
    }
    

相关问题