티스토리 뷰
이전 포스팅에서 알아봤던 Glide 는 ImageView 에 이미지를 로딩하기 참 쉽고, 예제도 많이 있다.
하지만 의외로 Glide 를 이용해 layout (RelativeLayout, LinearLayout ......) 에 background image 를 어떻게 설정하는지 질문을 많이 한다.
이번 포스팅에서는 Glide 를 이용하여 background image 를 설정하는 방법에 대해 알아보자.
[Android] - 유용한 이미지 라이브러리 - Glide (1)
[Android] - 유용한 이미지 라이브러리 - Glide (2)
Precondition
하단은 이번 포스팅에서 사용할 background image layout 과 image file 이다.
◼ Image
◼ Layout
<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:id="@+id/root" | |
android:layout_width="250.00dp" | |
android:layout_height="250.00dp" | |
android:layout_gravity="center" | |
android:background="#00FF00" | |
tools:context=".PhMainActivity"> | |
<ImageView | |
android:id="@+id/background" | |
android:layout_width="match_parent" | |
android:layout_height="match_parent" | |
app:layout_constraintStart_toStartOf="parent" | |
app:layout_constraintTop_toTopOf="parent" /> | |
</androidx.constraintlayout.widget.ConstraintLayout> |

CustomTarget 을 이용한 background image 설정
가장 간단한 방법으로 Glide 에서 제공하는 target 을 이용하는 방법이다.
Glide 에서 target 을 이용하면 bitmap, drawable, placeholder 를 받아 처리할 수 있다.
◼ Custom taget 을 이용한 drawable 을 받아서 처리하기
[Java]
Glide | |
.with(this) | |
.load(R.drawable.sample) | |
.into(new CustomTarget<Drawable>() { | |
@Override | |
public void onResourceReady(Drawable a_resource, Transition<? super Drawable> a_transition) { | |
final ConstraintLayout layout = findViewById(R.id.root); | |
layout.setBackground(a_resource); | |
} | |
@Override | |
public void onLoadCleared(@Nullable Drawable placeholder) { | |
} | |
}); |
[Kotlin]
Glide | |
.with(this) | |
.load(R.drawable.sample) | |
.into(object : CustomTarget<Drawable>() { | |
override fun onResourceReady(a_resource: Drawable, a_transition: Transition<in Drawable>?) { | |
val layout = findViewById<ConstraintLayout>(R.id.root) | |
layout.background = a_resource | |
} | |
override fun onLoadCleared(placeholder: Drawable?) {} | |
}) |
◼ Custom taget 을 이용한 bitmap 을 받아서 처리하기
[Java]
Glide | |
.with(this) | |
.asBitmap() // Bitmap 으로 변환 | |
.load(R.drawable.sample) | |
.into(new CustomTarget<Bitmap>() { | |
@Override | |
public void onResourceReady(@NonNull Bitmap resource, @Nullable Transition<? super Bitmap> transition) { | |
final ConstraintLayout layout = findViewById(R.id.root); | |
layout.setBackground(new BitmapDrawable(getResources(), resource)); | |
} | |
@Override | |
public void onLoadCleared(@Nullable Drawable placeholder) { | |
} | |
}); |
[Kotlin]
Glide | |
.with(this) | |
.asBitmap() // Bitmap 으로 변환 | |
.load(R.drawable.sample) | |
.into(object : CustomTarget<Bitmap>() { | |
override fun onResourceReady(a_resource: Bitmap, transition: Transition<in Bitmap>?) { | |
val layout = findViewById<ConstraintLayout>(R.id.root) | |
layout.background = BitmapDrawable(resources, a_resource) | |
} | |
override fun onLoadCleared(placeholder: Drawable?) {} | |
}) |
하지만 target 을 이용한 경우 하단과 같이 그림이 훼손될 가능성이 크다.
즉, background image 가 정해져 있고 layout 크기와 일치 할 경우만 쓸 수 있다.
(하단의 그림을 보고 이미지 훼손을 못 느낀다면 당신은 개발자가 천직이다!!)

Layout 크기의 ImageView 로 background image 설정하기
위에서 설명한 것 처럼 Glide 에서 제공하는 target 을 이용하면 간단히 처리할 수 있지만, image 의 scale 을 조절 할 수 없는 단점이 있다.
이런 경우 ImageView 를 layout 크기로 설정하여 background image 로 쓸 수 있다.
◼ ImageView 에 background image 설정하기
[Java]
final ImageView ivImage = findViewById(R.id.background); | |
Glide | |
.with(this) | |
.load(R.drawable.sample) | |
.into(ivImage); |
[Kotlin]
val ivImage = findViewById<ImageView>(R.id.background) | |
Glide | |
.with(this) | |
.load(R.drawable.sample) | |
.into(ivImage) |

◼ ImageView 에 center crop 을 적용하여 background image 설정하기
[Java]
final ImageView ivImage = findViewById(R.id.background); | |
Glide | |
.with(this) | |
.load(R.drawable.sample) | |
.centerCrop() | |
.into(ivImage); |
[Kotlin]
val ivImage = findViewById<ImageView>(R.id.background) | |
Glide | |
.with(this) | |
.load(R.drawable.sample) | |
.centerCrop() | |
.into(ivImage) |

'Android' 카테고리의 다른 글
유용한 에니메이션 라이브러리 - Lottie (0) | 2021.08.31 |
---|---|
requireContext, requireActivity (0) | 2021.08.26 |
Activity Result API (0) | 2021.06.04 |
유용한 이미지 라이브러리 - Glide (2) (0) | 2021.05.13 |
유용한 이미지 라이브러리 - Glide (1) (0) | 2021.05.13 |
- Total
- Today
- Yesterday
- 앱테크 추천
- android flag activity
- android intent
- android activity flag
- notifyDataSetChanged
- 앱테크
- android task
- Intent
- task
- 무료 채굴
- onCreateContextMenu
- bindservice
- mPANDO
- WEMIX
- StringUtils
- 안드로이드 서비스
- 무료채굴
- 리워드앱
- 채굴앱
- M2E
- 리워드 어플
- RoomDatabase
- registerForContextMenu
- Android Service
- p2e
- BroadcastReceiver
- onContextItemSelected
- 안드로이드 인텐트
- StartService
- task 생성
일 | 월 | 화 | 수 | 목 | 금 | 토 |
---|---|---|---|---|---|---|
1 | 2 | 3 | 4 | 5 | ||
6 | 7 | 8 | 9 | 10 | 11 | 12 |
13 | 14 | 15 | 16 | 17 | 18 | 19 |
20 | 21 | 22 | 23 | 24 | 25 | 26 |
27 | 28 | 29 | 30 |