2010-11-12 86 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