Android Application
Kotlin 그래픽(graphic) : 간단 그림판 1
작성자 임베디드코리아
작성일 23-10-17 00:41
조회1,548회
댓글0건
------------------------------------------------------------------------------------
MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
companion object {
const val LINE = 1
const val CIRCLE = 2
var curShape = LINE
}
override fun onCreate(savedInstanceState: Bundle?) {
super.onCreate(savedInstanceState)
//setContentView(R.layout.activity_main)
setContentView(MyGraphicView(this))
title = "간단 그림판"
}
override fun onCreateOptionsMenu(menu: Menu?): Boolean {
super.onCreateOptionsMenu(menu)
menu!!.add(0, 1, 0, "선 그리기")
menu!!.add(0, 2, 0, "원 그리기")
return true
}
override fun onOptionsItemSelected(item: MenuItem): Boolean {
when (item.itemId) {
1 -> {
curShape = LINE // 선
return true
}
2 -> {
curShape = CIRCLE // 원
return true
}
}
return super.onOptionsItemSelected(item)
}
private class MyGraphicView(context: Context) : View(context) {
var startX = -1f
var startY = -1f
var stopX = -1f
var stopY = -1f
override fun onTouchEvent(event: MotionEvent?): Boolean {
when (event!!.action) {
MotionEvent.ACTION_DOWN -> {
startX = event.x
startY = event.y
}
MotionEvent.ACTION_MOVE, MotionEvent.ACTION_UP -> {
stopX = event.x
stopY = event.y
this.invalidate()
}
}
return true
}
override fun onDraw(canvas: Canvas) {
super.onDraw(canvas)
val paint = Paint()
paint.isAntiAlias = true
paint.strokeWidth = 5f
paint.style = Paint.Style.STROKE
paint.color = Color.RED
when (curShape) {
LINE -> canvas.drawLine(startX, startY, stopX, stopY, paint)
CIRCLE -> {
var radius = Math.sqrt(Math.pow((stopX - startX).toDouble(), 2.0) + Math.pow((stopY - startY).toDouble(), 2.0))
canvas.drawCircle(startX, startY, radius.toFloat(), paint)
}
}
}
}
}
------------------------------------------------------------------------------------
themes.xml : 앱바 보이기
------------------------------------------------------------------------------------
<resources xmlns:tools="http://schemas.android.com/tools">
<!-- Base application theme. -->
<style name="Base.Theme.SimplePaintBoard" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
<!-- Customize your light theme here. -->
<!-- <item name="colorPrimary">@color/my_light_primary</item> -->
</style>
<style name="Theme.SimplePaintBoard" parent="Base.Theme.SimplePaintBoard" />
</resources>
158-840 서울시 양천구 남부순환로 571(신월동, 영남타운 307호) l 대표: 박길성 ㅣ Tel:02-2695-1114 ㅣ Fax:02-2695-1113
Copyright © 2015 Embedded Korea. All Rights Reserved.