Android Application

Kotlin 그래픽(graphic) : 간단 그림판 1

작성자 임베디드코리아 작성일23-10-17 00:48 조회2,029회 댓글0건

첨부파일

-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="wrap_content"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/cxtMenuLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="1"
        android:gravity="center"
        android:orientation="vertical" >
    </LinearLayout>

    <LinearLayout
        android:id="@+id/pictureLayout"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        android:layout_weight="9"
        android:gravity="center"
        android:orientation="vertical" >
    </LinearLayout>

</LinearLayout>


------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    //lateinit var contextMenu: LinearLayout
    private lateinit var graphicView: MyGraphicView

    companion object{
        const val LINE = 1 //선
        const val CIRCLE = 2 //원
        const val RECT = 3 //네모

        var curShape = LINE  //모양
        var curColor = Color.DKGRAY
    }

    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)

        title = "간단한 그림판(코틀린)"

        graphicView = MyGraphicView(this)
        setContentView(graphicView)

    }
    override fun onCreateOptionsMenu(menu: Menu?): Boolean {
        super.onCreateOptionsMenu(menu)

        menu?.add(0, 1, 2, "선 그리기")
        menu?.add(0, 2, 1, "원 그리기")
        menu?.add(0, 3, 0, "네모 그리기")

        return true
    }

    override fun onOptionsItemSelected(item: MenuItem): Boolean {
        when(item.itemId){
            1 ->{
                curShape = LINE
            }
            2 ->{
                curShape = CIRCLE
            }
            3 -> {
                curShape = RECT
            }

        }
        return super.onOptionsItemSelected(item)
    }

    private class MyGraphicView(context: Context) : View(context) {
        val myshape: ArrayList<MyShape> = ArrayList()

        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.toFloat()
                    startY = event.y.toFloat()
                }
                MotionEvent.ACTION_MOVE-> {
                    stopX = event.x.toFloat()
                    stopY = event.y.toFloat()
                    this.invalidate()
                }
                MotionEvent.ACTION_UP ->{
                    startX = event.x.toFloat()
                    startY = event.y.toFloat()
                    myshape.add(MyShape(curShape,startX,startY,stopX,stopY, curColor))
                    this.invalidate()
                }
            }
            return true
        }

        override fun onDraw(canvas: Canvas?) {
            super.onDraw(canvas)
            val paint = Paint()

            paint.style = Paint.Style.STROKE
            paint.strokeWidth = 4f
            paint.isAntiAlias = true

          for(i in myshape.indices){
              paint.color = myshape[i].color

              when(myshape[i].shapeType){
                  LINE -> {
                      canvas!!.drawLine(myshape[i].startX.toFloat(), myshape[i].startY.toFloat(),
                          myshape[i].stopX.toFloat(), myshape[i].stopY.toFloat(), paint)
                  }
                  CIRCLE -> {
                      var radius = Math.sqrt(Math.pow((myshape[i].stopX - myshape[i].startX).toDouble() , 2.0) + Math.pow((myshape[i].stopY - myshape[i].startY).toDouble(), 2.0))
                      canvas!!.drawCircle(myshape[i].startX.toFloat(), myshape[i].startY.toFloat(), radius.toFloat(), paint)
                  }
                  RECT -> {
                      canvas!!.drawRect(RectF(myshape[i].startX.toFloat(), myshape[i].startY.toFloat(), myshape[i].stopX.toFloat(), myshape[i].stopY.toFloat()), paint)
                  }

              }
          }

        }

    }

}


------------------------------------------------------------------------------------
 *****    MyShape.kt  -->  app/java
------------------------------------------------------------------------------------
class MyShape (shapeType:Int, startX: Float, startY:Float, stopX:Float, stopY:Float, color:Int) {
    var shapeType:Int=0
    var startX:Float=0F
    var startY:Float=0F
    var stopX:Float=0F
    var stopY:Float=0F
    var color:Int=0


    init {
        this.shapeType = shapeType
        this.startX = startX
        this.startY = startY
        this.stopX = stopX
        this.stopY = stopY
        this.color = color
    }
}


------------------------------------------------------------------------------------
 ***  themes.xml
------------------------------------------------------------------------------------
sources xmlns:tools="http://schemas.android.com/tools">
    <!-- Base application theme. -->
    <style name="Base.Theme.SimpleDrawingBoard" parent="Theme.MaterialComponents.DayNight.DarkActionBar">
        <!-- Customize your light theme here. -->
        <item name="colorPrimary">#7CB342</item>
        <item name="colorPrimaryDark">#00897B</item>
        <item name="colorAccent">#FFC940</item>
        <item name="android:textColor">@color/black</item>
        <item name="android:textColorPrimary">#F4511E</item>
        <item name="android:textColorPrimaryInverse">#F4511E</item>
    </style>

    <style name="Theme.SimpleDrawingBoard" parent="Base.Theme.SimpleDrawingBoard" />
</resources>