[Android Studio] DIalog Animation

노유성·2023년 7월 1일
0
post-thumbnail

🌞들어가며

다이얼로그가 생성될 때 특정 애니메이션을 갖고 생성되었으면 할 때가 있다. 이에 아래에서 위로 올라오는 dialog를 만드는 예제이다.

⭐과정

🪐animation 정의

res/anim 폴더에 열리도 닫힐 때의 animation을 정의한다.

☄️open.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromYDelta="100%"
        android:toYDelta="0%"
        android:duration="400"/>
</set>

위 코드는 아래에서 위로 0.4초의 시간을 갖고 올라가는 예제이다.

🌈set tag

set 태그는 안드로이드 애니메이션 리소스 파일에서 애니메이션 집합을 정의하는 데 사용되는 태그입니다. set 태그는 다른 애니메이션 태그들을 포함할 수 있으며, 각 애니메이션은 동시에 또는 순차적으로 실행될 수 있습니다
-chatGPT

☄️close.xml

<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
    android:interpolator="@android:anim/accelerate_interpolator">
    <translate
        android:fromYDelta="0%"
        android:toYDelta= "100%"
        android:duration="400"/>
</set>

open과 마찬가지이지만 반대로 이번엔 내려가는 예제이다.

🪐style.xml

만들어놓은 애니메이션은 어떤 상황에 적용할 것인지 정의한 스타일이다.

<style name="AnimationPopupStyle">
    <item name="android:windowEnterAnimation">@anim/open</item>
    <item name="android:windowExitAnimation">@anim/close</item>
</style>

팝업창이 열리고 닫힐 때 어떤 속성을 사용할 것인지 정의한 파일이다.

🪐Dialog.java

public class BaseDialog extends Dialog {
	protected Context mContext;
	public BaseDialog(Context context, int layoutId ) {
		super( context );
		this.requestWindowFeature(Window.FEATURE_NO_TITLE);
		setContentView( layoutId );
		this.mContext = context;

		setCancelable( true );
		setCanceledOnTouchOutside( true );

		Window window = getWindow();
		if( window != null ) {
			// 백그라운드 투명
			window.setBackgroundDrawable(new ColorDrawable(Color.TRANSPARENT));

			WindowManager.LayoutParams params = window.getAttributes();
			// 화면에 가득 차도록
			params.width     = WindowManager.LayoutParams.MATCH_PARENT;
			params.height    = WindowManager.LayoutParams.WRAP_CONTENT;

			// 열기&닫기 시 애니메이션 설정
			params.windowAnimations = R.style.AnimationPopupStyle;
			window.setAttributes( params );
			// UI 하단 정렬
			window.setGravity( Gravity.BOTTOM );
		}
	}
}

이를 활용해 다이얼로그를 정의했다. 다른 Dialog를 사용할 때 상속받아서 사용하면된다.

profile
풀스택개발자가되고싶습니다:)

0개의 댓글