이 가이드에서는 안드로이드 앱에서 CircularImageView(원형 이미지뷰)를 만드는 방법을 단계별로 살펴봅니다.
1단계 — 새 프로젝트 생성
Android Studio에서 File → New Project 메뉴로 이동한 후, 프로젝트 생성에 필요한 모든 정보를 입력하여 새 프로젝트를 만듭니다.
2단계 — Gradle 파일에 CircularImageView 라이브러리 추가
원형 이미지뷰를 구현하려면 외부 라이브러리가 필요합니다. 아래와 같이 build.gradle 파일의 dependencies 항목에 CircularImageView 라이브러리를 추가합니다.
apply plugin: 'com.android.application'
android {
compileSdkVersion 28
defaultConfig {
applicationId "com.example.andy.myapplication"
minSdkVersion 15
targetSdkVersion 28
versionCode 1
versionName "1.0"
testInstrumentationRunner "android.support.test.runner.AndroidJUnitRunner"
}
buildTypes {
release {
minifyEnabled false
proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
}
}
}
dependencies {
implementation fileTree(dir: 'libs', include: ['*.jar'])
implementation 'com.android.support:appcompat-v7:28.0.0'
implementation 'com.android.support.constraint:constraint-layout:1.1.3'
testImplementation 'junit:junit:4.12'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
implementation 'com.mikhaellopez:circularimageview:3.2.0'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}3단계 — MainActivity.java 작성
다음은 수정된 메인 액티비티 파일(MainActivity.java)의 내용입니다. 이 파일에는 액티비티의 기본 생명주기(life cycle) 메서드가 포함될 수 있으며, 실제 CircularImageView는 레이아웃 파일(activity_main.xml)에서 선언합니다.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
}
}4단계 — activity_main.xml에 CircularImageView 추가
다음은 res/layout/activity_main.xml 파일의 내용으로, 여기에 CircularImageView를 포함시킵니다.
<?xml version = "1.0" encoding = "utf-8"?> <android.support.constraint.ConstraintLayout xmlns:android = "https://schemas.android.com/apk/res/android" xmlns:app = "https://schemas.android.com/apk/res-auto" xmlns:tools = "https://schemas.android.com/tools" android:layout_width = "match_parent" android:layout_height = "match_parent" tools:context = ".MainActivity"> <LinearLayout android:layout_width = "match_parent" android:gravity = "center_horizontal" android:orientation = "vertical" android:layout_height = "match_parent"> <com.mikhaellopez.circularimageview.CircularImageView android:layout_width = "250dp" android:layout_height = "250dp" android:src = "@drawable/image" app:civ_border_color = "#EEEEEE" app:civ_border_width = "4dp" app:civ_shadow = "true" app:civ_shadow_radius = "10" app:civ_shadow_color = "#8BC34A"/> <TextView android:text = "Mohammad Mohtashim" android:layout_width = "match_parent" android:layout_height = "match_parent" android:layout_marginTop = "20dp" android:textSize = "20sp" android:textAlignment = "center"/> </LinearLayout> </android.support.constraint.ConstraintLayout>
위 레이아웃에서는 CircularImageView를 com.mikhaellopez.circularimageview.CircularImageView로 선언하고, drawable 폴더에 있는 image 리소스를 이미지로 지정했습니다.
애플리케이션 실행하기
이제 앱을 실행해 보겠습니다. 실제 안드로이드 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일 중 하나를 열고 툴바의 Run 아이콘을 클릭하세요. 실행 옵션 목록에서 자신의 모바일 기기를 선택하면, 기기 화면에 아래와 같은 결과가 표시됩니다.

CircularImageView 주요 속성 정리
app:civ_border — 테두리(border) 생성 여부를 설정하는 속성입니다. 기본값은 true입니다.
app:civ_border_color — 테두리 색상을 지정하는 속성입니다. 기본 색상은 흰색입니다.
app:civ_border_width — 테두리 두께를 지정하는 속성입니다. 기본값은 4dp입니다.
app:civ_background_color — 배경색을 지정하는 속성입니다. 기본 배경색은 흰색입니다.
app:civ_shadow — 이미지에 그림자 효과를 적용할지 결정하는 속성입니다. 기본값은 false이며, 그림자가 필요하면 true로 설정해야 합니다.
app:civ_shadow_color — 그림자 색상을 지정하는 속성입니다. 기본값은 검정입니다.
app:civ_shadow_radius — 그림자 반경을 지정하는 속성입니다. 기본값은 8.0f입니다.
app:civ_shadow_gravity — 그림자의 위치(gravity)를 지정하는 속성입니다. 기본값은 bottom(아래)입니다.