1
1
package com.hfad.minipaint
2
2
3
3
import android.content.Context
4
- import android.graphics.Bitmap
5
- import android.graphics.Canvas
4
+ import android.graphics.*
5
+ import android.view.MotionEvent
6
6
import android.view.View
7
+ import android.view.ViewConfiguration
7
8
import androidx.core.content.res.ResourcesCompat
8
9
10
+ private const val STROKE_WIDTH = 12f
11
+
9
12
class MyCanvasView (context : Context ) : View(context) {
10
13
private lateinit var extraCanvas: Canvas
11
14
private lateinit var extraBitmap: Bitmap
15
+ private lateinit var frame: Rect
12
16
private val backgroundColor = ResourcesCompat .getColor(resources, R .color.colorBackground, null )
17
+ private val drawColor = ResourcesCompat .getColor(resources, R .color.colorPaint, null )
18
+ private val paint = Paint ().apply {
19
+ color = drawColor
20
+ isAntiAlias = true
21
+ isDither = true
22
+ style = Paint .Style .STROKE
23
+ strokeJoin = Paint .Join .ROUND
24
+ strokeCap = Paint .Cap .ROUND
25
+ strokeWidth = STROKE_WIDTH
26
+ }
27
+ private val touchTolerance = ViewConfiguration .get(context).scaledTouchSlop
13
28
14
29
override fun onSizeChanged (w : Int , h : Int , oldw : Int , oldh : Int ) {
15
30
super .onSizeChanged(w, h, oldw, oldh)
16
31
if (::extraBitmap.isInitialized) extraBitmap.recycle() // avoid memory leak
17
32
extraBitmap = Bitmap .createBitmap(w, h, Bitmap .Config .ARGB_8888 )
18
33
extraCanvas = Canvas (extraBitmap)
19
34
extraCanvas.drawColor(backgroundColor)
35
+ // add a rectangular frame
36
+ val inset = 40
37
+ frame = Rect (inset, inset, w - inset, h - inset)
38
+ extraCanvas.drawRect(frame, paint)
20
39
}
21
40
22
41
override fun onDraw (canvas : Canvas ) {
23
42
super .onDraw(canvas)
24
43
canvas.drawBitmap(extraBitmap, 0f , 0f , null )
25
44
}
45
+
46
+ override fun onTouchEvent (event : MotionEvent ): Boolean {
47
+ motionTouchEventX = event.x
48
+ motionTouchEventY = event.y
49
+
50
+ when (event.action) {
51
+ MotionEvent .ACTION_DOWN -> touchStart()
52
+ MotionEvent .ACTION_MOVE -> touchMove()
53
+ MotionEvent .ACTION_UP -> touchUp()
54
+ }
55
+ return true
56
+ }
57
+
58
+ private fun touchStart () {
59
+ path.reset()
60
+ path.moveTo(motionTouchEventX, motionTouchEventY)
61
+ currentX = motionTouchEventX
62
+ currentY = motionTouchEventY
63
+ }
64
+
65
+ private fun touchMove () {
66
+ val dx = Math .abs(motionTouchEventX - currentX)
67
+ val dy = Math .abs(motionTouchEventY - currentY)
68
+ if (dx >= touchTolerance || dy >= touchTolerance) {
69
+ // QuadTo() adds a quadratic bezier from the last point,
70
+ // approaching control point (x1,y1), and ending at (x2,y2).
71
+ path.quadTo(currentX, currentY, (motionTouchEventX + currentX) / 2 , (motionTouchEventY + currentY) / 2 )
72
+ currentX = motionTouchEventX
73
+ currentY = motionTouchEventY
74
+ // Draw the path in the extra bitmap to cache it.
75
+ extraCanvas.drawPath(path, paint)
76
+ }
77
+ invalidate()
78
+ }
79
+
80
+ private fun touchUp () {
81
+ path.reset()
82
+ }
83
+
84
+ companion object {
85
+ private var path = Path ()
86
+ private var motionTouchEventX = 0f
87
+ private var motionTouchEventY = 0f
88
+ private var currentX = 0f
89
+ private var currentY = 0f
90
+ }
26
91
}
0 commit comments