Skip to content

Commit 7d2a50b

Browse files
authored
feat: Adding SnapshotDemoActivity. (#433)
1 parent 6a51655 commit 7d2a50b

File tree

6 files changed

+225
-0
lines changed

6 files changed

+225
-0
lines changed

ApiDemos/kotlin/app/src/gms/AndroidManifest.xml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -53,6 +53,7 @@
5353
<activity android:name=".RawMapViewDemoActivity" />
5454
<activity android:name=".RetainMapDemoActivity" />
5555
<activity android:name=".SaveStateDemoActivity" />
56+
<activity android:name=".SnapshotDemoActivity" />
5657
<activity android:name=".SplitStreetViewPanoramaAndMapDemoActivity" />
5758
<activity android:name=".StreetViewPanoramaBasicDemoActivity" />
5859
<activity android:name=".StreetViewPanoramaEventsDemoActivity" />

ApiDemos/kotlin/app/src/gms/java/com/example/kotlindemos/DemoDetailsList.kt

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -89,6 +89,9 @@ class DemoDetailsList {
8989
DemoDetails(R.string.save_state_demo_label,
9090
R.string.save_state_demo_description,
9191
SaveStateDemoActivity::class.java),
92+
DemoDetails(R.string.snapshot_demo_label,
93+
R.string.snapshot_demo_description,
94+
SnapshotDemoActivity::class.java),
9295
DemoDetails(R.string.street_view_panorama_basic_demo_label,
9396
R.string.street_view_panorama_basic_demo_details,
9497
StreetViewPanoramaBasicDemoActivity::class.java),
Lines changed: 78 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,78 @@
1+
// Copyright 2020 Google LLC
2+
//
3+
// Licensed under the Apache License, Version 2.0 (the "License");
4+
// you may not use this file except in compliance with the License.
5+
// You may obtain a copy of the License at
6+
//
7+
// http://www.apache.org/licenses/LICENSE-2.0
8+
//
9+
// Unless required by applicable law or agreed to in writing, software
10+
// distributed under the License is distributed on an "AS IS" BASIS,
11+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12+
// See the License for the specific language governing permissions and
13+
// limitations under the License.
14+
package com.example.kotlindemos
15+
16+
import android.os.Bundle
17+
import android.view.View
18+
import android.widget.Button
19+
import android.widget.CheckBox
20+
import android.widget.ImageView
21+
import androidx.appcompat.app.AppCompatActivity
22+
import androidx.lifecycle.lifecycleScope
23+
import com.google.android.gms.maps.GoogleMap
24+
import com.google.android.gms.maps.GoogleMap.SnapshotReadyCallback
25+
import com.google.android.gms.maps.SupportMapFragment
26+
import com.google.maps.android.ktx.awaitMap
27+
28+
/**
29+
* This shows how to take a snapshot of the map.
30+
*/
31+
class SnapshotDemoActivity : AppCompatActivity() {
32+
/**
33+
* Note that this may be null if the Google Play services APK is not available.
34+
*/
35+
private lateinit var map: GoogleMap
36+
private lateinit var waitForMapLoadCheckBox: CheckBox
37+
private lateinit var snapshotHolder: ImageView
38+
39+
override fun onCreate(savedInstanceState: Bundle?) {
40+
super.onCreate(savedInstanceState)
41+
setContentView(R.layout.snapshot_demo)
42+
waitForMapLoadCheckBox = findViewById(R.id.wait_for_map_load)
43+
snapshotHolder = findViewById(R.id.snapshot_holder)
44+
val mapFragment = supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment
45+
lifecycleScope.launchWhenCreated {
46+
map = mapFragment.awaitMap()
47+
attachButtonListeners()
48+
}
49+
}
50+
51+
private fun attachButtonListeners() {
52+
findViewById<Button>(R.id.button_take_snapshot).setOnClickListener {
53+
takeSnapshot()
54+
}
55+
findViewById<Button>(R.id.button_clear_snapshot).setOnClickListener {
56+
clearSnapshot()
57+
}
58+
}
59+
60+
private fun takeSnapshot() {
61+
val callback =
62+
SnapshotReadyCallback { snapshot -> // Callback is called from the main thread, so we can modify the ImageView safely.
63+
snapshotHolder.setImageBitmap(snapshot)
64+
}
65+
if (waitForMapLoadCheckBox.isChecked) {
66+
map.setOnMapLoadedCallback { map.snapshot(callback) }
67+
} else {
68+
map.snapshot(callback)
69+
}
70+
}
71+
72+
/**
73+
* Called when the clear button is clicked.
74+
*/
75+
private fun clearSnapshot() {
76+
snapshotHolder.setImageDrawable(null)
77+
}
78+
}
Lines changed: 68 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,68 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright 2020 Google LLC
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
18+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
19+
android:layout_width="match_parent"
20+
android:layout_height="match_parent"
21+
android:orientation="vertical">
22+
23+
<LinearLayout
24+
android:layout_width="match_parent"
25+
android:layout_height="0dp"
26+
android:layout_weight="1"
27+
android:orientation="horizontal">
28+
29+
<fragment
30+
android:id="@+id/map"
31+
android:layout_width="0dp"
32+
android:layout_weight="1"
33+
android:layout_height="match_parent"
34+
class="com.google.android.gms.maps.SupportMapFragment" />
35+
36+
<ImageView
37+
android:id="@+id/snapshot_holder"
38+
android:layout_width="0dp"
39+
android:layout_weight="1"
40+
android:layout_height="match_parent"
41+
android:contentDescription="@string/snapshot_holder_description" />
42+
</LinearLayout>
43+
44+
<CheckBox
45+
android:id="@+id/wait_for_map_load"
46+
android:layout_width="match_parent"
47+
android:layout_height="wrap_content"
48+
android:checked="true"
49+
android:text="@string/wait_for_map_load" />
50+
51+
<LinearLayout
52+
android:layout_width="wrap_content"
53+
android:layout_height="wrap_content"
54+
android:orientation="horizontal">
55+
56+
<Button
57+
android:id="@+id/button_take_snapshot"
58+
android:layout_width="wrap_content"
59+
android:layout_height="wrap_content"
60+
android:text="@string/snapshot_take_button" />
61+
62+
<Button
63+
android:id="@+id/button_clear_snapshot"
64+
android:layout_width="wrap_content"
65+
android:layout_height="wrap_content"
66+
android:text="@string/snapshot_clear_button" />
67+
</LinearLayout>
68+
</LinearLayout>
Lines changed: 67 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
<?xml version="1.0" encoding="utf-8"?>
2+
<!--
3+
Copyright (C) 2012 The Android Open Source Project
4+
5+
Licensed under the Apache License, Version 2.0 (the "License");
6+
you may not use this file except in compliance with the License.
7+
You may obtain a copy of the License at
8+
9+
http://www.apache.org/licenses/LICENSE-2.0
10+
11+
Unless required by applicable law or agreed to in writing, software
12+
distributed under the License is distributed on an "AS IS" BASIS,
13+
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14+
See the License for the specific language governing permissions and
15+
limitations under the License.
16+
-->
17+
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
18+
android:layout_width="match_parent"
19+
android:layout_height="match_parent"
20+
android:orientation="vertical">
21+
22+
<LinearLayout
23+
android:layout_width="match_parent"
24+
android:layout_height="0dp"
25+
android:layout_weight="1"
26+
android:orientation="vertical">
27+
28+
<fragment
29+
android:id="@+id/map"
30+
android:layout_width="match_parent"
31+
android:layout_weight="1"
32+
android:layout_height="0dp"
33+
class="com.google.android.gms.maps.SupportMapFragment" />
34+
35+
<ImageView
36+
android:id="@+id/snapshot_holder"
37+
android:layout_width="match_parent"
38+
android:layout_weight="1"
39+
android:layout_height="0dp"
40+
android:contentDescription="@string/snapshot_holder_description" />
41+
</LinearLayout>
42+
43+
<CheckBox
44+
android:id="@+id/wait_for_map_load"
45+
android:layout_width="match_parent"
46+
android:layout_height="wrap_content"
47+
android:checked="true"
48+
android:text="@string/wait_for_map_load" />
49+
50+
<LinearLayout
51+
android:layout_width="wrap_content"
52+
android:layout_height="wrap_content"
53+
android:orientation="horizontal">
54+
55+
<Button
56+
android:id="@+id/button_take_snapshot"
57+
android:layout_width="wrap_content"
58+
android:layout_height="wrap_content"
59+
android:text="@string/snapshot_take_button" />
60+
61+
<Button
62+
android:id="@+id/button_clear_snapshot"
63+
android:layout_width="wrap_content"
64+
android:layout_height="wrap_content"
65+
android:text="@string/snapshot_clear_button" />
66+
</LinearLayout>
67+
</LinearLayout>

ApiDemos/kotlin/app/src/main/res/values/strings.xml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -315,4 +315,12 @@
315315
<string name="save_state_demo_description">Demonstrates how to save the state of a MapFragment upon rotation of the device.</string>
316316
<string name="save_state_instructions">Drag the marker, tap on it to change its color and rotate the device to check that the state of the map is preserved.</string>
317317

318+
<!-- Snapshot -->
319+
<string name="snapshot_clear_button">Clear</string>
320+
<string name="snapshot_demo_label">Snapshot</string>
321+
<string name="snapshot_demo_description">Demonstrates how to take a snapshot of the map.</string>
322+
<string name="snapshot_holder_description">Area for the map snapshot.</string>
323+
<string name="snapshot_take_button">Take snapshot</string>
324+
<string name="wait_for_map_load">Wait for Map Load</string>
325+
318326
</resources>

0 commit comments

Comments
 (0)