1.5/Android

android view visibility animation

하례은 2021. 4. 11. 20:59

 

개발을 하다보면 시각적인 효과를 위해 animation을 적용하는 경우가 많다. 보통은 anim directory에 animation xml 파일을 만들고 startAnimation을 통해 적용하는데... 

이걸 자동으로 Animation 효과를 넣어줄 수 있다..

android:animateLayoutChanges="true"

적용시켜줄 View의 부모 레이아웃에 넣어주면 알아서 animation 효과를 넣어준다.

 

예를 들어, view를 감추고 보여줄때 자연스럽게 위아래로 스르륵 (?) 효과를 넣어주고 싶을 때가 있다. 그럴때 사용하면 아주 좋다.

 

하지만 

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">

    <LinearLayout
        android:id="@+id/main_lin_1"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="10dp"
            android:layout_marginVertical="10dp"
            android:text="확인용"
            android:textColor="@color/black"
            android:textSize="20dp"
            android:textStyle="bold" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:animateLayoutChanges="true"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/main_img_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginVertical="10dp"
                android:scaleType="fitXY"
                android:src="@drawable/img_1"
                android:visibility="gone" />

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/main_lin_2"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="10dp"
            android:layout_marginVertical="10dp"
            android:text="확인용"
            android:textColor="@color/black"
            android:textSize="20dp"
            android:textStyle="bold" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/main_img_2"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginVertical="10dp"
                android:scaleType="fitXY"
                android:src="@drawable/img_1"
                android:visibility="gone" />

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/main_lin_3"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="10dp"
            android:layout_marginVertical="10dp"
            android:text="확인용"
            android:textColor="@color/black"
            android:textSize="20dp"
            android:textStyle="bold" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/main_img_3"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginVertical="10dp"
                android:scaleType="fitXY"
                android:src="@drawable/img_1"
                android:visibility="gone" />

        </LinearLayout>
    </LinearLayout>

    <LinearLayout
        android:id="@+id/main_lin_4"
        android:layout_width="match_parent"
        android:layout_height="wrap_content"
        android:orientation="vertical">

        <TextView
            android:layout_width="wrap_content"
            android:layout_height="wrap_content"
            android:layout_marginHorizontal="10dp"
            android:layout_marginVertical="10dp"
            android:text="확인용"
            android:textColor="@color/black"
            android:textSize="20dp"
            android:textStyle="bold" />

        <View
            android:layout_width="match_parent"
            android:layout_height="1dp"
            android:background="@color/black" />

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="wrap_content"
            android:orientation="vertical">

            <ImageView
                android:id="@+id/main_img_4"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:layout_marginVertical="10dp"
                android:scaleType="fitXY"
                android:src="@drawable/img_1"
                android:visibility="gone" />

        </LinearLayout>
    </LinearLayout>


</LinearLayout>

이런 리스트 형태의 뷰를 나열하는 경우가 있다. 이런 경우에도 자연스럽게 드르륵(?) 효과를 주고싶은데 여기서 효과를 주면

 

 

이렇게 어색하게 된다...

 

 

그래서 부모 레이아웃안에 다른 아이템들도 다 넣어줘야 하는데

