이번 예제에서는 안드로이드(Android)에서 두 개의 이미지를 서로 겹쳐(오버레이) 하나의 ImageView에 표시하는 방법을 단계별로 알아보겠습니다.
1단계 — 새 프로젝트 만들기
Android Studio에서 File → New Project 메뉴로 이동한 뒤, 필요한 정보를 모두 입력하여 새 프로젝트를 생성합니다.
2단계 — 레이아웃 파일(activity_main.xml) 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다.
<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout 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"
android:padding="16dp"
tools:context=".MainActivity">
<Button
android:id="@+id/btnOverlay"
android:onClick="OverLayImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:layout_marginTop="10dp"
android:text="Overlay Image"/>
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent"
android:layout_below="@+id/btnOverlay"/>
</RelativeLayout>
3단계 — 이미지 준비 및 layer.xml 생성
합성하고 싶은 두 개의 이미지를 res/drawable 폴더에 복사해서 넣습니다. 그다음 drawable 리소스 파일(layer.xml)을 새로 생성하고 아래 코드를 추가합니다. <layer-list>는 하위 item 순서대로 이미지를 쌓아 올리므로, 나중에 선언된 이미지가 위에 표시됩니다.
<?xml version="1.0" encoding="utf-8"?>
<layer-list xmlns:android="https://schemas.android.com/apk/res/android">
<item android:drawable="@drawable/image" />
<item android:drawable="@drawable/image1" />
</layer-list>
4단계 — MainActivity.java 작성
src/MainActivity.java 파일에 아래 코드를 추가합니다. 버튼을 클릭하면 OverLayImage() 메서드가 호출되어 layer-list 드로어블을 ImageView에 적용합니다.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
}
public void OverLayImage(View view) {
imageView.setImageDrawable(getResources().getDrawable(R.drawable.layer));
}
}
참고: getResources().getDrawable()은 최신 API에서 지원 중단(deprecated)되었습니다. API 21 이상을 대상으로 한다면 ContextCompat.getDrawable(this, R.drawable.layer) 사용을 권장합니다.
5단계 — AndroidManifest.xml 확인
androidManifest.xml에는 아래와 같이 MainActivity가 등록되어 있어야 합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android" package="app.com.sample">
<application
android:allowBackup="true"
android:icon="@mipmap/ic_launcher"
android:label="@string/app_name"
android:roundIcon="@mipmap/ic_launcher_round"
android:supportsRtl="true"
android:theme="@style/AppTheme">
<activity android:name=".MainActivity">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>
앱 실행 및 결과 확인
이제 애플리케이션을 실행해 보겠습니다. 실제 안드로이드 기기를 컴퓨터에 연결한 상태라고 가정합니다. Android Studio에서 프로젝트의 액티비티 파일을 하나 연 뒤 툴바의 Run 아이콘을 클릭하고, 목록에서 자신의 모바일 기기를 선택하세요. 그러면 앱이 실행되어 기본 화면이 표시됩니다.
버튼을 클릭하면 두 이미지가 겹쳐진 결과가 ImageView에 나타나는 것을 확인할 수 있습니다.

