Google地图过渡

时间:2018-06-26 08:58:52

标签: android google-maps

我有一个活动,其中有一个小面板,其中包含一个兴趣点的google地图。

现在,我希望当用户单击面板时,它可以展开为全屏地图(带有操作栏)以便能够对其进行操作。

我该如何实现?

2 个答案:

答案 0 :(得分:1)

对于Android 5.0(API级别21)及更高版本,您可以通过Shared Element Transition执行此操作。例如,像在this教程中一样,您可以使用通过两个活动共享的MapView(例如MainActivity-其中MapView位于小面板上,MapActivity- MapView进入全屏显示状态

  1. 启用窗口内容转换:将<item name="android:windowContentTransitions">true</item>添加到您的styles.xml文件中:

    <!-- Base application theme. -->
    <style name="AppTheme" parent="Theme.AppCompat.Light.DarkActionBar">
        <!-- Customize your theme here. -->
        <item name="colorPrimary">@color/colorPrimary</item>
        <item name="colorPrimaryDark">@color/colorPrimaryDark</item>
        <item name="colorAccent">@color/colorAccent</item>
    
        <item name="android:windowContentTransitions">true</item>
    </style>
    

  2. 分配通用的过渡名称,例如通过在"common_map"android:transitionName="common_map"的布局中将MapView参数添加到共享的MainActivityMapActivity

activity_main.xml

<?xml version="1.0" encoding="utf-8"?>
<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="<your_package_name>.MainActivity">

    <RelativeLayout
        android:id="@+id/panel"
        android:layout_centerVertical="true"
        android:layout_width="match_parent"
        android:layout_height="wrap_content">

        <com.google.android.gms.maps.MapView
            android:id="@+id/mapview"
            android:layout_width="100dp"
            android:layout_height="100dp"
            android:transitionName="common_map"
            />

        <TextView
            android:id="@+id/description_text"
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_toEndOf="@+id/mapview"
            android:layout_margin="16dp"
            android:text="Small panel with map"/>

    </RelativeLayout>
</RelativeLayout>

activity_map.xml

<?xml version="1.0" encoding="utf-8"?>
<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="<your_package_name>.MapActivity">

    <com.google.android.gms.maps.MapView
        android:id="@+id/mapview_fullscreen"
        android:layout_width="match_parent"
        android:layout_height="match_parent"
        android:transitionName="common_map"
        />

</RelativeLayout>
  1. 通过指定一束共享的MapActivity元素来启动MapView,例如在“小面板”(mPanelLayout)上单击:

    mPanelLayout = (RelativeLayout) findViewById(R.id.panel); mPanelLayout.setOnClickListener(new View.OnClickListener() { @Override public void onClick(View v) { Intent intent = new Intent(MainActivity.this, MapActivity.class); ActivityOptionsCompat options = ActivityOptionsCompat. makeSceneTransitionAnimation(MainActivity.this, (View) mMapView, "common_map"); startActivity(intent, options.toBundle()); } });

就是这样。

NB!您必须使用以下所有MapView方法进行转发:onCreate(Bundle), onStart(), onResume(), onPause(), onStop(), onDestroy(), onSaveInstanceState(), onLowMemory()或使用Lite Mode并仅转发onCreate()

完整源代码:

MainActivity.java

public class MainActivity extends AppCompatActivity {

    private static final String MAP_VIEW_BUNDLE_KEY = "MapViewBundleKey";
    static final LatLng KYIV = new LatLng(50.450311, 30.523730);

    private GoogleMap mGoogleMap;
    private MapView mMapView;
    private RelativeLayout mPanelLayout;

    @Override
    protected void onCreate(Bundle savedInstanceState) {

        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        Bundle mapViewBundle = null;
        if (savedInstanceState != null) {
            mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
        }

        mMapView = (MapView) findViewById(R.id.mapview);
        mMapView.onCreate(mapViewBundle);
        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mGoogleMap = googleMap;
                mGoogleMap.addMarker(new MarkerOptions()
                        .position(KYIV)
                        .title("Kyiv"));
                mGoogleMap.animateCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
                        .target(KYIV)
                        .zoom(12)
                        .build()));
            }
        });

        mPanelLayout = (RelativeLayout) findViewById(R.id.panel);
        mPanelLayout.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this, MapActivity.class);
                ActivityOptionsCompat options = ActivityOptionsCompat.
                        makeSceneTransitionAnimation(MainActivity.this, (View) mMapView, "common_map");
                startActivity(intent, options.toBundle());
            }
        });

    }

    @Override
    public void onSaveInstanceState(Bundle outState) {
        super.onSaveInstanceState(outState);

        Bundle mapViewBundle = outState.getBundle(MAP_VIEW_BUNDLE_KEY);
        if (mapViewBundle == null) {
            mapViewBundle = new Bundle();
            outState.putBundle(MAP_VIEW_BUNDLE_KEY, mapViewBundle);
        }

        mMapView.onSaveInstanceState(mapViewBundle);
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mMapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mMapView.onStop();
    }
    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }

}

MapActivity.java

public class MapActivity extends AppCompatActivity {

    private static final String MAP_VIEW_BUNDLE_KEY = "MapViewBundleKey1";
    static final LatLng KYIV = new LatLng(50.450311, 30.523730);

    private MapView mMapView;
    private GoogleMap mGoogleMap;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_map);

        Bundle mapViewBundle = null;
        if (savedInstanceState != null) {
            mapViewBundle = savedInstanceState.getBundle(MAP_VIEW_BUNDLE_KEY);
        }

        setTitle("Full screen");

        mMapView = (MapView) findViewById(R.id.mapview_fullscreen);
        mMapView.onCreate(mapViewBundle);
        mMapView.getMapAsync(new OnMapReadyCallback() {
            @Override
            public void onMapReady(GoogleMap googleMap) {
                mGoogleMap = googleMap;
                mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition.Builder()
                        .target(KYIV)
                        .zoom(12)
                        .build()));
                mGoogleMap.addMarker(new MarkerOptions()
                        .position(KYIV)
                        .title("Kyiv"));
            }
        });
    }

    @Override
    protected void onResume() {
        super.onResume();
        mMapView.onResume();
    }

    @Override
    protected void onStart() {
        super.onStart();
        mMapView.onStart();
    }

    @Override
    protected void onStop() {
        super.onStop();
        mMapView.onStop();
    }
    @Override
    protected void onPause() {
        mMapView.onPause();
        super.onPause();
    }
    @Override
    protected void onDestroy() {
        mMapView.onDestroy();
        super.onDestroy();
    }
    @Override
    public void onLowMemory() {
        super.onLowMemory();
        mMapView.onLowMemory();
    }
}

结果:

MapView transition

此外,您还可以自定义过渡动画(使其更加流畅等)

答案 1 :(得分:0)

您可以将gmap布局放在FrameLayout中,还可以向其中添加透明视图,将onClick设置为透明视图并处理全屏