chore: Add GroundOverlayDemoActivity in Kotlin (#231)

Also apply following changes to Java version:
* Refactor variables to remove Hungarian notation
* Remove redundant cast for SeekBar
This commit is contained in:
Sean Barbeau 2020-06-01 11:17:27 -04:00 committed by GitHub
parent 7f1efcdab9
commit 92290d690d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
8 changed files with 233 additions and 23 deletions

View File

@ -50,24 +50,24 @@ public class GroundOverlayDemoActivity extends AppCompatActivity
private static final LatLng NEAR_NEWARK =
new LatLng(NEWARK.latitude - 0.001, NEWARK.longitude - 0.025);
private final List<BitmapDescriptor> mImages = new ArrayList<BitmapDescriptor>();
private final List<BitmapDescriptor> images = new ArrayList<BitmapDescriptor>();
private GroundOverlay mGroundOverlay;
private GroundOverlay groundOverlay;
private GroundOverlay mGroundOverlayRotated;
private GroundOverlay groundOverlayRotated;
private SeekBar mTransparencyBar;
private SeekBar transparencyBar;
private int mCurrentEntry = 0;
private int currentEntry = 0;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.ground_overlay_demo);
mTransparencyBar = (SeekBar) findViewById(R.id.transparencySeekBar);
mTransparencyBar.setMax(TRANSPARENCY_MAX);
mTransparencyBar.setProgress(0);
transparencyBar = findViewById(R.id.transparencySeekBar);
transparencyBar.setMax(TRANSPARENCY_MAX);
transparencyBar.setProgress(0);
SupportMapFragment mapFragment =
(SupportMapFragment) getSupportFragmentManager().findFragmentById(R.id.map);
@ -81,24 +81,24 @@ public class GroundOverlayDemoActivity extends AppCompatActivity
map.moveCamera(CameraUpdateFactory.newLatLngZoom(NEWARK, 11));
mImages.clear();
mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_nj_1922));
mImages.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_prudential_sunny));
images.clear();
images.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_nj_1922));
images.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_prudential_sunny));
// Add a small, rotated overlay that is clickable by default
// (set by the initial state of the checkbox.)
mGroundOverlayRotated = map.addGroundOverlay(new GroundOverlayOptions()
.image(mImages.get(1)).anchor(0, 1)
groundOverlayRotated = map.addGroundOverlay(new GroundOverlayOptions()
.image(images.get(1)).anchor(0, 1)
.position(NEAR_NEWARK, 4300f, 3025f)
.bearing(30)
.clickable(((CheckBox) findViewById(R.id.toggleClickability)).isChecked()));
// Add a large overlay at Newark on top of the smaller overlay.
mGroundOverlay = map.addGroundOverlay(new GroundOverlayOptions()
.image(mImages.get(mCurrentEntry)).anchor(0, 1)
groundOverlay = map.addGroundOverlay(new GroundOverlayOptions()
.image(images.get(currentEntry)).anchor(0, 1)
.position(NEWARK, 8600f, 6500f));
mTransparencyBar.setOnSeekBarChangeListener(this);
transparencyBar.setOnSeekBarChangeListener(this);
// Override the default content description on the view, for accessibility mode.
// Ideally this string would be localised.
@ -115,14 +115,14 @@ public class GroundOverlayDemoActivity extends AppCompatActivity
@Override
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
if (mGroundOverlay != null) {
mGroundOverlay.setTransparency((float) progress / (float) TRANSPARENCY_MAX);
if (groundOverlay != null) {
groundOverlay.setTransparency((float) progress / (float) TRANSPARENCY_MAX);
}
}
public void switchImage(View view) {
mCurrentEntry = (mCurrentEntry + 1) % mImages.size();
mGroundOverlay.setImage(mImages.get(mCurrentEntry));
currentEntry = (currentEntry + 1) % images.size();
groundOverlay.setImage(images.get(currentEntry));
}
/**
@ -131,7 +131,7 @@ public class GroundOverlayDemoActivity extends AppCompatActivity
@Override
public void onGroundOverlayClick(GroundOverlay groundOverlay) {
// Toggle transparency value between 0.0f and 0.5f. Initial default value is 0.0f.
mGroundOverlayRotated.setTransparency(0.5f - mGroundOverlayRotated.getTransparency());
groundOverlayRotated.setTransparency(0.5f - groundOverlayRotated.getTransparency());
}
/**
@ -140,8 +140,8 @@ public class GroundOverlayDemoActivity extends AppCompatActivity
* This callback is defined on the CheckBox in the layout for this Activity.
*/
public void toggleClickability(View view) {
if (mGroundOverlayRotated != null) {
mGroundOverlayRotated.setClickable(((CheckBox) view).isChecked());
if (groundOverlayRotated != null) {
groundOverlayRotated.setClickable(((CheckBox) view).isChecked());
}
}
}

View File

@ -35,6 +35,10 @@ class DemoDetailsList {
R.string.events_demo_label,
R.string.events_demo_details,
EventsDemoActivity::class.java),
DemoDetails(
R.string.ground_overlay_demo_label,
R.string.ground_overlay_demo_details,
GroundOverlayDemoActivity::class.java),
DemoDetails(R.string.layers_demo_label, R.string.layers_demo_description,
LayersDemoActivity::class.java),
DemoDetails(R.string.lite_demo_label, R.string.lite_demo_details,

View File

@ -0,0 +1,131 @@
// Copyright 2020 Google LLC
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
package com.example.kotlindemos
import android.os.Bundle
import android.view.View
import android.widget.CheckBox
import android.widget.SeekBar
import android.widget.SeekBar.OnSeekBarChangeListener
import androidx.appcompat.app.AppCompatActivity
import com.google.android.gms.maps.CameraUpdateFactory
import com.google.android.gms.maps.GoogleMap
import com.google.android.gms.maps.GoogleMap.OnGroundOverlayClickListener
import com.google.android.gms.maps.OnMapReadyCallback
import com.google.android.gms.maps.SupportMapFragment
import com.google.android.gms.maps.model.*
import java.util.*
/**
* This shows how to add a ground overlay to a map.
*/
class GroundOverlayDemoActivity : AppCompatActivity(), OnSeekBarChangeListener,
OnMapReadyCallback, OnGroundOverlayClickListener {
private val images: MutableList<BitmapDescriptor> = ArrayList()
private var groundOverlay: GroundOverlay? = null
private var groundOverlayRotated: GroundOverlay? = null
private lateinit var transparencyBar: SeekBar
private var currentEntry = 0
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(R.layout.ground_overlay_demo)
transparencyBar = findViewById(R.id.transparencySeekBar)
transparencyBar.max = TRANSPARENCY_MAX
transparencyBar.progress = 0
val mapFragment =
supportFragmentManager.findFragmentById(R.id.map) as SupportMapFragment?
mapFragment?.getMapAsync(this)
}
override fun onMapReady(map: GoogleMap?) {
map ?: return
// Register a listener to respond to clicks on GroundOverlays.
map.setOnGroundOverlayClickListener(this)
map.moveCamera(
CameraUpdateFactory.newLatLngZoom(
NEWARK,
11f
)
)
images.clear()
images.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_nj_1922))
images.add(BitmapDescriptorFactory.fromResource(R.drawable.newark_prudential_sunny))
// Add a small, rotated overlay that is clickable by default
// (set by the initial state of the checkbox.)
groundOverlayRotated = map.addGroundOverlay(
GroundOverlayOptions()
.image(images[1]).anchor(0f, 1f)
.position(NEAR_NEWARK, 4300f, 3025f)
.bearing(30f)
.clickable((findViewById<View>(R.id.toggleClickability) as CheckBox).isChecked)
)
// Add a large overlay at Newark on top of the smaller overlay.
groundOverlay = map.addGroundOverlay(
GroundOverlayOptions()
.image(images[currentEntry]).anchor(0f, 1f)
.position(NEWARK, 8600f, 6500f)
)
transparencyBar.setOnSeekBarChangeListener(this)
// Override the default content description on the view, for accessibility mode.
// Ideally this string would be localised.
map.setContentDescription("Google Map with ground overlay.")
}
override fun onStopTrackingTouch(seekBar: SeekBar) {}
override fun onStartTrackingTouch(seekBar: SeekBar) {}
override fun onProgressChanged(seekBar: SeekBar, progress: Int, fromUser: Boolean) {
groundOverlay?.transparency = progress.toFloat() / TRANSPARENCY_MAX.toFloat()
}
fun switchImage(view: View?) {
val overlay = groundOverlay ?: return
currentEntry = (currentEntry + 1) % images.size
overlay.setImage(images[currentEntry])
}
/**
* Toggles the visibility between 100% and 50% when a [GroundOverlay] is clicked.
*/
override fun onGroundOverlayClick(groundOverlay: GroundOverlay) {
// Toggle transparency value between 0.0f and 0.5f. Initial default value is 0.0f.
val overlayRotated = groundOverlayRotated ?: return
overlayRotated.transparency = 0.5f - overlayRotated.transparency
}
/**
* Toggles the clickability of the smaller, rotated overlay based on the state of the View that
* triggered this call.
* This callback is defined on the CheckBox in the layout for this Activity.
*/
fun toggleClickability(view: View) {
groundOverlayRotated?.isClickable = (view as CheckBox).isChecked
}
companion object {
private const val TRANSPARENCY_MAX = 100
private val NEWARK = LatLng(40.714086, -74.228697)
private val NEAR_NEWARK = LatLng(
NEWARK.latitude - 0.001,
NEWARK.longitude - 0.025
)
}
}

