본문 바로가기
안드로이드 자바

[Android][Java] DrawingAppLibrary 사용해서 그림판 만들기

by teamnova 2022. 12. 31.
728x90

안녕하세요 오늘은 DrawingAppLibray를 사용해서 그림판을 빠르게 만들어보도록하겠습니다.

https://github.com/ajithvgiri/canvas.git

 

GitHub - ajithvgiri/canvas: Canvas Drawing Android Library

Canvas Drawing Android Library. Contribute to ajithvgiri/canvas development by creating an account on GitHub.

github.com

 

1. 라이브러리 등록

	allprojects {
		repositories {
			...
			maven { url 'https://jitpack.io' }
		}
	}
    
    	dependencies {
	        compile 'com.github.ajithvgiri:Canvas-Library:v1.1.1'
	}

2. 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">

    <androidx.constraintlayout.widget.ConstraintLayout
        android:id="@+id/parentView"
        android:layout_width="match_parent"
        android:layout_height="0dp"
        app:layout_constraintBottom_toTopOf="@+id/btn_clear"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent"
        app:layout_constraintTop_toTopOf="parent">

    </androidx.constraintlayout.widget.ConstraintLayout>

    <Button
        android:id="@+id/btn_clear"
        android:text="캔버스 비우기"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        app:layout_constraintBottom_toBottomOf="parent"
        app:layout_constraintEnd_toEndOf="parent"
        app:layout_constraintStart_toStartOf="parent" />

</androidx.constraintlayout.widget.ConstraintLayout>

3. Java 동작 코드 작성

public class MainActivity extends AppCompatActivity {

    private ActivityMainBinding binding;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityMainBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        CanvasView canvasView = new CanvasView(MainActivity.this);
        binding.parentView.addView(canvasView);

        binding.btnClear.setOnClickListener(view -> {
            canvasView.clearCanvas();
        });
    }
}