이 튜토리얼에서는 Android 앱에서 이미지 크기를 조정(리사이즈)하는 방법을 단계별로 살펴봅니다. 갤러리에서 이미지를 불러온 뒤, Bitmap.createScaledBitmap() 메서드를 사용해 원하는 크기로 손쉽게 변경할 수 있습니다.
핵심 개념
Android에서 이미지 크기 조정은 Bitmap 클래스의 createScaledBitmap() 정적 메서드를 통해 수행됩니다. 이 메서드는 원본 비트맵과 목표 가로·세로 크기, 그리고 필터링 여부를 인자로 받아 새로운 크기의 비트맵 객체를 반환합니다.
1단계 – 새 프로젝트 생성
Android Studio에서 File ⇒ New Project로 이동한 후, 프로젝트 생성에 필요한 모든 세부 정보를 입력하여 새 프로젝트를 만듭니다.
2단계 – 레이아웃 작성 (res/layout/activity_main.xml)
이미지를 표시할 ImageView와 두 개의 버튼(Upload Image, Resize Image)을 포함하는 레이아웃을 아래 코드로 작성합니다.
<?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:gravity="center"
tools:context=".MainActivity">
<ImageView
android:id="@+id/ivImage"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_centerHorizontal="true"
android:contentDescription="@string/app_name"
android:src="@drawable/ic_launcher_foreground" />
<LinearLayout
android:layout_width="match_parent"
android:layout_height="wrap_content"
android:layout_below="@+id/ivImage"
android:layout_centerHorizontal="true">
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:onClick="uploadImage"
android:text="Upload Image" />
<Button
android:layout_width="0dp"
android:layout_height="wrap_content"
android:layout_marginTop="8dp"
android:layout_weight="1"
android:onClick="resizeImage"
android:text="Resize Image" />
</LinearLayout>
</RelativeLayout>3단계 – MainActivity.java 코드 작성
버튼 클릭 시 갤러리에서 이미지를 선택하고, 선택된 이미지를 화면에 표시한 후 리사이즈 버튼을 누르면 400×400 크기로 조정되도록 구현합니다.
package app.tutorialspoint.com.sample;
import android.app.Activity;
import android.content.Intent;
import android.graphics.Bitmap;
import android.net.Uri;
import android.provider.MediaStore;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.ImageView;
import java.io.IOException;
public class MainActivity extends AppCompatActivity {
ImageView ivImage;
public static final int PICK_IMAGE = 1;
Bitmap yourBitmap;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
ivImage = findViewById(R.id.ivImage);
}
// 갤러리에서 이미지 업로드
public void uploadImage(View view) {
Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
intent.setType("image/*");
startActivityForResult(intent, PICK_IMAGE);
}
// 이미지 크기를 400x400으로 조정
public void resizeImage(View view) {
Bitmap resized = Bitmap.createScaledBitmap(yourBitmap, 400, 400, true);
ivImage.setImageBitmap(resized);
}
@Override
public void onActivityResult(int requestCode, int resultCode, Intent data) {
super.onActivityResult(requestCode, resultCode, data);
if (requestCode == PICK_IMAGE && resultCode == Activity.RESULT_OK) {
if (data == null) {
// 오류 표시
return;
}
try {
Uri imageUri = data.getData();
yourBitmap = MediaStore.Images.Media.getBitmap(
this.getContentResolver(), imageUri);
ivImage.setImageBitmap(yourBitmap);
} catch (IOException e) {
e.printStackTrace();
}
}
}
}4단계 – AndroidManifest.xml 설정
매니페스트 파일에 아래와 같이 메인 액티비티를 등록합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.tutorialspoint.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>실행 결과 및 동작 방식
앱을 실행하면 다음과 같이 동작합니다.
① Upload Image 버튼 클릭 → 기기의 갤러리가 열리고, 사용자가 선택한 이미지가 ImageView에 그대로 표시됩니다.
② Resize Image 버튼 클릭 → Bitmap.createScaledBitmap(yourBitmap, 400, 400, true) 호출을 통해 이미지가 400×400 픽셀로 축소되어 화면에 다시 나타납니다.
마지막 인자인 true(filter 옵션)는 양선형 보간(bilinear interpolation)을 활성화하여 축소 시 이미지가 더 부드럽고 자연스럽게 보이도록 합니다. 실제 프로젝트에서는 원본 비트맵이 매우 클 경우 OutOfMemoryError가 발생할 수 있으므로, BitmapFactory.Options의 inSampleSize를 활용한 다운샘플링을 함께 고려하는 것이 좋습니다.