반응형
[Gradle 설정]
1
|
id 'kotlin-android-extensions'
|
cs |
<MainActivity>
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
|
package com.cookandroid.project9_1
import android.content.Context
import android.content.res.Resources
import android.graphics.*
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.Menu
import android.view.MenuItem
import android.view.MotionEvent
import android.view.View
import androidx.core.graphics.scale
import com.cookandroid.project9_1.MainActivity.Companion.curShape
class MainActivity : AppCompatActivity() {
companion object{
const val LINE = 1
const val CIRCLE = 2
const val RECT = 3
const val IMAGE = 4
var curColor = Color.RED
var curShape = LINE
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
setContentView(MyGraphicView(this))
title = "간단 그림판"
}
private class MyGraphicView(context: Context) : View(context){
var startX = -1
var startY = -1
var stopX = -1
var stopY = -1
override fun onTouchEvent(event: MotionEvent?): Boolean {
when(event!!.action){
MotionEvent.ACTION_DOWN ->{
startX = event.x.toInt()
startY = event.y.toInt()
}
MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP -> {
stopX = event.x.toInt()
stopY = event.y.toInt()
this.invalidate()
}
}
return true
}
override fun onDraw(canvas: Canvas?) {
super.onDraw(canvas)
val paint = Paint()
paint.isAntiAlias = true
paint.style = Paint.Style.STROKE
paint.strokeWidth = 5f
paint.color = curColor
when(curShape){
LINE -> {
canvas!!.drawLine(startX.toFloat(), startY.toFloat(),
stopX.toFloat(), stopY.toFloat(), paint)
}
CIRCLE -> {
var radius = Math.sqrt(Math.pow((stopX - startX).toDouble() , 2.0) + Math.pow((stopY - startY).toDouble(), 2.0))
canvas!!.drawCircle(startX.toFloat(), startY.toFloat(), radius.toFloat(), paint)
}
RECT -> {
canvas!!.drawRect(RectF(startX.toFloat(), startY.toFloat(),
stopX.toFloat(), stopY.toFloat()
), paint)
}
IMAGE -> {
var picture = BitmapFactory.decodeResource(resources, R.drawable.jeju14)
picture.scale(500, 300)
canvas!!.drawBitmap(picture, stopX.toFloat(), stopY.toFloat(), paint)
}
}
}
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
menuInflater.inflate(R.menu.menu1, menu)
return super.onCreateOptionsMenu(menu)
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when(item.itemId){
R.id.itemLine ->{
curShape = LINE
}
R.id.itemCircle ->{
curShape = CIRCLE
}
R.id.itemRect -> {
curShape = RECT
}
R.id.itemImage -> {
curShape = IMAGE
}
R.id.itemRed -> {
curColor = Color.RED
}
R.id.itemBlue -> {
curColor = Color.BLUE
}
R.id.itemGreen -> {
curColor = Color.GREEN
}
}
return super.onOptionsItemSelected(item)
}
}
|
cs |
<menu1.xml>
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
|
<?xml version="1.0" encoding="utf-8"?>
<menu xmlns:android="http://schemas.android.com/apk/res/android">
<item
android:id="@+id/itemLine"
android:title="선 그리기">
</item>
<item
android:id="@+id/itemCircle"
android:title="원 그리기">
</item>
<item
android:id="@+id/itemRect"
android:title="사각형 그리기">
</item>
<item
android:id="@+id/itemImage"
android:title="이미지 그리기">
</item>
<item
android:id="@+id/itemColor"
android:title="색상 변경>>">
<menu>
<item
android:id="@+id/itemRed"
android:title="빨강색">
</item>
<item
android:id="@+id/itemBlue"
android:title="파랑색">
</item>
<item
android:id="@+id/itemGreen"
android:title="초록색">
</item>
</menu>
</item>
</menu>
|
cs |
반응형
'안드로이드 프로그래밍 > 코틀린' 카테고리의 다른 글
[코틀린을 활용한 안드로이드 프로그래밍] 11장 직접 풀어보기 11-1 (0) | 2022.06.08 |
---|---|
[코틀린을 활용한 안드로이드 프로그래밍] 9장 직접 풀어보기 9-3 (0) | 2022.05.26 |
[코틀린을 활용한 안드로이드 프로그래밍] 8장 직접 풀어보기 8-2 (0) | 2022.05.25 |
[코틀린을 활용한 안드로이드 프로그래밍] 8장 직접 풀어보기 8-1 (0) | 2022.05.25 |
[코틀린을 활용한 안드로이드 프로그래밍] 6장 직접 풀어보기 6-3 (0) | 2022.05.25 |