반응형
버튼을 눌렀을 때 보이지 않던 뷰가 슬라이딩 방식으로 나타나는 기능
여러 뷰를 중첩해두었다가 하나씩 전환하면서 보여주는 방식에 애니메이션을 결합
/app/res/anim/translate_left.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<translate
android:duration="500"
android:fillAfter="true"
android:fromXDelta="100%p"
android:repeatCount="0"
android:toXDelta="0%p" />
</set>
/app/res/anim/translate_right.xml
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android"
android:interpolator="@android:anim/accelerate_decelerate_interpolator">
<translate
android:duration="500"
android:fillAfter="true"
android:fromXDelta="0%p"
android:repeatCount="0"
android:toXDelta="100%p" />
</set>
MainActivity
package com.example.a48_pagesliding;
import androidx.appcompat.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.LinearLayout;
public class MainActivity extends AppCompatActivity {
boolean isPageOpen = false;
Animation translateLeftAnim;
Animation translateRightAnim;
LinearLayout page;
Button button;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
page = findViewById(R.id.page);
translateLeftAnim = AnimationUtils.loadAnimation(this, R.anim.translate_left);
translateRightAnim = AnimationUtils.loadAnimation(this, R.anim.translate_right);
// 애니메이션 리스너 설정
SlidingPageAnimationListener animListener = new SlidingPageAnimationListener();
translateLeftAnim.setAnimationListener(animListener);
translateRightAnim.setAnimationListener(animListener);
button = findViewById(R.id.button);
button.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
if (isPageOpen) {
page.startAnimation(translateRightAnim);
} else {
page.setVisibility(View.VISIBLE);
page.startAnimation(translateLeftAnim);
}
}
});
}
private class SlidingPageAnimationListener implements Animation.AnimationListener {
public void onAnimationEnd(Animation animation) {
if (isPageOpen) {
page.setVisibility(View.INVISIBLE);
button.setText("Open");
isPageOpen = false;
} else {
button.setText("Close");
isPageOpen = true;
}
}
@Override
public void onAnimationStart(Animation animation) {
}
@Override
public void onAnimationRepeat(Animation animation) {
}
}
}
반응형