-
Notifications
You must be signed in to change notification settings - Fork 124
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge branch 'master' into PolygonDrawingViewModel
- Loading branch information
Showing
4 changed files
with
148 additions
and
155 deletions.
There are no files selected for viewing
74 changes: 0 additions & 74 deletions
74
gnd/src/main/java/com/google/android/gnd/ui/MarkerIconFactory.java
This file was deleted.
Oops, something went wrong.
63 changes: 63 additions & 0 deletions
63
gnd/src/main/java/com/google/android/gnd/ui/MarkerIconFactory.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
/* | ||
* Copyright 2018 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 | ||
* | ||
* https://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.google.android.gnd.ui | ||
|
||
import android.content.Context | ||
import javax.inject.Singleton | ||
import javax.inject.Inject | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import android.graphics.Bitmap | ||
import android.graphics.Canvas | ||
import androidx.appcompat.content.res.AppCompatResources | ||
import com.google.android.gnd.R | ||
import com.google.android.gnd.ui.home.mapcontainer.MapContainerViewModel | ||
import android.graphics.PorterDuff | ||
import androidx.annotation.ColorInt | ||
import androidx.core.content.res.ResourcesCompat | ||
import com.google.android.gms.maps.model.BitmapDescriptor | ||
import com.google.android.gms.maps.model.BitmapDescriptorFactory | ||
|
||
@Singleton | ||
class MarkerIconFactory @Inject constructor(@ApplicationContext private val context: Context) { | ||
fun getMarkerBitmap(color: Int, currentZoomLevel: Float): Bitmap { | ||
val outline = AppCompatResources.getDrawable(context, R.drawable.ic_marker_outline) | ||
val fill = AppCompatResources.getDrawable(context, R.drawable.ic_marker_fill) | ||
val overlay = AppCompatResources.getDrawable(context, R.drawable.ic_marker_overlay) | ||
// TODO: Adjust size based on selection state. | ||
var scale = ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_default_scale) | ||
if (currentZoomLevel >= MapContainerViewModel.ZOOM_LEVEL_THRESHOLD) { | ||
scale = ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_zoomed_scale) | ||
} | ||
val width = (outline!!.intrinsicWidth * scale).toInt() | ||
val height = (outline.intrinsicHeight * scale).toInt() | ||
val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) | ||
val canvas = Canvas(bitmap) | ||
outline.setBounds(0, 0, width, height) | ||
outline.draw(canvas) | ||
fill!!.setColorFilter(color, PorterDuff.Mode.SRC_ATOP) | ||
fill.setBounds(0, 0, width, height) | ||
fill.draw(canvas) | ||
overlay!!.setBounds(0, 0, width, height) | ||
overlay.draw(canvas) | ||
return bitmap | ||
} | ||
|
||
fun getMarkerIcon(@ColorInt color: Int, currentZoomLevel: Float): BitmapDescriptor { | ||
val bitmap = getMarkerBitmap(color, currentZoomLevel) | ||
// TODO: Cache rendered bitmaps. | ||
return BitmapDescriptorFactory.fromBitmap(bitmap) | ||
} | ||
} |
81 changes: 0 additions & 81 deletions
81
gnd/src/test/java/com/google/android/gnd/ui/MarkerIconFactoryTest.java
This file was deleted.
Oops, something went wrong.
85 changes: 85 additions & 0 deletions
85
gnd/src/test/java/com/google/android/gnd/ui/MarkerIconFactoryTest.kt
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,85 @@ | ||
/* | ||
* Copyright 2021 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 | ||
* | ||
* https://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.google.android.gnd.ui | ||
|
||
import android.content.Context | ||
import dagger.hilt.android.testing.HiltAndroidTest | ||
import org.junit.runner.RunWith | ||
import org.robolectric.RobolectricTestRunner | ||
import com.google.android.gnd.BaseHiltTest | ||
import javax.inject.Inject | ||
import dagger.hilt.android.qualifiers.ApplicationContext | ||
import org.junit.Before | ||
import androidx.appcompat.content.res.AppCompatResources | ||
import com.google.android.gnd.R | ||
import android.graphics.Bitmap | ||
import android.graphics.Color | ||
import androidx.core.content.res.ResourcesCompat | ||
import com.google.android.gnd.ui.home.mapcontainer.MapContainerViewModel | ||
import com.google.common.truth.Truth.assertThat | ||
import org.junit.Test | ||
|
||
@HiltAndroidTest | ||
@RunWith(RobolectricTestRunner::class) | ||
class MarkerIconFactoryTest : BaseHiltTest() { | ||
@Inject | ||
@ApplicationContext | ||
lateinit var context: Context | ||
|
||
@Inject | ||
lateinit var markerIconFactory: MarkerIconFactory | ||
private var markerUnscaledWidth = 0 | ||
private var markerUnscaledHeight = 0 | ||
|
||
@Before | ||
override fun setUp() { | ||
super.setUp() | ||
val outline = AppCompatResources.getDrawable(context, R.drawable.ic_marker_outline) | ||
markerUnscaledWidth = outline!!.intrinsicWidth | ||
markerUnscaledHeight = outline.intrinsicHeight | ||
} | ||
|
||
@Test | ||
fun markerBitmap_zoomedOut_scaleIsSetCorrectly() { | ||
val bitmap = markerIconFactory.getMarkerBitmap( | ||
Color.BLUE, | ||
MapContainerViewModel.ZOOM_LEVEL_THRESHOLD - 0.1f | ||
) | ||
|
||
val scale = | ||
ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_default_scale) | ||
verifyBitmapScale(bitmap, scale) | ||
} | ||
|
||
@Test | ||
fun markerBitmap_zoomedIn_scaleIsSetCorrectly() { | ||
val bitmap = markerIconFactory.getMarkerBitmap( | ||
Color.BLUE, | ||
MapContainerViewModel.ZOOM_LEVEL_THRESHOLD | ||
) | ||
|
||
val scale = | ||
ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_zoomed_scale) | ||
verifyBitmapScale(bitmap, scale) | ||
} | ||
|
||
private fun verifyBitmapScale(bitmap: Bitmap, scale: Float) { | ||
val expectedWidth = (markerUnscaledWidth * scale).toInt() | ||
val expectedHeight = (markerUnscaledHeight * scale).toInt() | ||
assertThat(bitmap.width).isEqualTo(expectedWidth) | ||
assertThat(bitmap.height).isEqualTo(expectedHeight) | ||
} | ||
} |