View File

@ -0,0 +1,68 @@
<?xml version="1.0" encoding="utf-8"?><!--
Copyright (C) 2012 The Android Open Source Project
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
-->
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:orientation="vertical">
<RelativeLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:background="@color/white"
android:padding="5dp">
<TextView
android:id="@+id/transparency_text"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_gravity="center_vertical"
android:text="@string/transparency" />
<SeekBar
android:id="@+id/transparencySeekBar"
android:layout_width="fill_parent"
android:layout_height="wrap_content"
android:layout_toEndOf="@+id/transparency_text"
android:layout_toRightOf="@+id/transparency_text" />
<Button
android:id="@+id/switchImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@+id/transparencySeekBar"
android:onClick="switchImage"
android:text="@string/switch_image" />
<CheckBox
android:id="@+id/toggleClickability"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_alignBottom="@+id/switchImage"
android:layout_toEndOf="@+id/switchImage"
android:layout_toRightOf="@+id/switchImage"
android:checked="true"
android:onClick="toggleClickability"
android:text="@string/clickable" />
</RelativeLayout>
<fragment
android:id="@+id/map"
class="com.google.android.gms.maps.SupportMapFragment"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>

View File

@ -64,6 +64,7 @@
<activity android:name=".StreetViewPanoramaViewDemoActivity" />
<activity android:name=".LiteDemoActivity" />
<activity android:name=".EventsDemoActivity" />
<activity android:name=".GroundOverlayDemoActivity" />
</application>
</manifest>

Binary file not shown.

After

Width:  |  Height:  |  Size: 340 KiB

Binary file not shown.

After

Width:  |  Height:  |  Size: 68 KiB

View File

@ -55,6 +55,12 @@
<string name="move_the_camera">Move the camera</string>
<string name="tap_instructions">Tap or long press on the map</string>
<!-- GroundOverLay Demo -->
<string name="ground_overlay_demo_label">Ground Overlays</string>
<string name="ground_overlay_demo_details">Demonstrates how to add a GroundOverlay to a map.</string>
<string name="transparency">Transparency</string>
<string name="switch_image">Switch Image</string>
<!-- Layers Demo -->
<string name="buildings">Buildings</string>
<string name="hybrid">Hybrid</string>