Skip to content

Commit 53b37fc

Browse files
committed
fix(canvas): translate touch point properly before bucket fill
See: #20
1 parent 83d964b commit 53b37fc

File tree

1 file changed

+30
-17
lines changed

1 file changed

+30
-17
lines changed

app/src/main/kotlin/org/fossify/paint/views/MyCanvas.kt

Lines changed: 30 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ import android.content.Context
55
import android.graphics.Bitmap
66
import android.graphics.Canvas
77
import android.graphics.Color
8+
import android.graphics.Matrix
89
import android.graphics.Paint
910
import android.graphics.Point
1011
import android.graphics.PointF
@@ -394,23 +395,35 @@ class MyCanvas(context: Context, attrs: AttributeSet) : View(context, attrs) {
394395
}
395396

396397
private fun bucketFill() {
397-
val touchedX = mCurX.toInt()
398-
val touchedY = mCurY.toInt()
399-
if (contains(touchedX, touchedY)) {
400-
val bitmap = getBitmap()
401-
val color = mPaintOptions.color
402-
403-
ensureBackgroundThread {
404-
val path = bitmap.vectorFloodFill(
405-
color = color,
406-
x = touchedX,
407-
y = touchedY,
408-
tolerance = FLOOD_FILL_TOLERANCE
409-
)
410-
val paintOpts = PaintOptions(color = color, strokeWidth = 5f)
411-
addOperation(path, paintOpts)
412-
post { invalidate() }
413-
}
398+
if (mCenter == null) {
399+
mCenter = PointF(width / 2f, height / 2f)
400+
}
401+
402+
val touchedX = mLastTouchX.toInt()
403+
val touchedY = mLastTouchY.toInt()
404+
if (!contains(touchedX, touchedY)) return
405+
406+
val toScreen = Matrix().apply {
407+
preTranslate(mPosX, mPosY)
408+
preScale(mScaleFactor, mScaleFactor, mCenter!!.x, mCenter!!.y)
409+
}
410+
411+
val fromScreen = Matrix().apply { toScreen.invert(this) }
412+
413+
val bitmap = getBitmap()
414+
val color = mPaintOptions.color
415+
ensureBackgroundThread {
416+
val path = bitmap.vectorFloodFill(
417+
color = color,
418+
x = touchedX,
419+
y = touchedY,
420+
tolerance = FLOOD_FILL_TOLERANCE
421+
)
422+
423+
path.transform(fromScreen)
424+
val paintOpts = PaintOptions(color = color, strokeWidth = 5f)
425+
addOperation(path, paintOpts)
426+
post { invalidate() }
414427
}
415428
}
416429

0 commit comments

Comments
 (0)