안드로이드 앱을 개발하다 보면 인터넷에서 이미지를 불러와 화면에 맞게 크기를 조절해야 하는 경우가 자주 있습니다. 이때 Picasso는 Square에서 개발한 대표적인 이미지 로딩 라이브러리로, URL 기반 이미지 로딩과 리사이징을 아주 간단한 코드 한 줄로 처리할 수 있어 많이 사용됩니다.
이 글에서는 Picasso의 resize() 메서드를 활용해 이미지 크기를 조절하는 방법을 단계별로 살펴보겠습니다.
1단계: 새 프로젝트 생성
Android Studio를 실행하고 File → New Project 메뉴로 이동한 뒤, 필요한 정보를 모두 입력하여 새 프로젝트를 생성합니다.
2단계: Picasso 의존성 추가
build.gradle (Module: app) 파일을 열고 다음 의존성을 추가한 후 프로젝트를 동기화(Sync)합니다.
implementation 'com.squareup.picasso:picasso:2.5.2'
3단계: 레이아웃 파일 작성
res/layout/activity_main.xml에 아래와 같이 ImageView가 포함된 레이아웃을 작성합니다.
<?xml version="1.0" encoding="utf-8"?>
<LinearLayout 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:orientation="vertical"
android:padding="8dp"
tools:context=".MainActivity">
<ImageView
android:id="@+id/imageView"
android:layout_width="match_parent"
android:layout_height="match_parent" />
</LinearLayout>4단계: MainActivity 작성
src/MainActivity.java에 다음 코드를 추가합니다. 핵심은 Picasso.with(this).load(url).resize(200, 500).into(imageView) 부분으로, 이미지를 로드하면서 가로 200dp, 세로 500dp 크기로 조절하여 ImageView에 표시합니다.
import androidx.appcompat.app.AppCompatActivity;
import android.os.Bundle;
import android.widget.ImageView;
import com.squareup.picasso.Picasso;
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);
String url = "https://images.pexels.com/photos/414612/pexels-photo-414612.jpeg";
Picasso.with(this)
.load(url)
.resize(200, 500)
.into(imageView);
}
}참고: 유용한 Picasso 옵션
.centerCrop(): 지정한 크기에 맞게 이미지를 잘라내어 비율을 유지합니다..fit(): ImageView의 실제 크기에 맞춰 자동으로 조절합니다..placeholder(R.drawable.loading): 이미지가 로딩되는 동안 표시할 임시 이미지를 설정합니다.
5단계: 매니페스트에 인터넷 권한 추가
네트워크에서 이미지를 불러오므로 AndroidManifest.xml에 인터넷 권한을 반드시 선언해야 합니다.
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="https://schemas.android.com/apk/res/android"
package="app.com.sample">
<uses-permission android:name="android.permission.INTERNET"/>
<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(실행) 버튼을 클릭합니다. 실행할 기기 목록에서 연결된 스마트폰을 선택하면, 앱이 실행되며 지정한 크기(200×500)로 조절된 이미지가 화면에 표시됩니다.

이처럼 Picasso를 활용하면 복잡한 비트맵 처리 코드 없이도 이미지 로딩과 리사이징을 손쉽게 구현할 수 있습니다. 필요에 따라 centerCrop(), fit() 등의 옵션을 조합하면 더욱 다양한 방식으로 이미지를 제어할 수 있습니다.