Android Application

Kotlin 그래픽(graphic) : 터치 좌표

작성자 임베디드코리아 작성일23-10-16 23:59 조회1,567회 댓글0건

첨부파일

터치 좌표
-------------------------------------------------------------------------------------
      activity_main.xml
-------------------------------------------------------------------------------------
<?xml version="1.0" encoding="utf-8"?>
<androidx.constraintlayout.widget.ConstraintLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    tools:context=".MainActivity">

    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="화면을 터치해 주세요!"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintLeft_toLeftOf="parent"
        app:layout_constraintRight_toRightOf="parent"
        app:layout_constraintTop_toTopOf="parent" />

    <ek.example.touchcircle.PaintView
        android:id="@+id/paintView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
------------------------------------------------------------------------------------
class MainActivity : AppCompatActivity() {
    lateinit var binding : ActivityMainBinding

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

        binding = ActivityMainBinding.inflate(layoutInflater)
        val view = binding.root
        setContentView(view)

        binding.paintView.setOnTouchListener(binding.paintView)

    }
}

------------------------------------------------------------------------------------
*****  PaintView.kt    -->  app/java
------------------------------------------------------------------------------------
class PaintView: View, View.OnTouchListener{
    constructor(context : Context) : this(context, null)
    constructor(context : Context, attrs : AttributeSet?): this(context, attrs, 0)
    constructor(context : Context, attrs : AttributeSet?, defStyle: Int ): super(context, attrs, defStyle)

    // 터치 X좌표값을 저장할 변수
    private var coordsX: Float? = null

    var paint : Paint? = null
    var xAxis : Float? = null
    var yAxis : Float? = null

    init {
        paint = Paint()
        xAxis = -100F
        yAxis = -100F
    }

    override fun onTouch(v: View?, event: MotionEvent?): Boolean {
        when(event?.action){
            MotionEvent.ACTION_UP -> {
                xAxis = -100F
                yAxis = -100F
                invalidate()
            }
            MotionEvent.ACTION_DOWN -> {
                xAxis = event.x
                yAxis = event.y
                invalidate()
            }
        }
        return true
    }

    override fun onDraw(canvas: Canvas?) {
        super.onDraw(canvas)

        paint?.style = Paint.Style.STROKE
        paint?.strokeWidth = 5F
        paint?.color = Color.BLUE
        canvas?.drawCircle(xAxis!!, yAxis!!, 50F, paint!!)

        // coordX 변수에 저장된 값을 텍스트로 그린다.
        var STRxAxis:String=xAxis.toString()
        var STRyAxis:String=yAxis.toString()
        paint?.textSize = 50f
        canvas?.drawText("X=$STRxAxis    Y=$STRyAxis", 100F, 100F, paint!!)
    }
}

------------------------------------------------------------------------------------
  build.gradle(Module) : build.gradle.kts
-------------------------------------------------------------------------------------
// 안드로이드 스튜디오 4.0 이상
android {
    ...
    buildFeatures {
        viewBinding = true
    }
}