Android Application

Kotlin 그래픽(graphic) : 갠버스(Canvas)와 페인트(Paint)

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

첨부파일

갠버스(Canvas)와 페인트(Paint)
-------------------------------------------------------------------------------------
      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.canvas.CustomView
        android:id="@+id/paintView"
        android:layout_width="match_parent"
        android:layout_height="match_parent"/>

</androidx.constraintlayout.widget.ConstraintLayout>

------------------------------------------------------------------------------------
    MainActivity.kt
-------------------------------------------------------------------------------------
class MainActivity: AppCompatActivity() {

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

        setContentView(CustomView(this))

    }

}

------------------------------------------------------------------------------------
******    CustomView.kt    * app/java
------------------------------------------------------------------------------------
class CustomView: View {
    constructor(context: Context?) : super(context)
    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

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

        val paint = Paint()

        paint.color =Color.RED
        paint.setStrokeWidth(30f)
        canvas?.drawPoint(300f, 100f,  paint)

        paint.color =Color.BLUE
        paint.setStrokeWidth(10f)
        paint.style = Paint.Style.FILL
        canvas?.drawLine(50F,100F,360F,640F, paint)

        paint.color =Color.DKGRAY
        paint.style = Paint.Style.FILL
        canvas?.drawRect(500F, 100F, 1000F, 600F, paint)

        paint.color =Color.GREEN
        paint.style = Paint.Style.FILL
        canvas?.drawCircle(600f, 1000f, 300f, paint)

        paint.color =Color.LTGRAY
        paint.style = Paint.Style.FILL
        canvas?.drawRoundRect(RectF(200F,1500F, 1000F,1800F), 50F, 50F, paint)

        paint.color =Color.YELLOW
        paint.style = Paint.Style.FILL
        canvas?.drawOval(RectF(200F,1900F, 1000F,2200F), paint)


        paint.color =Color.CYAN
        canvas?.drawArc(RectF(200F,2220F, 400F,2400F), 270F, 290F, false, paint)
        canvas?.drawArc(RectF(200F,2420F, 1000F,2600F), 0F, 290F, true, paint)

        // 텍스트 속성 설정
        paint.color = Color.BLUE
        paint.textSize = 100f

        // 텍스트 그리기
        // 매개변수: "텍스트", X좌표, Y좌표, Paint객체
        canvas?.drawText("커스텀뷰", 400f, 1400f, paint)

    }
}