728x90
1. AndroidDraw 라이브러리 추가
app/build.gradle
dependencies {
implementation 'androidx.appcompat:appcompat:1.6.1'
implementation 'com.google.android.material:material:1.5.0'
implementation 'androidx.constraintlayout:constraintlayout:2.1.4'
testImplementation 'junit:junit:4.13.2'
androidTestImplementation 'androidx.test.ext:junit:1.1.5'
androidTestImplementation 'androidx.test.espresso:espresso-core:3.5.1'
implementation 'com.github.divyanshub024:AndroidDraw:v0.1' // 추가
}
settings.gradle
maven { url 'https://jitpack.io' } 추가
dependencyResolutionManagement {
repositoriesMode.set(RepositoriesMode.FAIL_ON_PROJECT_REPOS)
repositories {
google()
mavenCentral()
maven { url 'https://jitpack.io' } //cranberryai
}
}
gradle.properties :
android.enableJetifier=true 추가
# Project-wide Gradle settings.
# IDE (e.g. Android Studio) users:
# Gradle settings configured through the IDE *will override*
# any settings specified in this file.
# For more details on how to configure your build environment visit
# http://www.gradle.org/docs/current/userguide/build_environment.html
# Specifies the JVM arguments used for the daemon process.
# The setting is particularly useful for tweaking memory settings.
org.gradle.jvmargs=-Xmx2048m -Dfile.encoding=UTF-8
# When configured, Gradle will run in incubating parallel mode.
# This option should only be used with decoupled projects. More details, visit
# http://www.gradle.org/docs/current/userguide/multi_project_builds.html#sec:decoupled_projects
# org.gradle.parallel=true
# AndroidX package structure to make it clearer which packages are bundled with the
# Android operating system, and which are packaged with your app's APK
# https://developer.android.com/topic/libraries/support-library/androidx-rn
android.useAndroidX=true
# Enables namespacing of each library's R class so that its R class includes only the
# resources declared in the library itself and none from the library's dependencies,
# thereby reducing the size of the R class for that library
android.nonTransitiveRClass=true
android.enableJetifier=true // 추가
2. 레이아웃 디자인
<?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">
<com.divyanshu.draw.widget.DrawView
android:id="@+id/drawView"
android:layout_width="300dp"
android:layout_height="300dp"
android:layout_marginTop="10dp"
app:layout_constraintDimensionRatio="1:1"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.497"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toTopOf="parent" />
<Button
android:id="@+id/button"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_marginTop="10dp"
android:text="Button"
app:layout_constraintEnd_toEndOf="parent"
app:layout_constraintHorizontal_bias="0.498"
app:layout_constraintStart_toStartOf="parent"
app:layout_constraintTop_toBottomOf="@+id/drawView" />
</androidx.constraintlayout.widget.ConstraintLayout>
3. MainActivity.java
public class MainActivity extends AppCompatActivity {
DrawView drawView;
Button button;
String TAG = "MainActivity";
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
drawView = findViewById(R.id.drawView);
drawView.setStrokeWidth(70.0f); // 그림 그릴 때 선의 굵기 설정.
drawView.setColor(Color.WHITE); // 그림 그릴 때 선의 색상을 흰색으로 설정.
drawView.setBackgroundColor(Color.BLACK);// 배경색을 검은색으로 설정.
button = findViewById(R.id.button);
drawView.setOnTouchListener(new View.OnTouchListener() {
@Override
public boolean onTouch(View v, MotionEvent event) {
drawView.onTouchEvent(event); // // drawView에 터치 이벤트 전달.
return true;
}
});
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
drawView.clearCanvas(); // 버튼 클릭 시 초기화.
}
});
}
}
4. 결과
'안드로이드 자바' 카테고리의 다른 글
[Android][Java] 사운드 재생 (2) | 2024.02.08 |
---|---|
[Android][Java] 로고 애니메이션 넣어주기 (0) | 2024.02.02 |
[Android][Java] Camera2 API를 사용하여 사진찍기 (2) | 2024.01.28 |
[Android][Java] 카메라X 프리뷰 보여주기 (0) | 2024.01.21 |
[Android][Java] Camera2 API를 사용하여 사진찍기 (0) | 2024.01.19 |