-
Notifications
You must be signed in to change notification settings - Fork 8
/
P42_a.kt
53 lines (42 loc) · 1.18 KB
/
P42_a.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
package com.example.cse225_one
import android.content.Context
import android.graphics.Canvas
import android.graphics.Color
import android.graphics.Paint
import android.graphics.Path
import android.util.AttributeSet
import android.view.MotionEvent
import android.view.View
class CustomDrawing2 : View {
lateinit var p: Paint
lateinit var mpath: Path
constructor(context: Context?) : super(context) {
init()
}
constructor(context: Context?, attributeSet: AttributeSet) : super(context, attributeSet) {
init()
}
fun init() {
p = Paint()
mpath = Path()
p.color = Color.BLACK
p.style = Paint.Style.STROKE
p.strokeWidth = 10f
}
override fun onDraw(canvas: Canvas) {
canvas.drawColor(Color.MAGENTA)
canvas.drawPath(mpath, p)
}
override fun onTouchEvent(event: MotionEvent?): Boolean {
when (event?.action) {
MotionEvent.ACTION_DOWN -> mpath.moveTo(event.x, event.y)
MotionEvent.ACTION_MOVE -> mpath.lineTo(event.x, event.y)
}
invalidate()
return true
}
fun clearCanvas() {
mpath.reset()
postInvalidate()
}
}