|
| 1 | +/* |
| 2 | + * Copyright 2018 Google LLC |
| 3 | + * |
| 4 | + * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + * you may not use this file except in compliance with the License. |
| 6 | + * You may obtain a copy of the License at |
| 7 | + * |
| 8 | + * https://www.apache.org/licenses/LICENSE-2.0 |
| 9 | + * |
| 10 | + * Unless required by applicable law or agreed to in writing, software |
| 11 | + * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + * See the License for the specific language governing permissions and |
| 14 | + * limitations under the License. |
| 15 | + */ |
| 16 | +package com.google.android.gnd.ui |
| 17 | + |
| 18 | +import android.content.Context |
| 19 | +import javax.inject.Singleton |
| 20 | +import javax.inject.Inject |
| 21 | +import dagger.hilt.android.qualifiers.ApplicationContext |
| 22 | +import android.graphics.Bitmap |
| 23 | +import android.graphics.Canvas |
| 24 | +import androidx.appcompat.content.res.AppCompatResources |
| 25 | +import com.google.android.gnd.R |
| 26 | +import com.google.android.gnd.ui.home.mapcontainer.MapContainerViewModel |
| 27 | +import android.graphics.PorterDuff |
| 28 | +import androidx.annotation.ColorInt |
| 29 | +import androidx.core.content.res.ResourcesCompat |
| 30 | +import com.google.android.gms.maps.model.BitmapDescriptor |
| 31 | +import com.google.android.gms.maps.model.BitmapDescriptorFactory |
| 32 | + |
| 33 | +@Singleton |
| 34 | +class MarkerIconFactory @Inject constructor(@ApplicationContext private val context: Context) { |
| 35 | + fun getMarkerBitmap(color: Int, currentZoomLevel: Float): Bitmap { |
| 36 | + val outline = AppCompatResources.getDrawable(context, R.drawable.ic_marker_outline) |
| 37 | + val fill = AppCompatResources.getDrawable(context, R.drawable.ic_marker_fill) |
| 38 | + val overlay = AppCompatResources.getDrawable(context, R.drawable.ic_marker_overlay) |
| 39 | + // TODO: Adjust size based on selection state. |
| 40 | + var scale = ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_default_scale) |
| 41 | + if (currentZoomLevel >= MapContainerViewModel.ZOOM_LEVEL_THRESHOLD) { |
| 42 | + scale = ResourcesCompat.getFloat(context.resources, R.dimen.marker_bitmap_zoomed_scale) |
| 43 | + } |
| 44 | + val width = (outline!!.intrinsicWidth * scale).toInt() |
| 45 | + val height = (outline.intrinsicHeight * scale).toInt() |
| 46 | + val bitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888) |
| 47 | + val canvas = Canvas(bitmap) |
| 48 | + outline.setBounds(0, 0, width, height) |
| 49 | + outline.draw(canvas) |
| 50 | + fill!!.setColorFilter(color, PorterDuff.Mode.SRC_ATOP) |
| 51 | + fill.setBounds(0, 0, width, height) |
| 52 | + fill.draw(canvas) |
| 53 | + overlay!!.setBounds(0, 0, width, height) |
| 54 | + overlay.draw(canvas) |
| 55 | + return bitmap |
| 56 | + } |
| 57 | + |
| 58 | + fun getMarkerIcon(@ColorInt color: Int, currentZoomLevel: Float): BitmapDescriptor { |
| 59 | + val bitmap = getMarkerBitmap(color, currentZoomLevel) |
| 60 | + // TODO: Cache rendered bitmaps. |
| 61 | + return BitmapDescriptorFactory.fromBitmap(bitmap) |
| 62 | + } |
| 63 | +} |
0 commit comments