Skip to content

Commit

Permalink
workaround canvas drawing bug in Android 6 (fixes #1879)
Browse files Browse the repository at this point in the history
  • Loading branch information
westnordost committed Jun 4, 2020
1 parent 23882a3 commit 1ae4170
Showing 1 changed file with 14 additions and 2 deletions.
@@ -1,6 +1,7 @@
package de.westnordost.streetcomplete.map

import android.content.Context
import android.graphics.Bitmap
import android.graphics.Canvas
import android.graphics.Outline
import android.graphics.drawable.Drawable
Expand All @@ -27,6 +28,7 @@ class PointerPinView @JvmOverloads constructor(
) : View(context, attrs, defStyleAttr) {

private val pointerPin: Drawable = context.resources.getDrawable(R.drawable.quest_pin_pointer)
private var pointerPinBitmap: Bitmap? = null

/** rotation of the pin in degrees. Similar to rotation, only that the pointy end of the pin
* is always located at the edge of the view */
Expand Down Expand Up @@ -91,15 +93,25 @@ class PointerPinView @JvmOverloads constructor(
setMeasuredDimension(width, height)
}

override fun onSizeChanged(w: Int, h: Int, oldw: Int, oldh: Int) {
pointerPinBitmap?.recycle()

val size = min(width, height)
val bmp = Bitmap.createBitmap(size, size, Bitmap.Config.ARGB_8888)
val canvas = Canvas(bmp)
pointerPin.setBounds(0,0, size, size)
pointerPin.draw(canvas)
pointerPinBitmap = bmp
}

override fun onDraw(canvas: Canvas?) {
val c = canvas ?: return

val size = min(width, height)
val r = pinRotation

pointerPin.setBounds(0,0, size, size)
c.withRotation(r, width/2f, height/2f) {
pointerPin.draw(c)
pointerPinBitmap?.let { canvas.drawBitmap(it, 0f, 0f, null) }
}

val icon = pinIconDrawable
Expand Down

0 comments on commit 1ae4170

Please sign in to comment.