<?xml version="1.0" encoding="utf-8"?>
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
    xmlns:app="http://schemas.android.com/apk/res-auto"
    xmlns:tools="http://schemas.android.com/tools"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    android:orientation="vertical"
    tools:context=".MainActivity">
    <ScrollView
        android:layout_width="match_parent"
        android:layout_height="match_parent">

        <LinearLayout
            android:layout_width="match_parent"
            android:layout_height="match_parent"
            android:orientation="vertical">
            <LinearLayout
                android:id="@+id/main_lin_1"
                android:layout_width="match_parent"
                android:layout_height="wrap_content"
                android:orientation="vertical">

                <TextView
                    android:layout_width="wrap_content"
                    android:layout_height="wrap_content"
                    android:layout_marginHorizontal="10dp"
                    android:layout_marginVertical="10dp"
                    android:text="확인용"
                    android:textColor="@color/black"
                    android:textSize="20dp"
                    android:textStyle="bold" />

                <View
                    android:layout_width="match_parent"
                    android:layout_height="1dp"
                    android:background="@color/black" />

                <LinearLayout
                    android:layout_width="match_parent"
                    android:layout_height="wrap_content"
                    android:animateLayoutChanges="true"
                    android:orientation="vertical">

                    <ImageView
                        android:id="@+id/main_img_1"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:layout_marginVertical="10dp"
                        android:scaleType="fitXY"
                        android:src="@drawable/img_1"
                        android:visibility="gone" />

                    <LinearLayout
                        android:id="@+id/main_lin_2"
                        android:layout_width="match_parent"
                        android:layout_height="wrap_content"
                        android:orientation="vertical">

                        <TextView
                            android:layout_width="wrap_content"
                            android:layout_height="wrap_content"
                            android:layout_marginHorizontal="10dp"
                            android:layout_marginVertical="10dp"
                            android:text="확인용"
                            android:textColor="@color/black"
                            android:textSize="20dp"
                            android:textStyle="bold" />

                        <View
                            android:layout_width="match_parent"
                            android:layout_height="1dp"
                            android:background="@color/black" />

                        <LinearLayout
                            android:layout_width="match_parent"
                            android:layout_height="wrap_content"
                            android:animateLayoutChanges="true"
                            android:orientation="vertical">

                            <ImageView
                                android:id="@+id/main_img_2"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:layout_marginVertical="10dp"
                                android:scaleType="fitXY"
                                android:src="@drawable/img_1"
                                android:visibility="gone" />

                            <LinearLayout
                                android:id="@+id/main_lin_3"
                                android:layout_width="match_parent"
                                android:layout_height="wrap_content"
                                android:orientation="vertical">

                                <TextView
                                    android:layout_width="wrap_content"
                                    android:layout_height="wrap_content"
                                    android:layout_marginHorizontal="10dp"
                                    android:layout_marginVertical="10dp"
                                    android:text="확인용"
                                    android:textColor="@color/black"
                                    android:textSize="20dp"
                                    android:textStyle="bold" />

                                <View
                                    android:layout_width="match_parent"
                                    android:layout_height="1dp"
                                    android:background="@color/black" />

                                <LinearLayout
                                    android:layout_width="match_parent"
                                    android:layout_height="wrap_content"
                                    android:animateLayoutChanges="true"
                                    android:orientation="vertical">

                                    <ImageView
                                        android:id="@+id/main_img_3"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:layout_marginVertical="10dp"
                                        android:scaleType="fitXY"
                                        android:src="@drawable/img_1"
                                        android:visibility="gone" />

                                    <LinearLayout
                                        android:id="@+id/main_lin_4"
                                        android:layout_width="match_parent"
                                        android:layout_height="wrap_content"
                                        android:orientation="vertical">

                                        <TextView
                                            android:layout_width="wrap_content"
                                            android:layout_height="wrap_content"
                                            android:layout_marginHorizontal="10dp"
                                            android:layout_marginVertical="10dp"
                                            android:text="확인용"
                                            android:textColor="@color/black"
                                            android:textSize="20dp"
                                            android:textStyle="bold" />

                                        <View
                                            android:layout_width="match_parent"
                                            android:layout_height="1dp"
                                            android:background="@color/black" />

                                        <LinearLayout
                                            android:layout_width="match_parent"
                                            android:layout_height="wrap_content"
                                            android:orientation="vertical"
                                            android:animateLayoutChanges="true">

                                            <ImageView
                                                android:id="@+id/main_img_4"
                                                android:layout_width="match_parent"
                                                android:layout_height="wrap_content"
                                                android:layout_marginVertical="10dp"
                                                android:scaleType="fitXY"
                                                android:src="@drawable/img_1"
                                                android:visibility="gone" />

                                        </LinearLayout>
                                    </LinearLayout>
                                </LinearLayout>
                            </LinearLayout>

                        </LinearLayout>
                    </LinearLayout>

                </LinearLayout>
            </LinearLayout>

        </LinearLayout>


    </ScrollView>


</LinearLayout>

쌓인 레이아웃을 보면 한숨이 나온다...

 

 

물론 아주 잘된다 ㅋㅋ.

 

이런 경우엔 그냥 RecyclerView를 사용하자!!

 

 

+linear/relative 말고 constraint 사용하세요!!!