이 튜토리얼에서는 안드로이드 앱에서 이미지를 부드럽게 360도 회전시키는 애니메이션을 구현하는 방법을 단계별로 살펴봅니다. XML 애니메이션 리소스와 AnimationUtils 클래스를 활용하면 복잡한 코드 없이도 자연스러운 회전 효과를 만들 수 있습니다.
1단계: 새 프로젝트 생성
Android Studio를 열고 File → New Project 메뉴로 이동한 뒤, 필요한 정보를 모두 입력하여 새 프로젝트를 생성합니다.
2단계: 레이아웃 파일 작성
res/layout/activity_main.xml 파일에 아래 코드를 추가합니다. 화면 중앙에 ImageView를 배치하고, 그 아래에 회전을 시작할 Button을 배치합니다.
<?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"
tools:context=".MainActivity">
<ImageView android:id="@+id/imageView"
android:layout_width="wrap_content"
android:layout_height="250dp"
android:layout_centerInParent="true"
android:src="@drawable/image"/>
<Button
android:id="@+id/btnRotate"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:layout_below="@id/imageView"
android:layout_marginTop="50dp"
android:layout_centerInParent="true"
android:layout_marginBottom="10dp"
android:text="Rotate" />
</RelativeLayout>3단계: 회전 애니메이션 리소스 생성
res 폴더 안에 anim이라는 이름의 새 폴더를 생성합니다. anim 폴더를 마우스 오른쪽 버튼으로 클릭한 후 New → Animation Resource File을 선택하고 rotate.xml 파일을 만든 다음, 아래 코드를 입력합니다.
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="https://schemas.android.com/apk/res/android">
<rotate android:fromDegrees="0"
android:toDegrees="360"
android:pivotX="50%"
android:pivotY="50%"
android:duration="10000" />
</set>여기서 fromDegrees는 시작 각도(0도), toDegrees는 종료 각도(360도)를 의미합니다. pivotX와 pivotY를 50%로 설정하면 이미지의 중심을 축으로 회전하며, duration 값(10000ms = 10초)을 조절해 회전 속도를 변경할 수 있습니다.
4단계: MainActivity 작성
src/MainActivity.java 파일에 아래 코드를 추가합니다. 버튼을 클릭하면 rotate 애니메이션이 로드되어 이미지에 적용됩니다.
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.widget.Button;
import android.widget.ImageView;
public class MainActivity extends AppCompatActivity {
ImageView imageView;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
imageView = findViewById(R.id.imageView);
button = findViewById(R.id.btnRotate);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Animation animation = AnimationUtils.loadAnimation(getApplicationContext(),
R.anim.rotate);
imageView.startAnimation(animation);
}
});
}
}5단계: 매니페스트 확인
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 아이콘을 클릭하세요. 목록에서 사용 중인 모바일 기기를 선택하면, 기기 화면에 아래와 같은 기본 화면이 표시됩니다.

Rotate 버튼을 누르면 이미지가 중심축을 기준으로 10초 동안 한 바퀴 부드럽게 회전하는 것을 확인할 수 있습니다. duration 값을 줄이면 더 빠른 회전, 늘리면 더 완만한 회전 효과를 얻을 수 있으니 다양하게 응용해 보세요.