日々是好日~every day is a good day~

日常の中の非日常の備忘録

【Android Studio】手書きメモアプリ 2回目

今日は手書きメモアプリのMainActivity.ktの編集です

import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.view.SurfaceView
import android.widget.Button
import android.widget.ImageView

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

        val surfaceView: SurfaceView = findViewById(R.id.surfaceView)
        // CustomSurfaceViewのインスタンスを生成しonTouchリスナーをセット
        val customSurfaceView = CustomSurfaceView(this, surfaceView)
        surfaceView.setOnTouchListener { v, event ->
            customSurfaceView.onTouch(event)
        }

        val select : ImageView = findViewById(R.id.select)

        val btnBlack:Button=findViewById(R.id.btnBlack)
        btnBlack.stateListAnimator = null
        val btnRed:Button=findViewById(R.id.btnRed)
        btnRed.stateListAnimator = null
        val btnBlue:Button=findViewById(R.id.btnBlue)
        btnBlue.stateListAnimator = null
        val btnclear:Button=findViewById(R.id.btnclear)
        val btnsave:Button=findViewById(R.id.btnsave)
        val btnundo:Button = findViewById(R.id.btnundo)
        val btnredo:Button = findViewById(R.id.btnredo)

        // 選択印を表示
        select.bringToFront()
        select.setX(10f)
        select.setY(0f)

        // カラーボタン(CustomSurfaceViewのChangeColorへ)
        btnBlack.setOnClickListener {
            customSurfaceView.ChangeColor("black")
            select.setX(10f)
        }
        btnRed.setOnClickListener {
            customSurfaceView.ChangeColor("red")
            select.setX(130f)
        }
        btnBlue.setOnClickListener {
            customSurfaceView.ChangeColor("blue")
            select.setX(250f)
        }
        //clearボタン(CustomSurfaceViewのClearへ)
        btnclear.setOnClickListener {
            customSurfaceView.Clear()
        }
        // undoボタン(CustomSurfaceViewのUndoへ)
        btnundo.setOnClickListener {
            customSurfaceView.Undo()
        }
        // redoボタン(CustomSurfaceViewのRedoへ)
        btnredo.setOnClickListener {
            customSurfaceView.Redo()
        }
        // saveボタン(CustomSurfaceViewのSaveへ)
        btnsave.setOnClickListener {
            customSurfaceView.Save()
        }
    }
}

MainActivityではボタンタップや画面のタッチなどのUI部分を編集しました
画面への描画は別クラス(CustomSurfaceView)を作ってそこで行うことにします
明日はCustomSurfaceView.ktを編集していきます