Speed ​​up the start of an activity containing a map and expand MapviewActivity

I have an application that contains an activity which is MapviewActivity and basically is mapview. However, I noticed that the start time of the activity is very slow and causes a delay from the moment the button is pressed to enter the activity of the card. I feel this creates a bad user interface and would like to avoid this. I already set the map activity background to @null, as suggested in one of the articles on improving the user interface on the googles developer page, which, in my opinion, does not do the trick.

Is there any way to improve this? I would not want the main main screen to get stuck when starting this activity, even transferring to the activity of the map, and then loading the map would be better.

Thanks Jason

+3
source share
3 answers

You can try to remove MapViewfrom your layout. Then onCreate()use post()in to schedule Runnableto add to MapViewand perform the initialization associated with the map with Java. Using post()(or, potentially postDelayed()) to set aside MapViewfurther, you must first display the rest of the activity.

I have not tried this, so YMMV. :-)

+8
source

CommonsWare, , MapActivity MapView.

, , "MapLoading". MapLoading, post() MapActivity. - MapLoading ( , , ).

MapLoading setHistory, , back MapActivity, . MapActivity , MapLoading .

MapLoading ( 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" />
+1

, , CommonsWare:

, , .

My MapActivity:

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

/activity_map.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 , .

Google ImageView , 128x128 :

ImageView below Google Map in an Activity as a mock loading screen

+1

Source: https://habr.com/ru/post/1774480/


All Articles