setContentView() slow with map fragment

2020-02-22 04:27发布

问题:

I am working on an app that shows a google map (api v2) as a fragment. When the app loads it shows a blank white screen for a couple of seconds before showing the map. I have used log statements see where the delays is but I don't know why it's so slow.

Here is my onCreate:

@Override
protected void onCreate(Bundle savedInstanceState) {
    Log.i(TAG, "onCreate Start -------------------------------");
    super.onCreate(savedInstanceState);
    Log.i(TAG, "onCreate 1 -------------------------------");
    setContentView(R.layout.activity_main);
    Log.i(TAG, "onCreate 2 -------------------------------");
    do_async_setup();
    Log.i(TAG, "onCreate 3 -------------------------------");
    prefs = getSharedPreferences(PREFS_NAME, 0);
    prefs_editor = prefs.edit();
    Log.i(TAG, "onCreate Finish -------------------------------"); 

And here is the output.

01-20 10:05:28.802: I/HeadsUp(19544): onCreate Start -------------------------------
01-20 10:05:28.802: I/HeadsUp(19544): onCreate 1 -------------------------------
01-20 10:05:30.396: I/HeadsUp(19544): onCreate 2 -------------------------------
01-20 10:05:30.396: I/HeadsUp(19544): onCreate 3 -------------------------------
01-20 10:05:30.403: I/HeadsUp(19544): onCreate Finish -------------------------------

You can see a 1.5 sec delay for setContentView. Here is my layout.

<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity" >

<fragment xmlns:android="http://schemas.android.com/apk/res/android"
        android:id="@+id/map"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        class="com.google.android.gms.maps.SupportMapFragment"/>

</RelativeLayout>

It's simply looks bad, like the app has frozen when loading. How can I either speed this up or hide the delay?

thanks,

回答1:

This is a drawback of using a GLSurfaceView such as the new maps v2 in Android. The delay is caused by the initialisation of the GLSurfaceView. See here http://code.google.com/p/gmaps-api-issues/issues/detail?id=4639 for a brief explanation near post #5 from a Googler and a snippet to help this problem (a little). Please star the issue to increase its priority.



回答2:

I've found that this can be pretty slow if you have a debugging session connected, but is usually a more acceptable speed when you don't. If it runs faster with the USB cable disconnected, that's probably why.