代码之家  ›  专栏  ›  技术社区  ›  Jason

加快启动包含mapview和扩展MapviewActivity的活动

  •  3
  • Jason  · 技术社区  · 15 年前

    我有一个应用程序,其中包含一个活动,这是一个MapviewActivity,主要是一个mapview。 不过,我注意到活动的启动时间非常慢,从按下按钮进入“地图”活动的那一刻起就会造成一个延迟。我觉得这会造成不好的用户体验,希望避免这种情况。 我已经将地图活动的背景设置为 @null 正如google开发者页面上的一篇UI改进文章所建议的那样,我觉得这并没有起到作用。

    有什么方法可以改进吗?我不希望主主屏幕被困在活动的启动上,即使转移到地图活动,然后加载地图视图也会更好。

    谢谢, 杰森

    3 回复  |  直到 15 年前
        1
  •  8
  •   CommonsWare    15 年前

    您可以尝试删除 MapView 从你的布局。那么,在 onCreate() ,使用 post() 安排 Runnable 添加 地图视图 并从Java执行与地图相关的初始化。通过使用 发布() (或者,潜在的, postDelayed() )推迟 地图视图 此外,您应该能够首先呈现活动的其余部分。

    我没试过,所以YMMV。:-)

        2
  •  2
  •   easytarget    11 年前

    这件事困扰了我一段时间,我通过扩展 公用软件 回答:

    我目前使用的这个解决方案大大加快了我的活动加载时间,并使一切看起来顺利。

    我的地图活动:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map); // See activity_map.xml below.
    
        // Start a runnable that does the entire Map setup.
        // A delay does not seem to be necessary.
        Handler mapSetupHandler = new Handler();
        Runnable mapSetupRunnable = new Runnable() {
            public void run() {
                FragmentManager fragMan = getSupportFragmentManager();
                FragmentTransaction fragTransaction = fragMan.beginTransaction();
    
                final SupportMapFragment mapFragment = new SupportMapFragment();
                fragTransaction.add(R.id.container_map, mapFragment, "mapFragment");
                fragTransaction.commit();
    
                // At the end, retrieve the GoogleMap object and the View for further setup,
                // zoom in on a region, add markers etc.
                mapFragment.getMapAsync(new OnMapReadyCallback() {
                    @Override
                    public void onMapReady(GoogleMap googleMap) {
                        mMap = googleMap;
                        mMapView = mapFragment.getView();
                        setUpMap();
                    }
                });
            }
        };
        mapSetupHandler.post(mapSetupRunnable);
    }
    

    布局/活动图.xml:

    <RelativeLayout    
    xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
    
    <!--This FrameLayout will be used by the GoogleMap-->
    <!--as a container for the SupportMapFragment.-->
    <FrameLayout
        android:id="@+id/container_map"
        android:layout_width="match_parent"
        android:layout_height="fill_parent"
        android:layout_above="@id/bar_room_password"
        android:layout_below="@id/toolbar">
    
        <!--Initially the container is filled with a placeholder image.-->
        <!--A small, heavily blurred image of a map screenshot is used-->
        <!--in order to fake a loading map.-->
        <ImageView
            android:id="@+id/image_map_placeholder"
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:alpha="50"
            android:contentDescription="@string/map"
            android:scaleType="centerCrop"
            android:src="@drawable/placeholder_map" />
    </FrameLayout>
    
    [... more, unrelated Views...]
    </RelativeLayout>
    

    由于安装程序是在Runnable内部处理的,因此该活动显示得很快。另外,在GoogleMap进行设置和下载时,我不只是显示一个空视图,而是显示一个模糊的假地图图像。

    谷歌标志被放置在ImageView的顶部相对较早,因此它看起来非常有说服力,只有一个128x128像素的图像:

    ImageView below GoogleMap in an Activity as a mock loading screen

        3
  •  1
  •   durka42    14 年前

    我尝试了commonware的答案,但是在没有mappview的情况下,即使是一个裸mappactivity的启动也有明显的延迟。

    所以,我把另一个活动放在中间,“地图加载”。“我的主页”活动启动MapLoading,然后使用post()立即启动MapActivity。由于延迟,应用程序会在地图加载上坚持几秒钟(这是预期的结果,而不是坚持我的家庭活动)。

    MapLoading活动没有设置历史记录,因此当从MapActivity单击“上一步”按钮时,它将直接进入主屏幕。未来的地图活动启动非常快,地图加载活动只是在屏幕上闪烁。

    以下是用于映射加载的代码(其中包含MapView的活动称为Map):

    public class MapLoading extends Activity {
    
        @Override
        public void onCreate(Bundle savedInstanceState) {
            super.onCreate(savedInstanceState);
            setContentView(R.layout.maploading);
    
            new Handler().post(new Runnable() {
                public void run()
                {
                    startActivity(new Intent(MapLoading.this, Map.class));
                }
            });
        }
    }
    

    这是它的布局:

    <?xml version="1.0" encoding="utf-8"?>
    <RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
        android:layout_width="match_parent"
        android:layout_height="match_parent" >
    
        <TextView
            android:id="@+id/maploading"
            android:layout_width="fill_parent"
            android:layout_height="fill_parent"
            android:gravity="center"
            android:textSize="14pt"
            android:text="Loading map..."
            />
    
    </RelativeLayout>
    

    以及AndroidManifest.xml中的相应节:

    <activity android:label="@string/app_name"
              android:name="MapLoading"
              android:noHistory="true" />