Glide란 무엇인가?
본격적인 예제에 앞서 Glide가 무엇인지 간단히 살펴보겠습니다. Glide는 Bumptech에서 개발한 오픈소스 이미지 처리 라이브러리로, 이미지 로딩과 디코딩, 캐싱, 애니메이션 GIF 재생까지 다양한 기능을 지원합니다. 특히 메모리 관리와 스크롤 성능 최적화에 강점이 있어 RecyclerView나 ListView처럼 많은 이미지를 반복적으로 표시해야 하는 화면에서 널리 활용됩니다.
이 글에서는 안드로이드 프로젝트에 Glide를 통합하고, URL로 이미지를 불러와 화면에 표시하는 방법을 단계별로 알아보겠습니다.
1단계 — 새 프로젝트 생성
Android Studio를 실행한 뒤 File → New Project 메뉴로 이동하고, 새 프로젝트 생성에 필요한 정보를 모두 입력해 프로젝트를 만듭니다.
2단계 — build.gradle(Module:app) 설정
앱 수준의 build.gradle 파일에 아래 코드를 추가합니다. 여기서 핵심은 Glide 라이브러리와 컴파일러(annotationProcessor) 의존성입니다.
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'
implementation 'com.github.bumptech.glide:glide:4.8.0'
annotationProcessor 'com.github.bumptech.glide:compiler:4.8.0'
androidTestImplementation 'com.android.support.test:runner:1.0.2'
androidTestImplementation 'com.android.support.test.espresso:espresso-core:3.0.2'
}
3단계 — build.gradle(Project) 설정
프로젝트 수준의 build.gradle 파일에 아래 코드를 추가합니다.
// Top-level build file where you can add configuration options common to all sub-projects/modules.
buildscript {
repositories {
google()
jcenter()
}
dependencies {
classpath 'com.android.tools.build:gradle:3.2.1'
// NOTE: Do not place your application dependencies here; they belong
// in the individual module build.gradle files
}
}
allprojects {
repositories {
google()
jcenter()
}
}
task clean(type: Delete) {
delete rootProject.buildDir
}
4단계 — activity_main.xml 레이아웃 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가해 ImageView를 배치합니다.
<?xml version = "1.0" encoding = "utf-8"?>
<android.support.constraint.ConstraintLayout
xmlns:android = "https://schemas.android.com/apk/res/android" xmlns:tools = "https://schemas.android.com/tools" android:layout_width = "match_parent"
android:layout_height = "match_parent">
<LinearLayout
android:layout_width = "match_parent"
android:layout_height = "match_parent"
android:background = "#797979"
android:gravity = "center"
android:orientation = "vertical">
<ImageView
android:id = "@+id/imageView"
android:layout_width = "wrap_content"
android:layout_height = "wrap_content" />
</LinearLayout>
</android.support.constraint.ConstraintLayout>
5단계 — MainActivity.java 코드 작성
src/MainActivity.java 파일에 아래 코드를 추가합니다. Glide의 load() 메서드에 이미지 URL을 전달하고, into()로 대상 ImageView를 지정하면 끝입니다.
import android.os.Bundle;
import android.support.v7.app.AppCompatActivity;
import android.widget.ImageView;
import com.bumptech.glide.Glide;
public class MainActivity extends AppCompatActivity {
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ImageView imageView = findViewById(R.id.imageView);
Glide.with(this)
.load("https://www.tutorialspoint.com/images/tp-logo-diamond.png")
.into(imageView);
}
}
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 모바일 기기가 컴퓨터에 연결되어 있다고 가정합니다. Android Studio에서 앱을 실행하려면 프로젝트의 액티비티 파일 중 하나를 연 뒤, 툴바에서 Run
아이콘을 클릭합니다. 목록에서 자신의 모바일 기기를 선택하면, 기기 화면에 아래와 같이 이미지가 로드되어 표시되는 것을 확인할 수 있습니다.
