안드로이드 프로그래밍[JAVA Code]

Animation : sample sliding

훈츠 2019. 12. 4. 05:16
반응형

안드로이드에서는 화면을 움직이거나 다른 것을 이동 할수 있도록 애니메이션 기능을 제공하고있다. 하는 방법은 res 폴더에 anim 이라는 폴더를 생성 후, 그 안에 움직일 xml 파일을 생성한다. 어디에서 어디로 어떤 크기로 움직일것인지 셋팅 할수있다. 그 후 MainActivity 에서 AnimationUtils.loadAnimation 를 통해서 시점을 완성 할 수 있다. 아래 예시를 보고 실습 해볼수 있다. 

1) anim 폴더 만들고 그안에 xml 파일 정의 하기 

anim 폴더 만들기 예시

<!-- translate_left 코드 -->

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

</set>


<!-- translate_right 코드 -->
<?xml version="1.0" encoding="utf-8"?>
<set xmlns:android="http://schemas.android.com/apk/res/android">
    <translate
        android:fromXDelta="0%p"
        android:toXDelta="100%p"
        android:duration="1500"
   />

</set>

2) main.xml 파일 정의 하기 : 프래임레이아웃 안에 두개의 레이아웃을 만들고 하나는 배경, 나머지하나는 슬라이딩 가능하도록 id=page라고 줬다. 

<?xml version="1.0" encoding="utf-8"?>
<FrameLayout xmlns:android="http://schemas.android.com/apk/res/android"
    android:layout_width="match_parent"
    android:layout_height="match_parent">
<LinearLayout
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:background="#ff5555ff">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="배경 영역"
        android:textSize="30sp"
        android:textColor="#ffffff"/>
</LinearLayout>
    
<LinearLayout
    android:id="@+id/page"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    android:layout_gravity="right"
    android:background="#ffff66">
    <TextView
        android:layout_width="wrap_content"
        android:layout_height="wrap_content"
        android:text="슬라이딩 영역"
        android:textColor="#000000"
        android:textSize="30sp"/>
</LinearLayout>
    
<Button
    android:id="@+id/button"
    android:layout_width="wrap_content"
    android:layout_height="wrap_content"
    android:text="열기"
    android:layout_gravity="center_vertical|right"/>

</FrameLayout>

3) Activity 에서 AnimationUtils.loadAnimation 이용하여, page 이동, 그후 슬라이딩 리스너 구현후, 만들어진 애니메이션 객체에 리스너 전달.

 

 

반응형