[android] 회전 애니메이션

안드로이드 앱에서 화면이 회전할 때 애니메이션을 적용하는 방법을 알아보겠습니다.

1. Manifest 파일 설정

먼저, 액티비티를 선언한 Manifest 파일에서 각 액티비티에 대해 화면 회전 옵션을 설정해야 합니다.

<activity android:name=".MainActivity"
          android:screenOrientation="unspecified">
</activity>

screenOrientation 속성을 사용하여 각 액티비티의 화면 회전 옵션을 지정할 수 있습니다. 값으로 unspecified, landscape, portrait 등을 지정할 수 있습니다.

2. 애니메이션 리소스 생성

화면 회전 시에 적용할 애니메이션을 정의하기 위해 리소스 파일을 생성합니다.

res 폴더 내의 anim 폴더에 회전 애니메이션을 정의하는 XML 파일을 추가합니다.

<!-- res/anim/rotate.xml -->
<rotate xmlns:android="http://schemas.android.com/apk/res/android"
    android:fromDegrees="0"
    android:toDegrees="90"
    android:pivotX="50%"
    android:pivotY="50%"
    android:duration="500" />

위의 예시에서는 0도부터 90도까지 500ms 동안 회전하는 애니메이션을 정의했습니다.

3. 액티비티에서 애니메이션 적용

액티비티에서 onConfigurationChanged 메소드를 오버라이드하여 화면 회전 시에 애니메이션을 적용할 수 있습니다.

@Override
public void onConfigurationChanged(Configuration newConfig) {
    super.onConfigurationChanged(newConfig);
    if (newConfig.orientation == Configuration.ORIENTATION_LANDSCAPE) {
        // 가로 모드일 때
        Animation animation = AnimationUtils.loadAnimation(this, R.anim.rotate);
        findViewById(R.id.imageView).startAnimation(animation);  // 애니메이션 적용할 뷰의 아이디를 지정
    } else {
        // 세로 모드일 때
        // 세로 모드일 때의 처리
    }
}

마치며

위의 방법을 통해 안드로이드 앱에서 화면 회전 시에 애니메이션을 적용할 수 있습니다. 다양한 애니메이션 효과를 통해 사용자에게 더 다채로운 화면 전환을 제공할 수 있습니다.