이 글에서는 안드로이드 앱 내부에서 코드만으로 화면을 캡처하는 방법, 즉 프로그래밍 방식으로 스크린샷을 찍는 과정을 단계별로 살펴봅니다. 뷰(View)의 드로잉 캐시(Drawing Cache)를 활용하는 간단한 예제를 통해 누구나 쉽게 따라 할 수 있도록 정리했습니다.
1단계 — 새 프로젝트 생성
Android Studio에서 File → New Project를 선택해 새 프로젝트를 만들고, 프로젝트 생성에 필요한 모든 정보를 입력합니다.
2단계 — 레이아웃 작성 (res/layout/activity_main.xml)
아래 코드를 res/layout/activity_main.xml 파일에 추가합니다. 스크린샷을 실행할 버튼과, 캡처된 이미지를 화면에 보여줄 ImageView가 함께 배치되어 있습니다.
<?xml version="1.0" encoding="utf-8"?> <RelativeLayout xmlns:android="https://schemas.android.com/apk/res/android" style="?attr/actionButtonStyle" android:layout_width="match_parent" android:layout_height="match_parent" android:id="@+id/main"> <Button android:id="@+id/button" android:layout_width="wrap_content" android:layout_height="wrap_content" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:text="Take screenshot"/> <ImageView android:id="@+id/imageView" android:layout_below="@id/button" android:layout_width="match_parent" android:layout_height="match_parent" android:layout_centerHorizontal="true" android:layout_marginTop="30dp" android:scaleType="fitCenter"/> </RelativeLayout>
3단계 — 스크린샷 유틸리티 클래스 작성 (Screenshot.java)
새로운 자바(Java) 클래스를 생성하고 아래 코드를 추가합니다. 이 클래스가 스크린샷 기능의 핵심입니다. 대상 뷰의 드로잉 캐시를 활성화한 후 이를 비트맵(Bitmap)으로 변환해 반환하며, 루트 뷰(Root View)를 지정하면 화면 전체를 캡처할 수 있습니다.
import android.graphics.Bitmap;
import android.view.View;
public class Screenshot{
public static Bitmap takescreenshot(View v) {
v.setDrawingCacheEnabled(true);
v.buildDrawingCache(true);
Bitmap b = Bitmap.createBitmap(v.getDrawingCache());
v.setDrawingCacheEnabled(false);
return b;
}
public static Bitmap takescreenshotOfRootView(View v) {
return takescreenshot(v.getRootView());
}
}4단계 — 메인 액티비티 구현 (src/MainActivity.java)
버튼 클릭 시 스크린샷을 찍어 ImageView에 표시하고, 배경색을 변경하는 코드입니다. 아래 내용을 src/MainActivity.java에 추가합니다.
import android.graphics.Bitmap;
import android.graphics.Color;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
private View main;
private ImageView imageView;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
main = findViewById(R.id.main);
imageView = (ImageView) findViewById(R.id.imageView);
Button btn = (Button) findViewById(R.id.button);
btn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View view) {
Bitmap b = Screenshot.takescreenshotOfRootView(imageView);
imageView.setImageBitmap(b);
main.setBackgroundColor(Color.parseColor("#999999"));
}
});
}
}5단계 — 매니페스트 설정 (AndroidManifest.xml)
마지막으로 아래 코드를 androidManifest.xml 파일에 추가합니다.
<?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에 그대로 표시되고, 배경색이 회색(#999999)으로 바뀌는 것을 확인할 수 있습니다. 이처럼 드로잉 캐시를 활용하면 별도의 권한 없이도 앱 내부 화면을 손쉽게 비트맵 이미지로 저장하거나 활용할 수 있습니다.

