미니 그림판 만들기

이아름·2022년 10월 6일
0

프로젝트

목록 보기
1/4
post-thumbnail


터치한 위치를 저장할 data class

그림판을 담당하는 CustomView

class Drawing : View {
    var list = arrayListOf<Point>()
    var colorList = arrayListOf(Color.RED, Color.BLACK, Color.BLUE)
    var eraseFlag = false
    var colorIndex = 1
    var figureIndex = 0

    constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs) {}

    override fun onDraw(canvas: Canvas) { //그림 그려줌
        super.onDraw(canvas)
        val paint = Paint()
        lateinit var firstPoint: Point
        //start 지점, end 지점 찾기
        var startFlag = true
        list.forEachIndexed { index, point ->
            if (point.erase) {
                paint.color = Color.WHITE
                paint.strokeWidth = 50F //두께
                point.figure = 0
            } else {
                paint.color = colorList[point.color]
                paint.strokeWidth = 10F //두께
            }
            if (point.figure == 0) {
                if (index > 1 && point.isContinue) {
                    canvas.drawLine(list[index-1].x, list[index-1].y, point.x, point.y, paint)
                }
            } else {
                if (!point.isContinue && startFlag) {
                    Log.v(TAG, "setting : ${point.x},${point.y}")
                    firstPoint = point
                    startFlag = false
                } else if (!point.isContinue) {
                    Log.v(TAG, "print : ${point.x},${point.y}")
                    if (point.figure == 1) {//원형
                        canvas.drawOval(firstPoint.x, firstPoint.y, point.x, point.y, paint)
                    } else {//사각형
                        canvas.drawRect(firstPoint.x, firstPoint.y, point.x, point.y, paint)
                    }
                    startFlag = true
                }
            }
        }
    }

    @SuppressLint("ClickableViewAccessibility")
    override fun onTouchEvent(event: MotionEvent): Boolean {
        when (event.action) {
            MotionEvent.ACTION_DOWN -> {
                Log.v(TAG, "down : ${figureIndex}")
                list.add(Point(event.x, event.y, false, eraseFlag, colorIndex, figureIndex))
            }
            MotionEvent.ACTION_MOVE -> {
                Log.v(TAG, "move ${event.x},${event.y}")
                list.add(Point(event.x, event.y, true, eraseFlag, colorIndex, figureIndex))
            }
            MotionEvent.ACTION_UP -> {
                if (figureIndex > 0) {
                    Log.v(TAG, "up & figureIndex : ${figureIndex}")
                    list.add(Point(event.x, event.y, false, eraseFlag, colorIndex, figureIndex))
                } else {
                    Log.v(TAG, "up & figureIndex = 0")
                    list.add(Point(event.x, event.y, true, eraseFlag, colorIndex, figureIndex))
                }
            }
        }
        invalidate() //그림을 그려줘라 ->onDraw를 호출

        //touch이후에 event를 전달할것인가? true: 여기서 종료. false :뒤로 전달.
        // touch -> click -> longclick
        return true
    }

}

MainActivity

class MainActivity : AppCompatActivity() {
    lateinit var binding: ActivityMainBinding
    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        binding = ActivityMainBinding.inflate(layoutInflater)
        setContentView(binding.root)

        initialize()
    }

    private fun initialize(){
        binding.imgDraw.setOnClickListener{
            binding.canvas.eraseFlag = false
            binding.layoutFigure.visibility = View.GONE
            binding.layoutColor.visibility = View.GONE
        }
        initErase()
        initColor()
        initFigure()
    }
    fun initErase(){
        binding.imgErase.setOnClickListener{
            binding.canvas.eraseFlag = true
            binding.layoutFigure.visibility = View.GONE
            binding.layoutColor.visibility = View.GONE
        }
        binding.imgErase.setOnLongClickListener{
            binding.canvas.list.clear()
            binding.canvas.invalidate()
            false
        }
    }

    fun initColor(){
        binding.imgColor.setOnClickListener{
            binding.canvas.eraseFlag = false
            binding.layoutFigure.visibility = View.GONE
            binding.layoutColor.visibility = View.VISIBLE
        }
        binding.colorRed.setOnClickListener{
            binding.canvas.colorIndex = 0
            binding.layoutColor.visibility = View.GONE
        }
        binding.colorBlack.setOnClickListener{
            binding.canvas.colorIndex = 1
            binding.layoutColor.visibility = View.GONE
        }
        binding.colorBlue.setOnClickListener{
            binding.canvas.colorIndex = 2
            binding.layoutColor.visibility = View.GONE
        }
    }

    fun initFigure(){
        binding.imgFigure.setOnClickListener{
            binding.canvas.eraseFlag = false
            binding.layoutColor.visibility = View.GONE
            binding.layoutFigure.visibility = View.VISIBLE
        }

        binding.imgLine.setOnClickListener{
            binding.canvas.figureIndex = 0
            binding.layoutFigure.visibility = View.GONE
        }
        binding.imgCircle.setOnClickListener{
            binding.canvas.figureIndex = 1
            binding.layoutFigure.visibility = View.GONE
        }
        binding.imgSquare.setOnClickListener{
            binding.canvas.figureIndex = 2
            binding.layoutFigure.visibility = View.GONE
        }
    }
}

activity_main.xml

profile
반갑습니다

0개의 댓글