-
Notifications
You must be signed in to change notification settings - Fork 0
/
PinDraw.kt
210 lines (163 loc) · 6.79 KB
/
PinDraw.kt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
package com.shevelev.googlemapsdemo.shared.pin
import android.content.Context
import android.graphics.*
import android.net.Uri
import androidx.annotation.ColorInt
import com.shevelev.googlemapsdemo.R
class PinDraw
constructor(private val context: Context) {
private data class MeasuredText(
val text: String,
val width: Int,
val height: Int,
val baselineOffset: Int
)
private val scaleFactor = 1f
// Sizes [px]
private val height = 410.scale()
private val widthMax = 990.scale()
private val pinWidth = 330.scale()
private val pinImageSize = 300.scale()
private val pinSpearheadWidth = 40.scale()
private val textMargin = 25.scale()
private val textTagHeight = 80.scale()
fun draw(backgroundColor: Int, textColor: Int, image: Uri?, text: String?): PinDrawResult =
draw(backgroundColor, textColor, cropUriImageToCircle(image), text)
private fun draw(backgroundColor: Int, textColor: Int, image: Bitmap?, text: String?): PinDrawResult {
return if(text.isNullOrBlank()) {
drawWithoutText(image, backgroundColor)
} else {
drawWithText(image, text, backgroundColor, textColor)
}
}
private fun drawWithoutText(image: Bitmap?, @ColorInt color: Int) : PinDrawResult {
val output = Bitmap.createBitmap(pinWidth, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)
val paint = Paint()
drawPin(canvas, paint, color, pinWidth, height)
drawImage(canvas, paint, image, pinWidth)
return PinDrawResult(output, 0.5f)
}
private fun drawWithText(image: Bitmap?, text: String, @ColorInt backgroundColor: Int, @ColorInt textColor: Int) : PinDrawResult {
val paint = Paint()
paint.textSize = context.resources.getDimension(R.dimen.textSizeSmall)
val maxTextWidth = widthMax.scale() - pinWidth - textMargin * 2
val measuredText = measureText(text, paint, maxTextWidth.toFloat())
val width = pinWidth + textMargin*2 + measuredText.width + textTagHeight/2
val output = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)
drawPin(
canvas,
paint,
backgroundColor,
pinWidth,
height)
drawTag(
canvas,
paint,
backgroundColor,
textColor,
pinWidth,
textMargin,
measuredText)
drawImage(
canvas,
paint,
image,
pinWidth)
return PinDrawResult(output, (pinWidth / width).toFloat())
}
/**
* Calculates text length to inscribe in into the painted area
* @return painted area len and inscribed
*/
private fun measureText(text: String, paint: Paint, maxTextWidth: Float): MeasuredText {
val textBounds = Rect()
paint.getTextBounds(text, 0, text.length, textBounds)
val textWidth = textBounds.width()
if(textWidth <= maxTextWidth) {
return MeasuredText(text, textBounds.width(), textBounds.height(), textBounds.bottom)
}
val oneCharWidth = textWidth / text.length
val maxChars = (maxTextWidth / oneCharWidth).toInt()
return MeasuredText(text.substring(0, maxChars-3)+"...", maxChars * oneCharWidth, textBounds.height(), textBounds.bottom)
}
private fun drawPin(canvas: Canvas, paint: Paint, @ColorInt color: Int, pinWidth: Int, pinHeight: Int) {
paint.color = color
paint.style = Paint.Style.FILL
// The image background
canvas.drawCircle(pinWidth/2f, pinWidth/2f, pinWidth/2f, paint)
// The pin spearhead
val spearheadPath = Path()
spearheadPath.moveTo((pinWidth-pinSpearheadWidth)/2f, pinWidth*0.95f)
spearheadPath.lineTo((pinWidth+pinSpearheadWidth)/2f, pinWidth*0.95f)
spearheadPath.lineTo(pinWidth/2f, pinHeight.toFloat())
spearheadPath.lineTo((pinWidth-pinSpearheadWidth)/2f, pinWidth*0.95f)
canvas.drawPath(spearheadPath, paint)
}
private fun drawImage(canvas: Canvas, paint: Paint, image: Bitmap?, pinWidth: Int) {
image?.let {
val destOffset = (pinWidth - pinImageSize) / 2
val imageDestRect = Rect(destOffset, destOffset, destOffset + pinImageSize, destOffset + pinImageSize)
canvas.drawBitmap(it, Rect(0, 0, it.width, it.height), imageDestRect, paint)
}
}
private fun drawTag(
canvas: Canvas,
paint: Paint,
@ColorInt backgroundColor: Int,
@ColorInt textColor: Int,
pinWidth: Int,
textMargin: Int,
measuredText: MeasuredText
) {
val path = Path()
paint.color = backgroundColor
paint.style = Paint.Style.FILL
val left = pinWidth*0.95f
val leftOffset = pinWidth - left
val right = left +leftOffset + textMargin + measuredText.width
val top = (pinWidth - textTagHeight) / 2f
val bottom = (pinWidth + textTagHeight) / 2f
// Tag
path.moveTo(left, top)
path.lineTo(right, top)
path.arcTo(right - textTagHeight/2f, top, right + textTagHeight/2f, bottom, 270f, 180f, false)
path.lineTo(left, bottom)
path.lineTo(left, top)
canvas.drawPath(path, paint)
paint.color = textColor
canvas.drawText(
measuredText.text,
(pinWidth + textMargin).toFloat(),
((pinWidth + measuredText.height) / 2f) - measuredText.baselineOffset,
paint)
}
private fun cropUriImageToCircle(imageUri: Uri?): Bitmap? =
imageUri?.let {
val bitmap = context.contentResolver.openInputStream(it).use { stream ->
BitmapFactory.decodeStream(stream)
}
cropBitmapToCircle(bitmap)
}
private fun cropBitmapToCircle(bitmap: Bitmap): Bitmap {
val outputSize = bitmap.width.coerceAtMost(bitmap.height)
val output = Bitmap.createBitmap(outputSize, outputSize, Bitmap.Config.ARGB_8888)
val canvas = Canvas(output)
val paint = Paint()
paint.isAntiAlias = true
canvas.drawARGB(0, 0, 0, 0)
canvas.drawCircle(outputSize/2f, outputSize/2f, outputSize/2f, paint)
paint.xfermode = PorterDuffXfermode(PorterDuff.Mode.SRC_IN)
val bitmapRect = if(bitmap.width > bitmap.height) {
val left = (bitmap.width-outputSize)/2
Rect(left, 0, bitmap.width - left, outputSize)
} else {
val top = (bitmap.height - outputSize) / 2
Rect(0, top, outputSize, bitmap.height - top)
}
canvas.drawBitmap(bitmap, bitmapRect, Rect(0, 0, outputSize, outputSize), paint)
return output
}
private fun Int.scale() = (this*scaleFactor).toInt() + 1
}