Android的V2 MapFragment滚动视图中滚动时晃动 [英] Android v2 MapFragment shaking when scrolling in Scrollview

查看:160
本文介绍了Android的V2 MapFragment滚动视图中滚动时晃动的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我现在用的是SupportMapFragment显示在滚动型静态地图。我不喜欢动/缩放地图,只表示在该位置。

当我向下滚动/向上地图上动摇其边界内,感觉pretty的laggy。我的问题是,如何IST可以消除这种滞后,或如何使用静态版V2 API映射。

这是我的布局:

 < RelativeLayout的的xmlns:机器人=htt​​p://schemas.android.com/apk/res/android
    机器人:layout_width =match_parent
    机器人:layout_height =match_parent
    机器人:layout_margin =@扪/ margin_half
    机器人:背景=@色/白
    机器人:方向=垂直>

  ...

    <片段
        机器人:ID =@ + ID / fragmentMap
        机器人:名称=com.google.android.gms.maps.SupportMapFragment
        机器人:layout_width =match_parent
        机器人:layout_height =300dp
        机器人:layout_alignParentLeft =真
        机器人:layout_below =@ ID / viewDivider
        机器人:layout_margin =@扪/ margin_half

         />

    <查看
        机器人:layout_width =match_parent
        机器人:layout_height =300dp
        机器人:layout_alignBottom =@ ID / fragmentMap
        机器人:layout_alignLeft =@ ID / fragmentMap
        机器人:layout_alignRight =@ ID / fragmentMap
        机器人:layout_alignTop =@ ID / fragmentMap
        机器人:背景=@机器人:彩色/透明/>

   ...

< / RelativeLayout的>
 

这是不是真的与 MapFragment在滚动型,即使我用这一招去除黑色剪裁的旧设备,你可以用透明的视图中看到。

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

 的GetMap()getUiSettings()setAllGesturesEnabled(假)。
的GetMap()getUiSettings()setZoomControlsEnabled(假)。
的GetMap()getUiSettings()setMyLocationButtonEnabled(假)。

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

解决方案

随着谷歌的新版本播放服务,谷歌增加了一个<一个href="https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/GoogleMap#snapshot%28com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback,%20android.graphics.Bitmap%29"相对=nofollow>为了检索所示currenrt映射,它代替可以示出的位图快照的方法。因此,互动是不可能的,但在租赁地图不抖了。

使用

  GoogleMap.snapshot(GoogleMap.SnapshotReadyCallback回调)
 

和实施SnapshotReadyCallback。一旦快照交付,包含快照的ImageView的更换MapFragment。

本例pretty的井在onResume方法:

  @覆盖
    公共无效onResume(){
        super.onResume();
        notifyDataChanged();
        最后ViewTreeObserver viewTreeObserver = fragmentMap.getView()
                .getViewTreeObserver();

        如果(viewTreeObserver.isAlive()){
            viewTreeObserver
                    .addOnGlobalLayoutListener(新OnGlobalLayoutListener(){

                        @覆盖
                        公共无效onGlobalLayout(){
                            onMesuredMap(本);
                        }
                    });
        }

    }

    私人无效onMesuredMap(OnGlobalLayoutListener监听器){
        如果(fragmentMap.getView()。的getWidth()!= 0
                &功放;&安培; fragmentMap.getView()。的getHeight()!= 0){

            fragmentMap.getView()。getViewTreeObserver()
                    .removeOnGlobalLayoutListener(听众);

            makeSnapShot();
        }
    }

        私人无效makeSnapShot(){
    可运行动作=新的Runnable(){
        @覆盖
        公共无效的run(){

            fragmentMap.getMap()。快照(新SnapshotReadyCallback(){

                @覆盖
                公共无效onSnapshotReady(位图将arg0){
                    imageViewStaticMap.setImageBitmap(为arg0);
                    removeMapFragment();
                }

            });
        }
    };
            //痘痘黑客以确保地图加载肯定
    。fragmentMap.getView()postDelayed(行动,1500);
}

    私人无效removeMapFragment(){
        如果(fragmentMap.isVisible()){
            getChildFragmentManager()
                    .beginTransaction()
                    卸下摆臂(fragmentMap)
                    。承诺();
        }
    }
 

旁注: 要使用新版本运行Android SDK管理器的更新,而当使用maven运行行家-Android-SDK-部署有:

  MVN安装-fae
 

I am using the SupportMapFragment to display a static map in a ScrollView. I do not like to move / zoom the Map, just showing where the location is.

When I am scrolling down/up the map shakes inside its bounds, it feels pretty laggy. My question is, how is ist possible to remove this lag, or how to use a static version of the v2 api map.

This is my layout:

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

This is not really related to MapFragment in ScrollView, even though i used this trick to remove the black clipping on older devices, as you can see with the transparent view.

I also disabled all gestures and the click-ability of the maps view:

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

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

解决方案

With the new release of the Google Play Services, Google added a snapshot method in order to retrieve a Bitmap of the currenrt shown map, which instead may be shown. So interaction is not possible but at leased the map is not shaking anymore.

use

GoogleMap.snapshot (GoogleMap.SnapshotReadyCallback callback)

and implement the SnapshotReadyCallback. Once the Snapshot is delivered, replace the MapFragment with an ImageView containing the Snapshot.

This example works pretty well in the onResume method:

@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: To use the new release run an Update of the Android SDK Manager, and when using maven run the maven-android-sdk-deployer with:

mvn install -fae

这篇关于Android的V2 MapFragment滚动视图中滚动时晃动的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