2013-07-24 48 views
2

我正在使用SupportMapFragment在ScrollView中顯示靜態地圖。我不喜歡移動/縮放地圖,只是顯示位置在哪裏。Android v2 MapFragment在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); 

回答

1

隨着新版Google Play服務的發佈,Google添加了一個snapshot方法,以便檢索currenrt顯示的地圖的位圖,而該圖可能會顯示。所以互動是不可能的,但在租賃地圖不再動搖。

使用

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 
0

嘗試使用這個類,它應該更好地滾動權利,在我們的滾動視圖離開時至少:

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

不幸的是,它不能解決垂直滾動滯後問題,但它是透明覆蓋層更美觀的解決方案。謝謝。 – joecks

+0

關於您的問題我目前沒有看到解決方案,我一直使用谷歌地圖全屏在活動 –

+0

這是個壞消息。也許我會再次使用API​​ v1。 – joecks