2010-11-12 81 views
3

我有一個應用程序,其中包含一個MapviewActivity活動,並且主要是一個mapview。 但是我注意到,活動的啓動時間非常慢,並且導致從按鈕進入地圖活動的那一刻起的滯後。我覺得這會造成糟糕的用戶體驗,並希望避免這種情況。 我已經將Google地圖活動的背景設置爲@null,正如Google使用Google開發者頁面上的其中一項UI改進文章所建議的那樣,我覺得這樣做並不能解決問題。加速啓動包含mapview的活動並擴展MapviewActivity

有沒有辦法改善這一點?我不希望主屏幕卡住活動的啓動,甚至轉移到地圖活動,然後加載mapview會更好。

感謝, 傑森

回答

8

你可以嘗試從佈局移除MapView。然後,在onCreate()中,使用post()安排Runnable以添加MapView,並從Java執行與映射相關的初始化。通過使用post()(或潛在地,postDelayed())將MapView進一步推遲,您應該能夠首先渲染剩餘的活動。

我沒有試過這個,所以YMMV。 :-)

+0

謝謝,並獲得成功,並感謝您的幫助您總是提供有用的答案 – Jason 2010-11-12 13:59:25

+0

@Jason:嘿,很高興它的工作。出於好奇,是'post()'就夠了,還是用'postDelayed()'結束了? – CommonsWare 2010-11-12 14:30:21

+0

這對我有幫助,但我需要使用postDelayed。 – tomwilson 2011-04-11 00:57:37

1

我試過CommonsWare的答案,但是在沒有MapView的情況下,即使是裸露的MapActivity也會出現明顯的延遲。

所以,我把另一個活動放在中間,「MapLoading」。我的家庭活動啓動MapLoading,然後使用post()立即啓動MapActivity。由於滯後,應用程序在MapLoading上停留了幾秒鐘(這是預期的結果,而不是堅持我的家庭活動)。

MapLoading活動沒有歷史記錄集,所以當從MapActivity單擊後退按鈕時,它會直接進入主屏幕。 MapActivity的未來發布速度非常快,MapLoading活動僅在屏幕上閃爍。

這裏是MapLoading代碼(用的MapView在它被稱爲地圖的活動):

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

這一直在竊聽我一段時間,我通過擴展o n CommonsWare的回答:

我目前使用這個解決方案,大大加快了我的活動加載時間,使一切看起來一樣順利,因爲它得到。

我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正在進行設置和下載,而不是隻顯示空的視圖,而是顯示地圖的模糊圖像。

谷歌標誌被放置在ImageView的頂部比較早,所以它看起來只是一個128×128像素的圖片非常有說服力:

ImageView below GoogleMap in an Activity as a mock loading screen

+0

與圖像很好的伎倆。 – Jason 2015-04-09 13:59:11